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

DbService::getSuspiciousActivityCountForIp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
/**
3
4
 *
5
 * @author Semih Serhat Karakaya
6
 * @copyright Copyright (c) 2017, ownCloud GmbH
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\Db;
24
25
use OC\AppFramework\Utility\TimeFactory;
26
use OCP\IDBConnection;
27
28
/**
29
 * Class DBService
30
 * @package OCA\Security\Db
31
 */
32
class DbService {
33
34
    /**
35
     * @var IDBConnection
36
     */
37
    private $connection;
38
39
    /**
40
     * @var TimeFactory
41
     */
42
    private $factory;
43
44
    /**
45
     * DBService constructor.
46
     *
47
     * @param IDBConnection $connection
48
     */
49
    public function __construct(IDBConnection $connection, TimeFactory $factory) {
50
        $this->connection = $connection;
51
        $this->factory = $factory;
52
    }
53
54
    /**
55
     * @param string $uid
56
     */
57
    public function addFailedLoginAttempt($uid, $ip){
58
        $builder = $this->connection->getQueryBuilder();
59
        $builder->insert('failed_login_attempts')
60
            ->setValue('ip', $builder->createNamedParameter($ip))
61
            ->setValue('uid', $builder->createNamedParameter($uid))
62
            ->setValue('attempted_at', $builder->createNamedParameter($this->factory->getTime()))
63
            ->execute();
64
    }
65
66
    /**
67
     * @param string $uid
68
     * @return int
69
     */
70 View Code Duplication
    public function getSuspiciousActivityCountForUid($uid) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
        $builder = $this->connection->getQueryBuilder();
72
        $thresholdTime = (new \DateTime())->modify("-86400 second")->getTimestamp();
73
        $attempts = $builder->select('*')
74
            ->from('failed_login_attempts')
75
            ->where($builder->expr()->gt('attempted_at', $builder->createNamedParameter($thresholdTime)))
76
            ->andWhere($builder->expr()->eq('uid', $builder->createNamedParameter($uid)))
77
            ->execute()
78
            ->fetchAll();
79
        return count($attempts);
80
    }
81
82
    /**
83
     * @param string $ip
84
     * @return int
85
     */
86 View Code Duplication
    public function getSuspiciousActivityCountForIp($ip) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
        $builder = $this->connection->getQueryBuilder();
88
        $thresholdTime = (new \DateTime())->modify("-86400 second")->getTimestamp();
89
        $attempts = $builder->select('*')
90
            ->from('failed_login_attempts')
91
            ->where($builder->expr()->gt('attempted_at', $builder->createNamedParameter($thresholdTime)))
92
            ->andWhere($builder->expr()->eq('ip', $builder->createNamedParameter($ip)))
93
            ->execute()
94
            ->fetchAll();
95
        return count($attempts);
96
    }
97
98
    public function deleteSuspiciousAttemptsForIp($ip) {
99
        $builder = $this->connection->getQueryBuilder();
100
        $builder->delete('failed_login_attempts')
101
            ->where($builder->expr()->eq('ip',$builder->createNamedParameter($ip)))
102
            ->execute();
103
    }
104
}