Completed
Pull Request — master (#11)
by
unknown
23:27
created

AuthUser::getRestrictionIdentifierFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace Aoe\FeloginBruteforceProtection\Service;
3
4
    /***************************************************************
5
     *  Copyright notice
6
     *
7
     *  (c) 2013 Kevin Schu <[email protected]>, AOE GmbH
8
     *  (c) 2014 André Wuttig <[email protected]>, portrino GmbH
9
     *
10
     *  All rights reserved
11
     *
12
     *  This script is part of the TYPO3 project. The TYPO3 project is
13
     *  free software; you can redistribute it and/or modify
14
     *  it under the terms of the GNU General Public License as published by
15
     *  the Free Software Foundation; either version 3 of the License, or
16
     *  (at your option) any later version.
17
     *
18
     *  The GNU General Public License can be found at
19
     *  http://www.gnu.org/copyleft/gpl.html.
20
     *
21
     *  This script is distributed in the hope that it will be useful,
22
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
     *  GNU General Public License for more details.
25
     *
26
     *  This copyright notice MUST APPEAR in all copies of the script!
27
     ***************************************************************/
28
29
use Aoe\FeloginBruteforceProtection\System\Configuration;
30
use Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionService;
31
use Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionIdentifierFactory;
32
use Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionIdentifierInterface;
33
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
36
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
37
use TYPO3\CMS\Sv\AuthenticationService;
38
39
/**
40
 * @package Aoe\FeloginBruteforceProtection\\Service
41
 *
42
 * @author Kevin Schu <[email protected]>
43
 * @author Timo Fuchs <[email protected]>
44
 * @author Andre Wuttig <[email protected]>
45
 *
46
 */
47
class AuthUser extends AuthenticationService
48
{
49
50
    /**
51
     * @var Configuration
52
     */
53
    protected $configuration;
54
55
    /**
56
     * Object manager
57
     *
58
     * @var ObjectManagerInterface
59
     */
60
    protected $objectManager;
61
62
    /**
63
     * @var RestrictionService
64
     */
65
    protected $restrictionService;
66
67
    /**
68
     * @var FrontendUserAuthentication
69
     */
70
    protected $frontendUserAuthentication;
71
72
    /**
73
     * Load extbase dependencies to use repositories and persistence.
74
     *
75
     * @return boolean TRUE if the service is available
76
     */
77
    public function init()
78
    {
79
        ExtensionManagementUtility::loadBaseTca(false);
80
        if (!isset($GLOBALS['TSFE']) || empty($GLOBALS['TSFE']->sys_page)) {
81
            $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
82
        }
83
        if (!isset($GLOBALS['TSFE']) || empty($GLOBALS['TSFE']->tmpl)) {
84
            $GLOBALS['TSFE']->tmpl = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\TemplateService');
85
        }
86
87
        return parent::init();
88
    }
89
90
    /**
91
     * Initialize authentication service
92
     *
93
     * @param string $mode Subtype of the service which is used to call the service.
94
     * @param array $loginData Submitted login form data
95
     * @param array $authInfo Information array. Holds submitted form data etc.
96
     * @param object $pObj Parent object
97
     * @return void
98
     * @todo Define visibility
99
     */
100
    public function initAuth($mode, $loginData, $authInfo, $pObj)
101
    {
102
        $this->frontendUserAuthentication = $pObj;
103
    }
104
105
    /**
106
     * Ensure chain breaking if client is already banned!
107
     * Simulate an invalid user and stop the chain by setting the "fetchAllUsers" configuration to "FALSE";
108
     *
109
     * @return boolean|array
110
     */
111
    public function getUser()
112
    {
113
        if ($this->isProtectionEnabled() && $this->getRestrictionService()->isClientRestricted()) {
114
            $GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth']['setup']
115
            [$this->frontendUserAuthentication->loginType . '_fetchAllUsers'] = false;
116
            return ['uid' => 0];
117
        }
118
        return parent::getUser();
119
    }
120
121
    /**
122
     * Ensure chain breaking if client is already banned!
123
     *
124
     * @param mixed $userData Data of user.
125
     * @return integer     Chain result (<0: break chain; 100: use next chain service; 200: success)
126
     */
127
    public function authUser(array $userData)
128
    {
129
        if ($this->isProtectionEnabled() && $this->getRestrictionService()->isClientRestricted()) {
130
            return -1;
131
        }
132
        return 100;
133
    }
134
135
    /**
136
     * @return boolean
137
     */
138
    public function isProtectionEnabled()
139
    {
140
        return $this->getConfiguration()->isEnabled();
141
    }
142
143
    /**
144
     * @return RestrictionService
145
     */
146
    private function getRestrictionService()
147
    {
148
        if (false === isset($this->restrictionService)) {
149
            /**
150
             * @var RestrictionIdentifierFactory $restrictionIdentifierFactory
151
             */
152
            $restrictionIdentifierFactory = $this->getRestrictionIdentifierFactory();
153
            /**
154
             * @var RestrictionIdentifierInterface $restrictionIdentifier
155
             */
156
            $restrictionIdentifier = $restrictionIdentifierFactory->getRestrictionIdentifier(
157
                $this->getConfiguration(),
158
                $this->frontendUserAuthentication
159
            );
160
161
            $this->restrictionService = $this->getObjectManager()
162
                ->get(
163
                    'Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionService',
164
                    $restrictionIdentifier
0 ignored issues
show
Unused Code introduced by
The call to ObjectManagerInterface::get() has too many arguments starting with $restrictionIdentifier.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
165
                );
166
        }
167
        return $this->restrictionService;
168
    }
169
170
    /**
171
     * @return Configuration
172
     */
173
    protected function getConfiguration()
174
    {
175
        if (false === isset($this->configuration)) {
176
            $this->configuration = $this->getObjectManager()
177
                ->get('Aoe\FeloginBruteforceProtection\System\Configuration');
178
        }
179
        return $this->configuration;
180
    }
181
182
    /**
183
     * @return ObjectManagerInterface
184
     */
185
    private function getObjectManager()
186
    {
187
        if (false === isset($this->objectManager)) {
188
            $this->objectManager = GeneralUtility::makeInstance(
189
                'TYPO3\CMS\Extbase\Object\ObjectManager'
190
            );
191
        }
192
        return $this->objectManager;
193
    }
194
195
    /**
196
     * @return RestrictionIdentifierFactory
197
     */
198
    protected function getRestrictionIdentifierFactory()
199
    {
200
        return $this->getObjectManager()
201
            ->get(
202
                'Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionIdentifierFactory'
203
            );
204
    }
205
}
206