[TASK] Apply CGL compatibility (PSR-1 and PSR-2)
[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', $GLOBALS['TYPO3_DB']->cleanIntArray($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 foreach (array('userName', 'uid', 'realName') as $field) {
54 $searchConstraints[] = $query->like(
55 $field, '%' . $GLOBALS['TYPO3_DB']->escapeStrForLike($demand->getUserName(), 'be_users') . '%'
56 );
57 }
58 $constraints[] = $query->logicalOr($searchConstraints);
59 }
60 // Only display admin users
61 if ($demand->getUserType() == \TYPO3\CMS\Beuser\Domain\Model\Demand::USERTYPE_ADMINONLY) {
62 $constraints[] = $query->equals('admin', 1);
63 }
64 // Only display non-admin users
65 if ($demand->getUserType() == \TYPO3\CMS\Beuser\Domain\Model\Demand::USERTYPE_USERONLY) {
66 $constraints[] = $query->equals('admin', 0);
67 }
68 // Only display active users
69 if ($demand->getStatus() == \TYPO3\CMS\Beuser\Domain\Model\Demand::STATUS_ACTIVE) {
70 $constraints[] = $query->equals('disable', 0);
71 }
72 // Only display in-active users
73 if ($demand->getStatus() == \TYPO3\CMS\Beuser\Domain\Model\Demand::STATUS_INACTIVE) {
74 $constraints[] = $query->logicalOr($query->equals('disable', 1));
75 }
76 // Not logged in before
77 if ($demand->getLogins() == \TYPO3\CMS\Beuser\Domain\Model\Demand::LOGIN_NONE) {
78 $constraints[] = $query->equals('lastlogin', 0);
79 }
80 // At least one login
81 if ($demand->getLogins() == \TYPO3\CMS\Beuser\Domain\Model\Demand::LOGIN_SOME) {
82 $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
83 }
84 // In backend user group
85 // @TODO: Refactor for real n:m relations
86 if ($demand->getBackendUserGroup()) {
87 $constraints[] = $query->logicalOr(
88 $query->equals('usergroup', (int)$demand->getBackendUserGroup()->getUid()),
89 $query->like('usergroup', (int)$demand->getBackendUserGroup()->getUid() . ',%'),
90 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()->getUid()),
91 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()->getUid() . ',%')
92 );
93 $query->contains('usergroup', $demand->getBackendUserGroup());
94 }
95 $query->matching($query->logicalAnd($constraints));
96 return $query->execute();
97 }
98
99 /**
100 * Find Backend Users currently online
101 *
102 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
103 */
104 public function findOnline()
105 {
106 $uids = array();
107
108 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_sessions');
109
110 $res = $queryBuilder
111 ->select('ses_userid')
112 ->from('be_sessions')
113 ->groupBy('ses_userid')
114 ->execute();
115
116 while ($row = $res->fetch()) {
117 $uids[] = $row['ses_userid'];
118 }
119
120 $query = $this->createQuery();
121 $query->matching($query->in('uid', $uids));
122 return $query->execute();
123 }
124
125 /**
126 * Overwrite createQuery to don't respect enable fields
127 *
128 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
129 */
130 public function createQuery()
131 {
132 $query = parent::createQuery();
133 $query->getQuerySettings()->setIgnoreEnableFields(true);
134 $query->getQuerySettings()->setIncludeDeleted(true);
135 return $query;
136 }
137 }