FeEntityService   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 169
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A findAllGroupsAuthenticatedByIp() 0 5 1
A findAllUsersAuthenticatedByIp() 0 5 1
A findAllGroupsWithIpAuthentication() 0 5 1
A findAllUsersWithIpAuthentication() 0 5 1
B findEntitiesAuthenticatedByIp() 0 27 6
B findEntitiesWithIpAuthentication() 0 40 6
A getIpService() 0 7 2
A getIpMatchingService() 0 7 2
1
<?php
2
namespace AOE\AoeIpauth\Domain\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2019 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use AOE\AoeIpauth\Utility\EnableFieldsUtility;
29
use TYPO3\CMS\Core\Database\ConnectionPool;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32
/**
33
 * Class FeEntityService
34
 *
35
 * @package AOE\AoeIpauth\Domain\Service
36
 */
37
class FeEntityService implements \TYPO3\CMS\Core\SingletonInterface
38
{
39
40
    const TABLE_GROUP = 'fe_groups';
41
    const TABLE_USER = 'fe_users';
42
43
    /**
44
     * @var \AOE\AoeIpauth\Domain\Service\IpService
45
     */
46
    protected $ipService = null;
47
48
    /**
49
     * @var \AOE\AoeIpauth\Service\IpMatchingService
50
     */
51
    protected $ipMatchingService = null;
52
53
    /**
54
     * Finds all groups that would be authenticated against a certain IP
55
     *
56
     * @param string $ip
57
     * @return array
58
     */
59
    public function findAllGroupsAuthenticatedByIp($ip)
60
    {
61
        $groups = $this->findEntitiesAuthenticatedByIp($ip, self::TABLE_GROUP);
62
        return $groups;
63
    }
64
65
    /**
66
     * Finds all groups that would be authenticated against a certain IP
67
     *
68
     * @param string $ip
69
     * @return array
70
     */
71
    public function findAllUsersAuthenticatedByIp($ip)
72
    {
73
        $groups = $this->findEntitiesAuthenticatedByIp($ip, self::TABLE_USER);
74
        return $groups;
75
    }
76
77
    /**
78
     * Returns all fe_groups with ip authentication enabled
79
     * Convenience method for "findEntitiesWithIpAuthentication"
80
     *
81
     * @return array
82
     */
83
    public function findAllGroupsWithIpAuthentication()
84
    {
85
        $groups = $this->findEntitiesWithIpAuthentication(self::TABLE_GROUP);
86
        return $groups;
87
    }
88
89
    /**
90
     * Returns all fe_users with ip authentication enabled
91
     * Convenience method for "findEntitiesWithIpAuthentication"
92
     *
93
     * @return array
94
     */
95
    public function findAllUsersWithIpAuthentication()
96
    {
97
        $users = $this->findEntitiesWithIpAuthentication(self::TABLE_USER);
98
        return $users;
99
    }
100
101
    /**
102
     * Finds all entities that would be authenticated against a certain IP
103
     *
104
     * @param string $ip
105
     * @param string $table
106
     * @return array
107
     */
108
    protected function findEntitiesAuthenticatedByIp($ip, $table)
109
    {
110
        $authenticatedEntities = array();
111
        $entities = $this->findEntitiesWithIpAuthentication($table);
112
113
        if (empty($entities)) {
114
            return $authenticatedEntities;
115
        }
116
117
        // Walk each group and check if it matches
118
        foreach ($entities as $entity) {
119
            $uid = $entity['uid'];
120
            $ips = $entity['tx_aoeipauth_ip'];
121
            unset($entity['tx_aoeipauth_ip']);
122
123
            $isWhitelisted = false;
124
            while (!$isWhitelisted && !empty($ips)) {
125
                $ipWhitelist = array_pop($ips);
126
                $isWhitelisted = $this->getIpMatchingService()->isIpAllowed($ip, $ipWhitelist);
127
            }
128
129
            if ($isWhitelisted) {
130
                $authenticatedEntities[$uid] = $entity;
131
            }
132
        }
133
        return $authenticatedEntities;
134
    }
135
136
    /**
137
     * Finds entities with IP authentication
138
     *
139
     * @param string $table
140
     * @return array
141
     * @throws \RuntimeException
142
     */
143
    protected function findEntitiesWithIpAuthentication($table)
144
    {
145
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
146
        $queryBuilder->getRestrictions()->removeAll();
147
        $entities = $queryBuilder->select('uid','pid')
148
            ->from($table)
149
            ->where(
150
                $queryBuilder->expr()->gt('tx_aoeipauth_ip', '0' . EnableFieldsUtility::enableFields($table))
151
            )
152
            ->execute()
153
            ->fetchAll();
154
155
        if (empty($entities)) {
156
            return array();
157
        }
158
159
        // Enrich with IPs
160
        $finalEntities = array();
161
162
        foreach ($entities as $entity) {
163
            $uid = $entity['uid'];
164
            if (self::TABLE_GROUP == $table) {
165
                $matchedIps = $this->getIpService()->findIpsByFeGroupId($uid);
166
            } elseif (self::TABLE_USER == $table) {
167
                $matchedIps = $this->getIpService()->findIpsByFeUserId($uid);
168
            } else {
169
                throw new \RuntimeException('Cannot load entries for unknown table.', 1390299890);
170
            }
171
172
            // Skip groups that do not find a corresponding ip
173
            if (empty($matchedIps)) {
174
                continue;
175
            }
176
            // Inject the matched ips to the group
177
            $entity['tx_aoeipauth_ip'] = $matchedIps;
178
            $finalEntities[] = $entity;
179
        }
180
181
        return $finalEntities;
182
    }
183
184
    /**
185
     * @return \AOE\AoeIpauth\Domain\Service\IpService
186
     */
187
    protected function getIpService()
188
    {
189
        if (null === $this->ipService) {
190
            $this->ipService = GeneralUtility::makeInstance('AOE\\AoeIpauth\\Domain\\Service\\IpService');
191
        }
192
        return $this->ipService;
193
    }
194
195
    /**
196
     * @return \AOE\AoeIpauth\Service\IpMatchingService
197
     */
198
    protected function getIpMatchingService()
199
    {
200
        if (null === $this->ipMatchingService) {
201
            $this->ipMatchingService = GeneralUtility::makeInstance('AOE\\AoeIpauth\\Service\\IpMatchingService');
202
        }
203
        return $this->ipMatchingService;
204
    }
205
}
206