[TASK] Re-work/simplify copyright header in PHP files - Part 7
[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 * @author Felix Kopp <felix-source@phorax.com>
21 */
22 class BackendUserRepository extends \TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository {
23
24 /**
25 * Finds Backend Users on a given list of uids
26 *
27 * @param array $uidList
28 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
29 */
30 public function findByUidList(array $uidList) {
31 $query = $this->createQuery();
32 return $query->matching($query->in('uid', $GLOBALS['TYPO3_DB']->cleanIntArray($uidList)))->execute();
33 }
34
35 /**
36 * Find Backend Users matching to Demand object properties
37 *
38 * @param \TYPO3\CMS\Beuser\Domain\Model\Demand $demand
39 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
40 */
41 public function findDemanded(\TYPO3\CMS\Beuser\Domain\Model\Demand $demand) {
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 $constraints[] = $query->like(
50 'userName',
51 '%' . $GLOBALS['TYPO3_DB']->escapeStrForLike($demand->getUserName(), 'be_users') . '%'
52 );
53 }
54 // Only display admin users
55 if ($demand->getUserType() == \TYPO3\CMS\Beuser\Domain\Model\Demand::USERTYPE_ADMINONLY) {
56 $constraints[] = $query->equals('admin', 1);
57 }
58 // Only display non-admin users
59 if ($demand->getUserType() == \TYPO3\CMS\Beuser\Domain\Model\Demand::USERTYPE_USERONLY) {
60 $constraints[] = $query->equals('admin', 0);
61 }
62 // Only display active users
63 if ($demand->getStatus() == \TYPO3\CMS\Beuser\Domain\Model\Demand::STATUS_ACTIVE) {
64 $constraints[] = $query->equals('disable', 0);
65 }
66 // Only display in-active users
67 if ($demand->getStatus() == \TYPO3\CMS\Beuser\Domain\Model\Demand::STATUS_INACTIVE) {
68 $constraints[] = $query->logicalOr($query->equals('disable', 1));
69 }
70 // Not logged in before
71 if ($demand->getLogins() == \TYPO3\CMS\Beuser\Domain\Model\Demand::LOGIN_NONE) {
72 $constraints[] = $query->equals('lastlogin', 0);
73 }
74 // At least one login
75 if ($demand->getLogins() == \TYPO3\CMS\Beuser\Domain\Model\Demand::LOGIN_SOME) {
76 $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
77 }
78 // In backend user group
79 // @TODO: Refactor for real n:m relations
80 if ($demand->getBackendUserGroup()) {
81 $constraints[] = $query->logicalOr(
82 $query->equals('usergroup', (int)$demand->getBackendUserGroup()->getUid()),
83 $query->like('usergroup', (int)$demand->getBackendUserGroup()->getUid() . ',%'),
84 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()->getUid()),
85 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()->getUid() . ',%')
86 );
87 $query->contains('usergroup', $demand->getBackendUserGroup());
88 }
89 $query->matching($query->logicalAnd($constraints));
90 return $query->execute();
91 }
92
93 /**
94 * Find Backend Users currently online
95 *
96 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult<\TYPO3\CMS\Beuser\Domain\Model\BackendUser>
97 */
98 public function findOnline() {
99 $uids = array();
100 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('DISTINCT ses_userid', 'be_sessions', '');
101 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
102 $uids[] = $row['ses_userid'];
103 }
104 $GLOBALS['TYPO3_DB']->sql_free_result($res);
105 $query = $this->createQuery();
106 $query->matching($query->in('uid', $uids));
107 return $query->execute();
108 }
109
110 /**
111 * Overwrite createQuery to don't respect enable fields
112 *
113 * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
114 */
115 public function createQuery() {
116 $query = parent::createQuery();
117 $query->getQuerySettings()->setIgnoreEnableFields(TRUE);
118 $query->getQuerySettings()->setIncludeDeleted(TRUE);
119 return $query;
120 }
121
122 }