[TASK] Extend username search in beuser module
[Packages/TYPO3.CMS.git] / typo3 / sysext / beuser / Classes / Domain / Repository / BackendUserRepository.php
1 <?php
2 namespace TYPO3\CMS\Beuser\Domain\Repository;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Repository for \TYPO3\CMS\Beuser\Domain\Model\BackendUser
19 */
20 class BackendUserRepository extends \TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository
21 {
22 /**
23 * Finds Backend Users on a given list of uids
24 *
25 * @param array $uidList
26 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
27 */
28 public function findByUidList(array $uidList)
29 {
30 $query = $this->createQuery();
31 return $query->matching($query->in('uid', $GLOBALS['TYPO3_DB']->cleanIntArray($uidList)))->execute();
32 }
33
34 /**
35 * Find Backend Users matching to Demand object properties
36 *
37 * @param \TYPO3\CMS\Beuser\Domain\Model\Demand $demand
38 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
39 */
40 public function findDemanded(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand)
41 {
42 $constraints = array();
43 $query = $this->createQuery();
44 // Find invisible as well, but not deleted
45 $constraints[] = $query->equals('deleted', 0);
46 $query->setOrderings(array('userName' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
47 // Username
48 if ($demand->getUserName() !== '') {
49 $searchConstraints = array();
50 foreach (array('userName', 'uid', 'realName') as $field) {
51 $searchConstraints[] = $query->like(
52 $field, '%' . $GLOBALS['TYPO3_DB']->escapeStrForLike($demand->getUserName(), 'be_users') . '%'
53 );
54 }
55 $constraints[] = $query->logicalOr($searchConstraints);
56 }
57 // Only display admin users
58 if ($demand->getUserType() == \TYPO3\CMS\Beuser\Domain\Model\Demand::USERTYPE_ADMINONLY) {
59 $constraints[] = $query->equals('admin', 1);
60 }
61 // Only display non-admin users
62 if ($demand->getUserType() == \TYPO3\CMS\Beuser\Domain\Model\Demand::USERTYPE_USERONLY) {
63 $constraints[] = $query->equals('admin', 0);
64 }
65 // Only display active users
66 if ($demand->getStatus() == \TYPO3\CMS\Beuser\Domain\Model\Demand::STATUS_ACTIVE) {
67 $constraints[] = $query->equals('disable', 0);
68 }
69 // Only display in-active users
70 if ($demand->getStatus() == \TYPO3\CMS\Beuser\Domain\Model\Demand::STATUS_INACTIVE) {
71 $constraints[] = $query->logicalOr($query->equals('disable', 1));
72 }
73 // Not logged in before
74 if ($demand->getLogins() == \TYPO3\CMS\Beuser\Domain\Model\Demand::LOGIN_NONE) {
75 $constraints[] = $query->equals('lastlogin', 0);
76 }
77 // At least one login
78 if ($demand->getLogins() == \TYPO3\CMS\Beuser\Domain\Model\Demand::LOGIN_SOME) {
79 $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
80 }
81 // In backend user group
82 // @TODO: Refactor for real n:m relations
83 if ($demand->getBackendUserGroup()) {
84 $constraints[] = $query->logicalOr(
85 $query->equals('usergroup', (int)$demand->getBackendUserGroup()->getUid()),
86 $query->like('usergroup', (int)$demand->getBackendUserGroup()->getUid() . ',%'),
87 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()->getUid()),
88 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()->getUid() . ',%')
89 );
90 $query->contains('usergroup', $demand->getBackendUserGroup());
91 }
92 $query->matching($query->logicalAnd($constraints));
93 return $query->execute();
94 }
95
96 /**
97 * Find Backend Users currently online
98 *
99 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
100 */
101 public function findOnline()
102 {
103 $uids = array();
104 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('DISTINCT ses_userid', 'be_sessions', '');
105 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
106 $uids[] = $row['ses_userid'];
107 }
108 $GLOBALS['TYPO3_DB']->sql_free_result($res);
109 $query = $this->createQuery();
110 $query->matching($query->in('uid', $uids));
111 return $query->execute();
112 }
113
114 /**
115 * Overwrite createQuery to don't respect enable fields
116 *
117 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
118 */
119 public function createQuery()
120 {
121 $query = parent::createQuery();
122 $query->getQuerySettings()->setIgnoreEnableFields(true);
123 $query->getQuerySettings()->setIncludeDeleted(true);
124 return $query;
125 }
126 }