1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\Rest\Entity; |
3
|
|
|
|
4
|
|
|
use Doctrine\ORM\Mapping as ORM; |
5
|
|
|
use Shlinkio\Shlink\Common\Entity\AbstractEntity; |
6
|
|
|
use Shlinkio\Shlink\Common\Util\StringUtilsTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ApiKey |
10
|
|
|
* @author Shlink |
11
|
|
|
* @link http://shlink.io |
12
|
|
|
* |
13
|
|
|
* @ORM\Entity() |
14
|
|
|
* @ORM\Table(name="api_keys") |
15
|
|
|
*/ |
16
|
|
|
class ApiKey extends AbstractEntity |
17
|
|
|
{ |
18
|
|
|
use StringUtilsTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
* @ORM\Column(name="`key`", nullable=false, unique=true) |
23
|
|
|
*/ |
24
|
|
|
protected $key; |
25
|
|
|
/** |
26
|
|
|
* @var \DateTime |
27
|
|
|
* @ORM\Column(name="expiration_date", nullable=true, type="datetime") |
28
|
|
|
*/ |
29
|
|
|
protected $expirationDate; |
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
* @ORM\Column(type="boolean") |
33
|
|
|
*/ |
34
|
|
|
protected $enabled; |
35
|
|
|
|
36
|
11 |
|
public function __construct() |
37
|
|
|
{ |
38
|
11 |
|
$this->enabled = true; |
39
|
11 |
|
$this->key = $this->generateV4Uuid(); |
40
|
11 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
2 |
|
public function getKey() |
46
|
|
|
{ |
47
|
2 |
|
return $this->key; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $key |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function setKey($key) |
55
|
|
|
{ |
56
|
|
|
$this->key = $key; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return \DateTime |
62
|
|
|
*/ |
63
|
4 |
|
public function getExpirationDate() |
64
|
|
|
{ |
65
|
4 |
|
return $this->expirationDate; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param \DateTime $expirationDate |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
2 |
|
public function setExpirationDate($expirationDate) |
73
|
|
|
{ |
74
|
2 |
|
$this->expirationDate = $expirationDate; |
75
|
2 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
3 |
|
public function isExpired() |
82
|
|
|
{ |
83
|
3 |
|
if (! isset($this->expirationDate)) { |
84
|
2 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $this->expirationDate < new \DateTime(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return boolean |
92
|
|
|
*/ |
93
|
7 |
|
public function isEnabled() |
94
|
|
|
{ |
95
|
7 |
|
return $this->enabled; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param boolean $enabled |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
3 |
|
public function setEnabled($enabled) |
103
|
|
|
{ |
104
|
3 |
|
$this->enabled = $enabled; |
105
|
3 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Disables this API key |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
2 |
|
public function disable() |
114
|
|
|
{ |
115
|
2 |
|
return $this->setEnabled(false); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Tells if this api key is enabled and not expired |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
5 |
|
public function isValid() |
124
|
|
|
{ |
125
|
5 |
|
return $this->isEnabled() && ! $this->isExpired(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* The string repesentation of an API key is the key itself |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function __toString() |
134
|
|
|
{ |
135
|
|
|
return $this->getKey(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|