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