Hooks::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
4
 *
5
 * @author Semih Serhat Karakaya
6
 * @copyright Copyright (c) 2016, ITU IT HEAD OFFICE.
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Security;
24
25
use OC\User\LoginException;
26
use OCP\IUserManager;
27
use OCP\IRequest;
28
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
29
use Symfony\Component\EventDispatcher\GenericEvent;
30
31
/**
32
 * Class Hooks
33
 * @package OCA\Secuity\Lib
34
 */
35
class Hooks {
36
37
    /** @var \OC\User\Manager */
38
    private $userManager;
39
40
    /** @var Throttle*/
41
    private $throttle;
42
43
    /** @var IRequest*/
44
    private $request;
45
46
    /** @var PasswordValidator */
47
    private $passValidator;
48
49
    /** @var EventDispatcherInterface */
50
    private $dispatcher;
51
52
    /**
53
     * @param IUserManager $userManager
54
     * @param Throttle $throttle
55
     * @param IRequest $request
56
     * @param PasswordValidator $passValidator
57
     * @param EventDispatcherInterface $dispatcher
58
     */
59
    public function __construct($userManager, $throttle, $request, $passValidator, $dispatcher){
60
        $this->userManager = $userManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $userManager of type object<OCP\IUserManager> is incompatible with the declared type object<OC\User\Manager> of property $userManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
        $this->throttle = $throttle;
62
        $this->request = $request;
63
        $this->passValidator = $passValidator;
64
        $this->dispatcher = $dispatcher;
65
66
    }
67
68
    public function register() {
69
        $this->userManager->listen('\OC\User', 'preLogin', function($uid) {
70
            $this->preLoginCallback($uid);
71
        });
72
73
        $this->userManager->listen('\OC\User', 'failedLogin', function($uid) {
74
            $this->failedLoginCallback($uid);
75
        });
76
        
77
        $this->userManager->listen('\OC\User', 'postLogin', function($user) {
78
            /** @var $user \OC\User\User */
79
            $this->postLoginCallback($user->getUID());
80
        });
81
82
        $this->dispatcher->addListener('OCP\User::validatePassword', function(GenericEvent $event) {
83
            $this->passValidator->validate($event->getArgument('password'));
84
        });
85
86
    }
87
88
    /**
89
     * @param string $uid
90
     */
91
    public function failedLoginCallback($uid) {
92
        $this->throttle->addFailedLoginAttempt($uid, $this->request->getRemoteAddress());
93
    }
94
95
    /**
96
     * @param string $uid
97
     */
98
    public function postLoginCallback($uid) {
99
        $this->throttle->clearSuspiciousAttemptsForUidIpCombination($uid, $this->request->getRemoteAddress());
100
    }
101
102
    /**
103
     * @param string $uid
104
     * @throws LoginException
105
     */
106
    public function preLoginCallback($uid) {
107
        $this->throttle->applyBruteForcePolicy($uid, $this->request->getRemoteAddress());
108
    }
109
}
110