[TASK] Doctrine: Migrate escapeStrForLike()
[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 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Repository for \TYPO3\CMS\Beuser\Domain\Model\BackendUser
22 */
23 class BackendUserRepository extends \TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository
24 {
25 /**
26 * Finds Backend Users on a given list of uids
27 *
28 * @param array $uidList
29 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
30 */
31 public function findByUidList(array $uidList)
32 {
33 $query = $this->createQuery();
34 return $query->matching($query->in('uid', array_map('intval', $uidList)))->execute();
35 }
36
37 /**
38 * Find Backend Users matching to Demand object properties
39 *
40 * @param \TYPO3\CMS\Beuser\Domain\Model\Demand $demand
41 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
42 */
43 public function findDemanded(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand)
44 {
45 $constraints = array();
46 $query = $this->createQuery();
47 // Find invisible as well, but not deleted
48 $constraints[] = $query->equals('deleted', 0);
49 $query->setOrderings(array('userName' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
50 // Username
51 if ($demand->getUserName() !== '') {
52 $searchConstraints = array();
53 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
54 foreach (array('userName', 'uid', 'realName') as $field) {
55 $searchConstraints[] = $query->like(
56 $field, '%' . $queryBuilder->escapeLikeWildcards($demand->getUserName()) . '%'
57 );
58 }
59 $constraints[] = $query->logicalOr($searchConstraints);
60 }
61 // Only display admin users
62 if ($demand->getUserType() == \TYPO3\CMS\Beuser\Domain\Model\Demand::USERTYPE_ADMINONLY) {
63 $constraints[] = $query->equals('admin', 1);
64 }
65 // Only display non-admin users
66 if ($demand->getUserType() == \TYPO3\CMS\Beuser\Domain\Model\Demand::USERTYPE_USERONLY) {
67 $constraints[] = $query->equals('admin', 0);
68 }
69 // Only display active users
70 if ($demand->getStatus() == \TYPO3\CMS\Beuser\Domain\Model\Demand::STATUS_ACTIVE) {
71 $constraints[] = $query->equals('disable', 0);
72 }
73 // Only display in-active users
74 if ($demand->getStatus() == \TYPO3\CMS\Beuser\Domain\Model\Demand::STATUS_INACTIVE) {
75 $constraints[] = $query->logicalOr($query->equals('disable', 1));
76 }
77 // Not logged in before
78 if ($demand->getLogins() == \TYPO3\CMS\Beuser\Domain\Model\Demand::LOGIN_NONE) {
79 $constraints[] = $query->equals('lastlogin', 0);
80 }
81 // At least one login
82 if ($demand->getLogins() == \TYPO3\CMS\Beuser\Domain\Model\Demand::LOGIN_SOME) {
83 $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
84 }
85 // In backend user group
86 // @TODO: Refactor for real n:m relations
87 if ($demand->getBackendUserGroup()) {
88 $constraints[] = $query->logicalOr(
89 $query->equals('usergroup', (int)$demand->getBackendUserGroup()->getUid()),
90 $query->like('usergroup', (int)$demand->getBackendUserGroup()->getUid() . ',%'),
91 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()->getUid()),
92 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()->getUid() . ',%')
93 );
94 $query->contains('usergroup', $demand->getBackendUserGroup());
95 }
96 $query->matching($query->logicalAnd($constraints));
97 return $query->execute();
98 }
99
100 /**
101 * Find Backend Users currently online
102 *
103 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
104 */
105 public function findOnline()
106 {
107 $uids = array();
108
109 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_sessions');
110
111 $res = $queryBuilder
112 ->select('ses_userid')
113 ->from('be_sessions')
114 ->groupBy('ses_userid')
115 ->execute();
116
117 while ($row = $res->fetch()) {
118 $uids[] = $row['ses_userid'];
119 }
120
121 $query = $this->createQuery();
122 $query->matching($query->in('uid', $uids));
123 return $query->execute();
124 }
125
126 /**
127 * Overwrite createQuery to don't respect enable fields
128 *
129 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
130 */
131 public function createQuery()
132 {
133 $query = parent::createQuery();
134 $query->getQuerySettings()->setIgnoreEnableFields(true);
135 $query->getQuerySettings()->setIncludeDeleted(true);
136 return $query;
137 }
138 }