1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron\records; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\helpers\DateTimeHelper; |
13
|
|
|
use craft\validators\DateTimeValidator; |
14
|
|
|
use DateTime; |
15
|
|
|
use flipbox\ember\helpers\ModelHelper; |
16
|
|
|
use flipbox\ember\helpers\ObjectHelper; |
17
|
|
|
use flipbox\ember\helpers\QueryHelper; |
18
|
|
|
use flipbox\ember\records\ActiveRecordWithId; |
19
|
|
|
use flipbox\ember\records\traits\StateAttribute; |
20
|
|
|
use flipbox\patron\db\TokenActiveQuery; |
21
|
|
|
use flipbox\patron\Patron; |
22
|
|
|
use yii\db\ActiveQueryInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Flipbox Factory <[email protected]> |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
* |
28
|
|
|
* @property string $accessToken |
29
|
|
|
* @property string $refreshToken |
30
|
|
|
* @property DateTime|null $dateExpires |
31
|
|
|
* @property array $values |
32
|
|
|
* @property TokenEnvironment[] $environments |
33
|
|
|
*/ |
34
|
|
|
class Token extends ActiveRecordWithId |
35
|
|
|
{ |
36
|
|
|
use StateAttribute; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The table alias |
40
|
|
|
*/ |
41
|
|
|
const TABLE_ALIAS = 'patron_tokens'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
* @return TokenActiveQuery |
46
|
|
|
* @throws \yii\base\InvalidConfigException |
47
|
|
|
*/ |
48
|
|
|
public static function find() |
49
|
|
|
{ |
50
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
51
|
|
|
return Craft::createObject(TokenActiveQuery::class, [get_called_class()]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function isActive(): bool |
58
|
|
|
{ |
59
|
|
|
return $this->isEnabled() && !$this->hasExpired(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
public function hasExpired(): bool |
66
|
|
|
{ |
67
|
|
|
$dateExpires = $this->dateExpires ?: new DateTime('now'); |
68
|
|
|
return DateTimeHelper::isInThePast($dateExpires); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/******************************************* |
72
|
|
|
* EVENTS |
73
|
|
|
*******************************************/ |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
* @throws \Throwable |
78
|
|
|
*/ |
79
|
|
|
public function afterSave($insert, $changedAttributes) |
80
|
|
|
{ |
81
|
|
|
Patron::getInstance()->manageTokens()->saveEnvironments($this); |
82
|
|
|
parent::afterSave($insert, $changedAttributes); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function rules() |
90
|
|
|
{ |
91
|
|
|
return array_merge( |
92
|
|
|
parent::rules(), |
93
|
|
|
$this->stateRules(), |
94
|
|
|
[ |
95
|
|
|
[ |
96
|
|
|
[ |
97
|
|
|
'accessToken', |
98
|
|
|
'refreshToken' |
99
|
|
|
], |
100
|
|
|
'unique' |
101
|
|
|
], |
102
|
|
|
[ |
103
|
|
|
[ |
104
|
|
|
'dateExpires' |
105
|
|
|
], |
106
|
|
|
DateTimeValidator::class |
107
|
|
|
], |
108
|
|
|
[ |
109
|
|
|
[ |
110
|
|
|
'providerId', |
111
|
|
|
'accessToken' |
112
|
|
|
], |
113
|
|
|
'required' |
114
|
|
|
], |
115
|
|
|
[ |
116
|
|
|
[ |
117
|
|
|
'providerId' |
118
|
|
|
], |
119
|
|
|
'number', |
120
|
|
|
'integerOnly' => true |
121
|
|
|
], |
122
|
|
|
[ |
123
|
|
|
[ |
124
|
|
|
'providerId', |
125
|
|
|
'accessToken', |
126
|
|
|
'values', |
127
|
|
|
'dateExpires' |
128
|
|
|
], |
129
|
|
|
'safe', |
130
|
|
|
'on' => [ |
131
|
|
|
ModelHelper::SCENARIO_DEFAULT |
132
|
|
|
] |
133
|
|
|
] |
134
|
|
|
] |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the associated Authorization |
140
|
|
|
* |
141
|
|
|
* @param array $config |
142
|
|
|
* @return ActiveQueryInterface |
143
|
|
|
*/ |
144
|
|
|
public function getProvider(array $config = []) |
145
|
|
|
{ |
146
|
|
|
$query = $this->hasOne( |
147
|
|
|
Provider::class, |
148
|
|
|
['providerId' => 'id'] |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
if (!empty($config)) { |
152
|
|
|
QueryHelper::configure( |
153
|
|
|
$query, |
154
|
|
|
$config |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $query; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get all of the associated environments. |
163
|
|
|
* |
164
|
|
|
* @param array $config |
165
|
|
|
* @return \yii\db\ActiveQuery |
166
|
|
|
*/ |
167
|
|
|
public function getEnvironments(array $config = []) |
168
|
|
|
{ |
169
|
|
|
$query = $this->hasMany( |
170
|
|
|
TokenEnvironment::class, |
171
|
|
|
['tokenId' => 'id'] |
172
|
|
|
)->indexBy('environment'); |
173
|
|
|
|
174
|
|
|
if (!empty($config)) { |
175
|
|
|
QueryHelper::configure( |
176
|
|
|
$query, |
177
|
|
|
$config |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $query; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $environments |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function setEnvironments(array $environments = []) |
189
|
|
|
{ |
190
|
|
|
$records = []; |
191
|
|
|
foreach (array_filter($environments) as $key => $environment) { |
192
|
|
|
$records[] = $this->resolveEnvironment($key, $environment); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->populateRelation('environments', $records); |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $key |
201
|
|
|
* @param $environment |
202
|
|
|
* @return TokenEnvironment |
203
|
|
|
*/ |
204
|
|
|
protected function resolveEnvironment(string $key, $environment): TokenEnvironment |
205
|
|
|
{ |
206
|
|
|
if (!$record = $this->environments[$key] ?? null) { |
207
|
|
|
$record = new TokenEnvironment(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if (!is_array($environment)) { |
211
|
|
|
$environment = ['environment' => $environment]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
215
|
|
|
return ObjectHelper::populate( |
216
|
|
|
$record, |
217
|
|
|
$environment |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|