[BUGFIX] Fallback to empty array if ExportController receives no input
[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\Beuser\Domain\Model\Demand;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Session\Backend\SessionBackendInterface;
21 use TYPO3\CMS\Core\Session\SessionManager;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository;
24 use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
25 use TYPO3\CMS\Extbase\Persistence\QueryInterface;
26
27 /**
28 * Repository for \TYPO3\CMS\Beuser\Domain\Model\BackendUser
29 * @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API.
30 */
31 class BackendUserRepository extends BackendUserGroupRepository
32 {
33 /**
34 * Finds Backend Users on a given list of uids
35 *
36 * @param array $uidList
37 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
38 */
39 public function findByUidList(array $uidList)
40 {
41 $query = $this->createQuery();
42 $query->matching($query->in('uid', array_map('intval', $uidList)));
43 /** @var QueryResult $result */
44 $result = $query->execute();
45 return $result;
46 }
47
48 /**
49 * Find Backend Users matching to Demand object properties
50 *
51 * @param Demand $demand
52 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
53 */
54 public function findDemanded(Demand $demand)
55 {
56 $constraints = [];
57 $query = $this->createQuery();
58 // Find invisible as well, but not deleted
59 $constraints[] = $query->equals('deleted', 0);
60 $query->setOrderings(['userName' => QueryInterface::ORDER_ASCENDING]);
61 // Username
62 if ($demand->getUserName() !== '') {
63 $searchConstraints = [];
64 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
65 foreach (['userName', 'uid', 'realName'] as $field) {
66 $searchConstraints[] = $query->like(
67 $field,
68 '%' . $queryBuilder->escapeLikeWildcards($demand->getUserName()) . '%'
69 );
70 }
71 $constraints[] = $query->logicalOr($searchConstraints);
72 }
73 // Only display admin users
74 if ($demand->getUserType() == Demand::USERTYPE_ADMINONLY) {
75 $constraints[] = $query->equals('admin', 1);
76 }
77 // Only display non-admin users
78 if ($demand->getUserType() == Demand::USERTYPE_USERONLY) {
79 $constraints[] = $query->equals('admin', 0);
80 }
81 // Only display active users
82 if ($demand->getStatus() == Demand::STATUS_ACTIVE) {
83 $constraints[] = $query->equals('disable', 0);
84 }
85 // Only display in-active users
86 if ($demand->getStatus() == Demand::STATUS_INACTIVE) {
87 $constraints[] = $query->logicalOr($query->equals('disable', 1));
88 }
89 // Not logged in before
90 if ($demand->getLogins() == Demand::LOGIN_NONE) {
91 $constraints[] = $query->equals('lastlogin', 0);
92 }
93 // At least one login
94 if ($demand->getLogins() == Demand::LOGIN_SOME) {
95 $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
96 }
97 // In backend user group
98 // @TODO: Refactor for real n:m relations
99 if ($demand->getBackendUserGroup()) {
100 $constraints[] = $query->logicalOr([
101 $query->equals('usergroup', (int)$demand->getBackendUserGroup()),
102 $query->like('usergroup', (int)$demand->getBackendUserGroup() . ',%'),
103 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()),
104 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup() . ',%')
105 ]);
106 }
107 $query->matching($query->logicalAnd($constraints));
108 /** @var QueryResult $result */
109 $result = $query->execute();
110 return $result;
111 }
112
113 /**
114 * Find Backend Users currently online
115 *
116 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
117 */
118 public function findOnline()
119 {
120 $uids = [];
121 foreach ($this->getSessionBackend()->getAll() as $sessionRecord) {
122 if (isset($sessionRecord['ses_userid']) && !in_array($sessionRecord['ses_userid'], $uids, true)) {
123 $uids[] = $sessionRecord['ses_userid'];
124 }
125 }
126
127 $query = $this->createQuery();
128 $query->matching($query->in('uid', $uids));
129 /** @var QueryResult $result */
130 $result = $query->execute();
131 return $result;
132 }
133
134 /**
135 * Overwrite createQuery to don't respect enable fields
136 *
137 * @return QueryInterface
138 */
139 public function createQuery()
140 {
141 $query = parent::createQuery();
142 $query->getQuerySettings()->setIgnoreEnableFields(true);
143 $query->getQuerySettings()->setIncludeDeleted(true);
144 return $query;
145 }
146
147 /**
148 * @return SessionBackendInterface
149 */
150 protected function getSessionBackend()
151 {
152 $loginType = $this->getBackendUserAuthentication()->getLoginType();
153 return GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend($loginType);
154 }
155
156 /**
157 * @return BackendUserAuthentication
158 */
159 protected function getBackendUserAuthentication()
160 {
161 return $GLOBALS['BE_USER'];
162 }
163 }