1<?php 2 3/* 4 * This file is part of the TYPO3 CMS project. 5 * 6 * It is free software; you can redistribute it and/or modify it under 7 * the terms of the GNU General Public License, either version 2 8 * of the License, or any later version. 9 * 10 * For the full copyright and license information, please read the 11 * LICENSE.txt file that was distributed with this source code. 12 * 13 * The TYPO3 project - inspiring people to share! 14 */ 15 16namespace TYPO3\CMS\Beuser\Domain\Repository; 17 18use TYPO3\CMS\Beuser\Domain\Model\Demand; 19use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; 20use TYPO3\CMS\Core\Database\ConnectionPool; 21use TYPO3\CMS\Core\Session\Backend\SessionBackendInterface; 22use TYPO3\CMS\Core\Session\SessionManager; 23use TYPO3\CMS\Core\Utility\GeneralUtility; 24use TYPO3\CMS\Core\Utility\MathUtility; 25use TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository; 26use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult; 27use TYPO3\CMS\Extbase\Persistence\QueryInterface; 28 29/** 30 * Repository for \TYPO3\CMS\Beuser\Domain\Model\BackendUser 31 * @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API. 32 */ 33class BackendUserRepository extends BackendUserGroupRepository 34{ 35 /** 36 * Finds Backend Users on a given list of uids 37 * 38 * @param array $uidList 39 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult 40 */ 41 public function findByUidList(array $uidList) 42 { 43 $query = $this->createQuery(); 44 $query->matching($query->in('uid', array_map('intval', $uidList))); 45 /** @var QueryResult $result */ 46 $result = $query->execute(); 47 return $result; 48 } 49 50 /** 51 * Find Backend Users matching to Demand object properties 52 * 53 * @param Demand $demand 54 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult 55 */ 56 public function findDemanded(Demand $demand) 57 { 58 $constraints = []; 59 $query = $this->createQuery(); 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', 'realName'] as $field) { 66 $searchConstraints[] = $query->like( 67 $field, 68 '%' . $queryBuilder->escapeLikeWildcards($demand->getUserName()) . '%' 69 ); 70 } 71 if (MathUtility::canBeInterpretedAsInteger($demand->getUserName())) { 72 $searchConstraints[] = $query->equals('uid', (int)$demand->getUserName()); 73 } 74 $constraints[] = $query->logicalOr($searchConstraints); 75 } 76 // Only display admin users 77 if ($demand->getUserType() == Demand::USERTYPE_ADMINONLY) { 78 $constraints[] = $query->equals('admin', 1); 79 } 80 // Only display non-admin users 81 if ($demand->getUserType() == Demand::USERTYPE_USERONLY) { 82 $constraints[] = $query->equals('admin', 0); 83 } 84 // Only display active users 85 if ($demand->getStatus() == Demand::STATUS_ACTIVE) { 86 $constraints[] = $query->equals('disable', 0); 87 } 88 // Only display in-active users 89 if ($demand->getStatus() == Demand::STATUS_INACTIVE) { 90 $constraints[] = $query->logicalOr($query->equals('disable', 1)); 91 } 92 // Not logged in before 93 if ($demand->getLogins() == Demand::LOGIN_NONE) { 94 $constraints[] = $query->equals('lastlogin', 0); 95 } 96 // At least one login 97 if ($demand->getLogins() == Demand::LOGIN_SOME) { 98 $constraints[] = $query->logicalNot($query->equals('lastlogin', 0)); 99 } 100 // In backend user group 101 // @TODO: Refactor for real n:m relations 102 if ($demand->getBackendUserGroup()) { 103 $constraints[] = $query->logicalOr([ 104 $query->equals('usergroup', (int)$demand->getBackendUserGroup()), 105 $query->like('usergroup', (int)$demand->getBackendUserGroup() . ',%'), 106 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()), 107 $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup() . ',%') 108 ]); 109 } 110 if ($constraints !== []) { 111 $query->matching($query->logicalAnd($constraints)); 112 } 113 /** @var QueryResult $result */ 114 $result = $query->execute(); 115 return $result; 116 } 117 118 /** 119 * Find Backend Users currently online 120 * 121 * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult 122 */ 123 public function findOnline() 124 { 125 $uids = []; 126 foreach ($this->getSessionBackend()->getAll() as $sessionRecord) { 127 if (isset($sessionRecord['ses_userid']) && !in_array($sessionRecord['ses_userid'], $uids, true)) { 128 $uids[] = $sessionRecord['ses_userid']; 129 } 130 } 131 132 $query = $this->createQuery(); 133 $query->matching($query->in('uid', $uids)); 134 /** @var QueryResult $result */ 135 $result = $query->execute(); 136 return $result; 137 } 138 139 /** 140 * Overwrite createQuery to don't respect enable fields 141 * 142 * @return QueryInterface 143 */ 144 public function createQuery() 145 { 146 $query = parent::createQuery(); 147 $query->getQuerySettings()->setIgnoreEnableFields(true); 148 return $query; 149 } 150 151 /** 152 * @return SessionBackendInterface 153 */ 154 protected function getSessionBackend() 155 { 156 $loginType = $this->getBackendUserAuthentication()->getLoginType(); 157 return GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend($loginType); 158 } 159 160 /** 161 * @return BackendUserAuthentication 162 */ 163 protected function getBackendUserAuthentication() 164 { 165 return $GLOBALS['BE_USER']; 166 } 167} 168