Passed
Push — master ( 282de4...7cf3da )
by
unknown
13:28
created

BackendUserRepository::getBackendUserAuthentication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
16
namespace TYPO3\CMS\Beuser\Domain\Repository;
17
18
use TYPO3\CMS\Beuser\Domain\Model\Demand;
19
use TYPO3\CMS\Core\Database\ConnectionPool;
20
use TYPO3\CMS\Core\Session\Backend\SessionBackendInterface;
21
use TYPO3\CMS\Core\Session\SessionManager;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Core\Utility\MathUtility;
24
use TYPO3\CMS\Extbase\Domain\Repository\BackendUserGroupRepository;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TYPO3\CMS\Beuser\Domain\...kendUserGroupRepository. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
25
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
26
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
     * Finds Backend Users on a given list of uids
36
     *
37
     * @param array $uidList
38
     * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
39
     */
40
    public function findByUidList(array $uidList)
41
    {
42
        $query = $this->createQuery();
43
        $query->matching($query->in('uid', array_map('intval', $uidList)));
44
        /** @var QueryResult $result */
45
        $result = $query->execute();
46
        return $result;
47
    }
48
49
    /**
50
     * Find Backend Users matching to Demand object properties
51
     *
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
        // Find invisible as well, but not deleted
60
        $constraints[] = $query->equals('deleted', 0);
61
        $query->setOrderings(['userName' => QueryInterface::ORDER_ASCENDING]);
62
        // Username
63
        if ($demand->getUserName() !== '') {
64
            $searchConstraints = [];
65
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
66
            foreach (['userName', 'realName'] as $field) {
67
                $searchConstraints[] = $query->like(
68
                    $field,
69
                    '%' . $queryBuilder->escapeLikeWildcards($demand->getUserName()) . '%'
70
                );
71
            }
72
            if (MathUtility::canBeInterpretedAsInteger($demand->getUserName())) {
73
                $searchConstraints[] = $query->equals('uid', (int)$demand->getUserName());
74
            }
75
            $constraints[] = $query->logicalOr($searchConstraints);
76
        }
77
        // Only display admin users
78
        if ($demand->getUserType() == Demand::USERTYPE_ADMINONLY) {
79
            $constraints[] = $query->equals('admin', 1);
80
        }
81
        // Only display non-admin users
82
        if ($demand->getUserType() == Demand::USERTYPE_USERONLY) {
83
            $constraints[] = $query->equals('admin', 0);
84
        }
85
        // Only display active users
86
        if ($demand->getStatus() == Demand::STATUS_ACTIVE) {
87
            $constraints[] = $query->equals('disable', 0);
88
        }
89
        // Only display in-active users
90
        if ($demand->getStatus() == Demand::STATUS_INACTIVE) {
91
            $constraints[] = $query->logicalOr($query->equals('disable', 1));
92
        }
93
        // Not logged in before
94
        if ($demand->getLogins() == Demand::LOGIN_NONE) {
95
            $constraints[] = $query->equals('lastlogin', 0);
96
        }
97
        // At least one login
98
        if ($demand->getLogins() == Demand::LOGIN_SOME) {
99
            $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
100
        }
101
        // In backend user group
102
        // @TODO: Refactor for real n:m relations
103
        if ($demand->getBackendUserGroup()) {
104
            $constraints[] = $query->logicalOr([
105
                $query->equals('usergroup', (int)$demand->getBackendUserGroup()),
106
                $query->like('usergroup', (int)$demand->getBackendUserGroup() . ',%'),
107
                $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()),
108
                $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup() . ',%')
109
            ]);
110
        }
111
        $query->matching($query->logicalAnd($constraints));
112
        /** @var QueryResult $result */
113
        $result = $query->execute();
114
        return $result;
115
    }
116
117
    /**
118
     * Find Backend Users currently online
119
     *
120
     * @return \TYPO3\CMS\Extbase\Persistence\Generic\QueryResult
121
     */
122
    public function findOnline()
123
    {
124
        $uids = [];
125
        foreach ($this->getSessionBackend()->getAll() as $sessionRecord) {
126
            if (isset($sessionRecord['ses_userid']) && !in_array($sessionRecord['ses_userid'], $uids, true)) {
127
                $uids[] = $sessionRecord['ses_userid'];
128
            }
129
        }
130
131
        $query = $this->createQuery();
132
        $query->matching($query->in('uid', $uids));
133
        /** @var QueryResult $result */
134
        $result = $query->execute();
135
        return $result;
136
    }
137
138
    /**
139
     * Overwrite createQuery to don't respect enable fields
140
     *
141
     * @return QueryInterface
142
     */
143
    public function createQuery()
144
    {
145
        $query = parent::createQuery();
146
        $query->getQuerySettings()->setIgnoreEnableFields(true);
147
        $query->getQuerySettings()->setIncludeDeleted(true);
148
        return $query;
149
    }
150
151
    /**
152
     * @return SessionBackendInterface
153
     */
154
    protected function getSessionBackend(): SessionBackendInterface
155
    {
156
        return GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend('BE');
157
    }
158
}
159