1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//---------------------------------------------------------------------- |
4
|
|
|
// |
5
|
|
|
// Copyright (C) 2017 Artem Rodygin |
6
|
|
|
// |
7
|
|
|
// You should have received a copy of the MIT License along with |
8
|
|
|
// this file. If not, see <http://opensource.org/licenses/MIT>. |
9
|
|
|
// |
10
|
|
|
//---------------------------------------------------------------------- |
11
|
|
|
|
12
|
|
|
namespace Pignus\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait for "lock account" feature. |
18
|
|
|
*/ |
19
|
|
|
trait LockAccountTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int Number of consecutive unsuccessful attempts to authenticate. |
23
|
|
|
* |
24
|
|
|
* @ORM\Column(name="auth_failures", type="integer", nullable=true) |
25
|
|
|
*/ |
26
|
|
|
protected $authFailures; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int Unix Epoch timestamp which the account is locked till. |
30
|
|
|
* When zero, the account is considered as locked for permanent. |
31
|
|
|
* |
32
|
|
|
* @ORM\Column(name="locked_until", type="integer", nullable=true) |
33
|
|
|
*/ |
34
|
|
|
protected $lockedUntil; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Increases number of authentication failures. |
38
|
|
|
* |
39
|
|
|
* @return int|null New authentication failures number. |
40
|
|
|
*/ |
41
|
3 |
|
public function incAuthFailures() |
42
|
|
|
{ |
43
|
3 |
|
return $this->canAccountBeLocked() ? ++$this->authFailures : null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Locks the account until specified moment of time (NULL for permanent lock). |
48
|
|
|
* |
49
|
|
|
* @param \DateTime $time |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
4 |
|
public function lockAccount(\DateTime $time = null) |
54
|
|
|
{ |
55
|
4 |
|
if ($this->canAccountBeLocked()) { |
56
|
4 |
|
$this->authFailures = null; |
57
|
4 |
|
$this->lockedUntil = $time === null ? 0 : $time->getTimestamp(); |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Unlocks the account. |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
2 |
|
public function unlockAccount() |
69
|
|
|
{ |
70
|
2 |
|
$this->authFailures = null; |
71
|
2 |
|
$this->lockedUntil = null; |
72
|
|
|
|
73
|
2 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Specifies whether the "lock account" feature is available for this user. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
7 |
|
protected function canAccountBeLocked(): bool |
82
|
|
|
{ |
83
|
7 |
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|