Completed
Push — master ( 74e59d...80234c )
by
unknown
18:35
created

BackendUserRepository::findDemanded()   D

Complexity

Conditions 11
Paths 384

Size

Total Lines 60
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 35
c 1
b 0
f 0
nc 384
nop 1
dl 0
loc 60
rs 4.1833

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Authentication\BackendUserAuthentication;
20
use TYPO3\CMS\Core\Database\ConnectionPool;
21
use TYPO3\CMS\Core\Session\Backend\SessionBackendInterface;
22
use TYPO3\CMS\Core\Session\SessionManager;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
use TYPO3\CMS\Core\Utility\MathUtility;
25
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...
26
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
27
use 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
 */
33
class 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
        // Find invisible as well, but not deleted
61
        $constraints[] = $query->equals('deleted', 0);
62
        $query->setOrderings(['userName' => QueryInterface::ORDER_ASCENDING]);
63
        // Username
64
        if ($demand->getUserName() !== '') {
65
            $searchConstraints = [];
66
            $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
67
            foreach (['userName', 'realName'] as $field) {
68
                $searchConstraints[] = $query->like(
69
                    $field,
70
                    '%' . $queryBuilder->escapeLikeWildcards($demand->getUserName()) . '%'
71
                );
72
            }
73
            if (MathUtility::canBeInterpretedAsInteger($demand->getUserName())) {
74
                $searchConstraints[] = $query->equals('uid', (int)$demand->getUserName());
75
            }
76
            $constraints[] = $query->logicalOr($searchConstraints);
77
        }
78
        // Only display admin users
79
        if ($demand->getUserType() == Demand::USERTYPE_ADMINONLY) {
80
            $constraints[] = $query->equals('admin', 1);
81
        }
82
        // Only display non-admin users
83
        if ($demand->getUserType() == Demand::USERTYPE_USERONLY) {
84
            $constraints[] = $query->equals('admin', 0);
85
        }
86
        // Only display active users
87
        if ($demand->getStatus() == Demand::STATUS_ACTIVE) {
88
            $constraints[] = $query->equals('disable', 0);
89
        }
90
        // Only display in-active users
91
        if ($demand->getStatus() == Demand::STATUS_INACTIVE) {
92
            $constraints[] = $query->logicalOr($query->equals('disable', 1));
93
        }
94
        // Not logged in before
95
        if ($demand->getLogins() == Demand::LOGIN_NONE) {
96
            $constraints[] = $query->equals('lastlogin', 0);
97
        }
98
        // At least one login
99
        if ($demand->getLogins() == Demand::LOGIN_SOME) {
100
            $constraints[] = $query->logicalNot($query->equals('lastlogin', 0));
101
        }
102
        // In backend user group
103
        // @TODO: Refactor for real n:m relations
104
        if ($demand->getBackendUserGroup()) {
105
            $constraints[] = $query->logicalOr([
106
                $query->equals('usergroup', (int)$demand->getBackendUserGroup()),
107
                $query->like('usergroup', (int)$demand->getBackendUserGroup() . ',%'),
108
                $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup()),
109
                $query->like('usergroup', '%,' . (int)$demand->getBackendUserGroup() . ',%')
110
            ]);
111
        }
112
        $query->matching($query->logicalAnd($constraints));
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
        $query->getQuerySettings()->setIncludeDeleted(true);
149
        return $query;
150
    }
151
152
    /**
153
     * @return SessionBackendInterface
154
     */
155
    protected function getSessionBackend()
156
    {
157
        $loginType = $this->getBackendUserAuthentication()->getLoginType();
158
        return GeneralUtility::makeInstance(SessionManager::class)->getSessionBackend($loginType);
159
    }
160
161
    /**
162
     * @return BackendUserAuthentication
163
     */
164
    protected function getBackendUserAuthentication()
165
    {
166
        return $GLOBALS['BE_USER'];
167
    }
168
}
169