1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* User Locking Model. |
18
|
|
|
*/ |
19
|
|
|
class UserLockingModel extends Model |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get the number of failed login for the user. |
23
|
|
|
* |
24
|
|
|
* @param string $username |
25
|
|
|
* |
26
|
|
|
* @return int |
27
|
|
|
*/ |
28
|
|
|
public function getFailedLogin($username) |
29
|
|
|
{ |
30
|
|
|
return (int) $this->db->table(UserModel::TABLE) |
|
|
|
|
31
|
|
|
->eq('username', $username) |
32
|
|
|
->findOneColumn('nb_failed_login'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Reset to 0 the counter of failed login. |
37
|
|
|
* |
38
|
|
|
* @param string $username |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function resetFailedLogin($username) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
return $this->db->table(UserModel::TABLE) |
|
|
|
|
45
|
|
|
->eq('username', $username) |
46
|
|
|
->update([ |
47
|
|
|
'nb_failed_login' => 0, |
48
|
|
|
'lock_expiration_date' => 0, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Increment failed login counter. |
54
|
|
|
* |
55
|
|
|
* @param string $username |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function incrementFailedLogin($username) |
60
|
|
|
{ |
61
|
|
|
return $this->db->table(UserModel::TABLE) |
|
|
|
|
62
|
|
|
->eq('username', $username) |
63
|
|
|
->increment('nb_failed_login', 1); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check if the account is locked. |
68
|
|
|
* |
69
|
|
|
* @param string $username |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function isLocked($username) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
return $this->db->table(UserModel::TABLE) |
|
|
|
|
76
|
|
|
->eq('username', $username) |
77
|
|
|
->neq('lock_expiration_date', 0) |
78
|
|
|
->gte('lock_expiration_date', time()) |
79
|
|
|
->exists(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Lock the account for the specified duration. |
84
|
|
|
* |
85
|
|
|
* @param string $username Username |
86
|
|
|
* @param int $duration Duration in minutes |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function lock($username, $duration = 15) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
return $this->db->table(UserModel::TABLE) |
|
|
|
|
93
|
|
|
->eq('username', $username) |
94
|
|
|
->update([ |
95
|
|
|
'lock_expiration_date' => time() + $duration * 60, |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return true if the captcha must be shown. |
101
|
|
|
* |
102
|
|
|
* @param string $username |
103
|
|
|
* @param int $tries |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function hasCaptcha($username, $tries = BRUTEFORCE_CAPTCHA) |
108
|
|
|
{ |
109
|
|
|
return $this->getFailedLogin($username) >= $tries; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.