[FEATURE] Migrate EXT: beuser to Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / beuser / Classes / Domain / Repository / BackendUserRepository.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Felix Kopp <felix-source@phorax.com>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Repository for Tx_Beuser_Domain_Model_BackendUser
29 *
30 * @author Felix Kopp <felix-source@phorax.com>
31 * @package TYPO3
32 * @subpackage beuser
33 */
34 class Tx_Beuser_Domain_Repository_BackendUserRepository extends Tx_Extbase_Domain_Repository_BackendUserRepository {
35
36
37 /**
38 * Finds Backend Users on a given list of uids
39 *
40 * @param array $uidList
41 * @return Tx_Extbase_Persistence_QueryResult<Tx_Beuser_Domain_Model_BackendUser>
42 */
43 public function findByUidList($uidList) {
44 $query = $this->createQuery();
45 return $query->matching(
46 $query->in('uid', $uidList)
47 )->execute();
48 }
49
50 /**
51 * Find Backend Users matching to Demand object properties
52 *
53 * @param Tx_Beuser_Domain_Model_Demand $demand
54 * @return Tx_Extbase_Persistence_QueryResult<Tx_Beuser_Domain_Model_BackendUser>
55 */
56 public function findDemanded(Tx_Beuser_Domain_Model_Demand $demand) {
57 $constraints = array();
58 $query = $this->createQuery();
59
60 // Find invisible as well, but not deleted
61
62 $constraints[] = $query->equals('deleted', 0);
63
64 $query->setOrderings(array('userName' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING));
65
66 // Username
67 if ($demand->getUserName() !== '') {
68 $constraints[] = $query->like('userName', '%' . $demand->getUserName() . '%');
69 }
70
71 // Only display admin users
72 if ($demand->getUserType() == Tx_Beuser_Domain_Model_Demand::USERTYPE_ADMINONLY) {
73 $constraints[] = $query->equals('admin', 1);
74 }
75
76 // Only display non-admin users
77 if ($demand->getUserType() == Tx_Beuser_Domain_Model_Demand::USERTYPE_USERONLY) {
78 $constraints[] = $query->equals('admin', 0);
79 }
80
81 // Only display active users
82 if ($demand->getStatus() == Tx_Beuser_Domain_Model_Demand::STATUS_ACTIVE) {
83 $constraints[] = $query->equals('disable', 0);
84 }
85
86 // Only display in-active users
87 if ($demand->getStatus() == Tx_Beuser_Domain_Model_Demand::STATUS_INACTIVE) {
88 $constraints[] = $query->logicalOr(
89 $query->equals('disable', 1)
90 );
91 }
92
93 // Not logged in before
94 if ($demand->getLogins() == Tx_Beuser_Domain_Model_Demand::LOGIN_NONE) {
95 $constraints[] = $query->equals('lastlogin', 0);
96 }
97
98 // At least one login
99 if ($demand->getLogins() == Tx_Beuser_Domain_Model_Demand::LOGIN_SOME) {
100 $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
101 }
102
103 // In backend user group
104 // @TODO: Refactor for real n:m relations
105 if ($demand->getBackendUserGroup()) {
106 $constraints[] = $query->logicalOr(
107 $query->equals('usergroup', $demand->getBackendUserGroup()->getUid()),
108 $query->like('usergroup', $demand->getBackendUserGroup()->getUid() . ',%'),
109 $query->like('usergroup', '%,' . $demand->getBackendUserGroup()->getUid()),
110 $query->like('usergroup', '%,' . $demand->getBackendUserGroup()->getUid() . ',%')
111 );
112
113 $query->contains('usergroup', $demand->getBackendUserGroup());
114 }
115
116 $query->matching($query->logicalAnd($constraints));
117
118 return $query->execute();
119 }
120
121 /**
122 * Find Backend Users currently online
123 *
124 * @return Tx_Extbase_Persistence_QueryResult<Tx_Beuser_Domain_Model_BackendUser>
125 */
126 public function findOnline() {
127 $uids = array();
128
129 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('DISTINCT ses_userid', 'be_sessions', '');
130 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
131 $uids[] = $row['ses_userid'];
132 }
133
134 $query = $this->createQuery();
135 $query->matching($query->in('uid', $uids));
136
137 return $query->execute();
138 }
139
140 /**
141 * Overwrite createQuery to don't respect enable fields
142 *
143 * @return Tx_Extbase_Persistence_QueryInterface
144 */
145 public function createQuery() {
146 $query = parent::createQuery();
147 $query->getQuerySettings()->setRespectEnableFields(FALSE);
148 return $query;
149 }
150
151 }
152
153 ?>