1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//---------------------------------------------------------------------- |
4
|
|
|
// |
5
|
|
|
// Copyright (C) 2017-2018 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 LazySec\Entity; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Mapping as ORM; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Trait for "password expiration" feature. |
18
|
|
|
*/ |
19
|
|
|
trait ExpirePasswordTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int Unix Epoch timestamp when the password expires. |
23
|
|
|
* |
24
|
|
|
* @ORM\Column(name="password_expires_at", type="integer", nullable=true) |
25
|
|
|
*/ |
26
|
|
|
protected $passwordExpiresAt; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Checks whether the user's credentials (password) has expired. |
30
|
|
|
* |
31
|
|
|
* Inherited from '\Symfony\Component\Security\Core\User\AdvancedUserInterface'. |
32
|
|
|
* |
33
|
|
|
* @return bool TRUE if the user's credentials are non expired, FALSE otherwise. |
34
|
|
|
*/ |
35
|
4 |
|
public function isCredentialsNonExpired(): bool |
36
|
|
|
{ |
37
|
4 |
|
return $this->canPasswordBeExpired() |
38
|
4 |
|
? $this->passwordExpiresAt === null || $this->passwordExpiresAt > time() |
39
|
4 |
|
: true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Makes password to expire at specified moment of time (NULL for no expiration). |
44
|
|
|
* |
45
|
|
|
* @param \DateTime $time |
46
|
|
|
* |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
2 |
|
public function expirePasswordAt(\DateTime $time = null): self |
50
|
|
|
{ |
51
|
2 |
|
if ($this->canPasswordBeExpired()) { |
52
|
2 |
|
$this->passwordExpiresAt = $time === null ? null : $time->getTimestamp(); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Makes password to expire in specified period of time. |
60
|
|
|
* |
61
|
|
|
* @param \DateInterval $interval |
62
|
|
|
* |
63
|
|
|
* @return self |
64
|
|
|
*/ |
65
|
1 |
|
public function expirePasswordIn(\DateInterval $interval): self |
66
|
|
|
{ |
67
|
1 |
|
if ($this->canPasswordBeExpired()) { |
68
|
1 |
|
$this->passwordExpiresAt = date_create()->add($interval)->getTimestamp(); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Specifies whether the "password expiration" feature is available for this user. |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
4 |
|
protected function canPasswordBeExpired(): bool |
80
|
|
|
{ |
81
|
4 |
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|