BackendUserRepository.php 5.6 KB
Newer Older
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6 7 8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10 11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13 14
 * The TYPO3 project - inspiring people to share!
 */
15

16 17
namespace TYPO3\CMS\Beuser\Domain\Repository;

18
use TYPO3\CMS\Beuser\Domain\Model\Demand;
19
use TYPO3\CMS\Core\Database\ConnectionPool;
20 21
use TYPO3\CMS\Core\Session\Backend\SessionBackendInterface;
use TYPO3\CMS\Core\Session\SessionManager;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Core\Utility\MathUtility;
24 25 26
use TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository;
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
27

28
/**
29
 * Repository for \TYPO3\CMS\Beuser\Domain\Model\BackendUser
30
 * @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API.
31
 */
32
class BackendUserRepository extends BackendUserGroupRepository
33 34 35 36 37
{
    /**
     * Finds Backend Users on a given list of uids
     *
     * @param array $uidList
38
     * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
39 40 41 42
     */
    public function findByUidList(array $uidList)
    {
        $query = $this->createQuery();
43 44 45 46
        $query->matching($query->in('uid', array_map('intval', $uidList)));
        /** @var QueryResult $result */
        $result = $query->execute();
        return $result;
47
    }
48

49 50 51
    /**
     * Find Backend Users matching to Demand object properties
     *
52
     * @param Demand $demand
53
     * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
54
     */
55
    public function findDemanded(Demand $demand)
56
    {
57
        $constraints = [];
58
        $query = $this->createQuery();
59
        $query->setOrderings(['userName' => QueryInterface::ORDER_ASCENDING]);
60 61
        // Username
        if ($demand->getUserName() !== '') {
62
            $searchConstraints = [];
63
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
64
            foreach (['userName', 'realName'] as $field) {
65
                $searchConstraints[] = $query->like(
66 67
                    $field,
                    '%' . $queryBuilder->escapeLikeWildcards($demand->getUserName()) . '%'
68 69
                );
            }
70 71 72
            if (MathUtility::canBeInterpretedAsInteger($demand->getUserName())) {
                $searchConstraints[] = $query->equals('uid', (int)$demand->getUserName());
            }
73
            $constraints[] = $query->logicalOr($searchConstraints);
74 75
        }
        // Only display admin users
76
        if ($demand->getUserType() == Demand::USERTYPE_ADMINONLY) {
77 78 79
            $constraints[] = $query->equals('admin', 1);
        }
        // Only display non-admin users
80
        if ($demand->getUserType() == Demand::USERTYPE_USERONLY) {
81 82 83
            $constraints[] = $query->equals('admin', 0);
        }
        // Only display active users
84
        if ($demand->getStatus() == Demand::STATUS_ACTIVE) {
85 86 87
            $constraints[] = $query->equals('disable', 0);
        }
        // Only display in-active users
88
        if ($demand->getStatus() == Demand::STATUS_INACTIVE) {
89 90 91
            $constraints[] = $query->logicalOr($query->equals('disable', 1));
        }
        // Not logged in before
92
        if ($demand->getLogins() == Demand::LOGIN_NONE) {
93 94 95
            $constraints[] = $query->equals('lastlogin', 0);
        }
        // At least one login
96
        if ($demand->getLogins() == Demand::LOGIN_SOME) {
97 98 99 100 101
            $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
        }
        // In backend user group
        // @TODO: Refactor for real n:m relations
        if ($demand->getBackendUserGroup()) {
102
            $constraints[] = $query->logicalOr([
103 104 105 106
                $query->equals('usergroup', (int)$demand->getBackendUserGroup()),
                $query->like('usergroup', (int)$demand->getBackendUserGroup() . ',%'),
                $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()),
                $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup() . ',%')
107
            ]);
108
        }
109 110 111
        if ($constraints !== []) {
            $query->matching($query->logicalAnd($constraints));
        }
112 113 114
        /** @var QueryResult $result */
        $result = $query->execute();
        return $result;
115
    }
116

117 118 119
    /**
     * Find Backend Users currently online
     *
120
     * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
121 122 123
     */
    public function findOnline()
    {
124
        $uids = [];
125 126 127 128
        foreach ($this->getSessionBackend()->getAll() as $sessionRecord) {
            if (isset($sessionRecord['ses_userid']) && !in_array($sessionRecord['ses_userid'], $uids, true)) {
                $uids[] = $sessionRecord['ses_userid'];
            }
129
        }
130

131 132
        $query = $this->createQuery();
        $query->matching($query->in('uid', $uids));
133 134 135
        /** @var QueryResult $result */
        $result = $query->execute();
        return $result;
136
    }
137

138 139 140
    /**
     * Overwrite createQuery to don't respect enable fields
     *
141
     * @return QueryInterface
142 143 144 145 146 147 148
     */
    public function createQuery()
    {
        $query = parent::createQuery();
        $query->getQuerySettings()->setIgnoreEnableFields(true);
        return $query;
    }
149 150 151 152

    /**
     * @return SessionBackendInterface
     */
153
    protected function getSessionBackend(): SessionBackendInterface
154
    {
155
        return GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend('BE');
156
    }
157
}