Completed
Pull Request — master (#11)
by
unknown
08:38 queued 07:05
created

PostUserLookUp::hasFeUserLoggedIn()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 8
cp 0
cc 5
eloc 7
nc 2
nop 1
crap 30
1
<?php
2
namespace Aoe\FeloginBruteforceProtection\Hooks\UserAuth;
3
4
/***************************************************************
5
 * Copyright notice
6
 *
7
 * (c) 2015 AOE GmbH, <[email protected]>
8
 * (c) 2013 Kevin Schu <[email protected]>, AOE GmbH
9
 * (c) 2014 André Wuttig <[email protected]>, portrino GmbH
10
 *
11
 * All rights reserved
12
 *
13
 * This script is part of the TYPO3 project. The TYPO3 project is
14
 * free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 3 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * The GNU General Public License can be found at
20
 * http://www.gnu.org/copyleft/gpl.html.
21
 *
22
 * This script is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * This copyright notice MUST APPEAR in all copies of the script!
28
 ***************************************************************/
29
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
32
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
33
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
34
use \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
35
use Aoe\FeloginBruteforceProtection\System\Configuration;
36
use Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionService;
37
use Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionIdentifierFactory;
38
use Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionIdentifierInterface;
39
40
/**
41
 * @package Aoe\FeloginBruteforceProtection\\Hook\UserAuth
42
 *
43
 * @author Kevin Schu <[email protected]>
44
 * @author Timo Fuchs <[email protected]>
45
 * @author Andre Wuttig <[email protected]>
46
 * @author Stefan Masztalerz <[email protected]>
47
 */
48
class PostUserLookUp
49
{
50
    /**
51
     * @var ObjectManagerInterface
52
     */
53
    protected $objectManager;
54
55
    /**
56
     * @var Configuration
57
     */
58
    protected $configuration;
59
60
    /**
61
     * @var RestrictionService
62
     */
63
    protected $restrictionService;
64
65
    /**
66
     * @var RestrictionIdentifierInterface
67
     */
68
    protected $restrictionIdentifier;
69
70
    /**
71
     * @var FrontendUserAuthentication
72
     */
73
    protected $frontendUserAuthentication;
74
75
    /**
76
     * @param array $params
77
     * @return void
78
     */
79
    public function handlePostUserLookUp(&$params)
80
    {
81
        /** @var \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication $frontendUserAuthentication */
82
        $frontendUserAuthentication = $params['pObj'];
83
84
        // Continue only if the user is in front-end
85
        if (false === $this->isUserInFrontEnd($frontendUserAuthentication)) {
86
            return;
87
        } else {
88
            $this->setFrontendUserAuthentication($frontendUserAuthentication);
89
        }
90
91
        // Continue only if the protection is enabled
92
        if ($this->getConfiguration()->isEnabled()) {
93
            /**
94
             * @var RestrictionIdentifierFactory $restrictionIdentifierFactory
95
             */
96
            $restrictionIdentifierFactory = $this->getRestrictionIdentifierFactory();
97
            $this->restrictionIdentifier = $restrictionIdentifierFactory->getRestrictionIdentifier(
98
                $this->getConfiguration(),
99
                $frontendUserAuthentication
100
            );
101
            $this->restrictionService = $this->initRestrictionService();
102
103
            if ($this->restrictionIdentifier->checkPreconditions()) {
104
                if ($this->hasFeUserLoggedIn($this->getFrontendUserAuthentication())) {
105
                    $this->getRestrictionService()->removeEntry();
106
                } elseif ($this->hasFeUserLogInFailed($this->getFrontendUserAuthentication())) {
107
                    $this->getRestrictionService()->checkAndHandleRestriction();
108
                    $this->updateGlobals($this->getFrontendUserAuthentication());
0 ignored issues
show
Bug introduced by
$this->getFrontendUserAuthentication() cannot be passed to updateglobals() as the parameter $userAuthObject expects a reference.
Loading history...
109
                }
110
            }
111
        }
112
    }
113
114
    /**
115
     * Check if the user is in front end
116
     *
117
     * @param AbstractUserAuthentication $userAuthentication
118
     * @return boolean
119
     */
120
    private function isUserInFrontEnd(AbstractUserAuthentication $userAuthentication)
121
    {
122
        return $userAuthentication instanceof FrontendUserAuthentication;
123
    }
124
125
    /**
126
     * @param $userAuthObject
127
     * @return boolean
128
     */
129
    private function updateGlobals(&$userAuthObject)
130
    {
131
        $GLOBALS ['felogin_bruteforce_protection'] ['restricted'] =
132
            false;
133
        if ($this->getRestrictionService()->isClientRestricted()) {
134
            $userAuthObject->loginFailure = 1;
135
            $GLOBALS ['felogin_bruteforce_protection'] ['restricted'] =
136
                true;
137
            $GLOBALS ['felogin_bruteforce_protection'] ['restriction_time'] =
138
                $this->getConfiguration()->getRestrictionTime();
139
            $GLOBALS ['felogin_bruteforce_protection'] ['restriction_message'] =
140
                $this->getRestrictionMessage();
141
            return false;
142
        }
143
        return true;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    private function getRestrictionMessage()
150
    {
151
        $time = (integer)($this->getConfiguration()->getRestrictionTime() / 60);
152
        return LocalizationUtility::translate(
153
            'restriction_message',
154
            'felogin_bruteforce_protection',
155
            [$time, $time]
156
        );
157
    }
158
159
    /**
160
     * @param AbstractUserAuthentication $userAuthObject
161
     * @return boolean
162
     */
163
    private function hasFeUserLoggedIn(AbstractUserAuthentication $userAuthObject)
164
    {
165
        if ($userAuthObject->loginType === 'FE' &&
166
            $userAuthObject->loginFailure === false &&
167
            is_array($userAuthObject->user) &&
168
            $userAuthObject->loginSessionStarted === true
169
        ) {
170
            return true;
171
        }
172
        return false;
173
    }
174
175
    /**
176
     * @param AbstractUserAuthentication $userAuthObject
177
     * @return boolean
178
     */
179
    private function hasFeUserLogInFailed(AbstractUserAuthentication $userAuthObject)
180
    {
181
        if ($userAuthObject->loginType === 'FE' && $userAuthObject->loginFailure === true && !$userAuthObject->user) {
182
            return true;
183
        }
184
        return false;
185
    }
186
187
    /**
188
     * @return RestrictionService
189
     */
190
    private function getRestrictionService()
191
    {
192
        return $this->restrictionService;
193
    }
194
195
    /**
196
     * @return \Aoe\FeloginBruteforceProtection\System\Configuration
197
     */
198
    protected function getConfiguration()
199
    {
200
        if (false === isset($this->configuration)) {
201
            $this->configuration = $this->getObjectManager()
202
                ->get('Aoe\FeloginBruteforceProtection\System\Configuration');
203
        }
204
        return $this->configuration;
205
    }
206
207
    /**
208
     * @return FrontendUserAuthentication
209
     */
210
    protected function getFrontendUserAuthentication()
211
    {
212
        return $this->frontendUserAuthentication;
213
    }
214
215
    /**
216
     * @param FrontendUserAuthentication $frontendUserAuthentication
217
     */
218
    protected function setFrontendUserAuthentication(FrontendUserAuthentication $frontendUserAuthentication)
219
    {
220
        $this->frontendUserAuthentication = $frontendUserAuthentication;
221
    }
222
223
    /**
224
     * @return RestrictionIdentifierFactory
225
     */
226
    protected function getRestrictionIdentifierFactory()
227
    {
228
        return $this->getObjectManager()
229
            ->get(
230
                'Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionIdentifierFactory'
231
            );
232
    }
233
234
    /**
235
     * @return RestrictionService
236
     */
237
    protected function initRestrictionService()
238
    {
239
        return $this->getObjectManager()
240
            ->get(
241
                'Aoe\FeloginBruteforceProtection\Domain\Service\RestrictionService',
242
                $this->restrictionIdentifier
0 ignored issues
show
Unused Code introduced by
The call to ObjectManagerInterface::get() has too many arguments starting with $this->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...
243
            );
244
    }
245
246
    /**
247
     * @return ObjectManagerInterface
248
     */
249
    private function getObjectManager()
250
    {
251
        if (false === isset($this->objectManager)) {
252
            $this->objectManager = GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
253
        }
254
        return $this->objectManager;
255
    }
256
}
257