Completed
Push — master ( 7a29a1...33d37f )
by Thomas
01:49
created

Hooks   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A register() 0 14 1
A failedLoginCallback() 0 4 1
A postLoginCallback() 0 3 1
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 OCP\IUser;
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
	/** @var EventDispatcherInterface */
49
	private $dispatcher;
50
51
    /**
52
     * @param IUserManager $userManager
53
     * @param Throttle $throttle
54
     * @param IRequest $request
55
	 * @param PasswordValidator $passValidator
56
	 * @param EventDispatcherInterface $dispatcher
57
     */
58
    public function __construct($userManager, $throttle, $request, $passValidator, $dispatcher){
59
        $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...
60
        $this->throttle = $throttle;
61
        $this->request = $request;
62
        $this->passValidator = $passValidator;
63
        $this->dispatcher = $dispatcher;
64
65
    }
66
67
    public function register() {
68
        $this->userManager->listen('\OC\User', 'failedLogin', function($uid) {
69
            $this->failedLoginCallback($uid);
70
        });
71
72
        $this->userManager->listen('\OC\User', 'postLogin', function($user) {
73
            $this->postLoginCallback($user);
74
        });
75
76
		$this->dispatcher->addListener('OCP\User::validatePassword', function(GenericEvent $event) {
77
			$this->passValidator->validate($event->getArgument('password'));
78
		});
79
80
    }
81
82
    /**
83
     * @param string $uid
84
     * @param Throttle $throttle
0 ignored issues
show
Bug introduced by
There is no parameter named $throttle. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
85
     */
86
    public function failedLoginCallback($uid) {
87
        $this->throttle->addFailedLoginAttempt($uid, $this->request->getRemoteAddress());
88
        $this->throttle->putDelay($uid, $this->request->getRemoteAddress());
89
    }
90
91
    /**
92
     * @param IUser $user
93
     */
94
    public function postLoginCallback($user) {
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
        $this->throttle->clearSuspiciousAttemptsForIp($this->request->getRemoteAddress());
96
    }
97
}
98