1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* JtiService.php |
4
|
|
|
* |
5
|
|
|
* PHP version 5.6+ |
6
|
|
|
* |
7
|
|
|
* @author Philippe Gaultier <[email protected]> |
8
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
9
|
|
|
* @license http://www.sweelix.net/license license |
10
|
|
|
* @version 1.2.0 |
11
|
|
|
* @link http://www.sweelix.net |
12
|
|
|
* @package sweelix\oauth2\server\services\mySql |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace sweelix\oauth2\server\services\mySql; |
16
|
|
|
|
17
|
|
|
use sweelix\oauth2\server\exceptions\DuplicateIndexException; |
18
|
|
|
use sweelix\oauth2\server\exceptions\DuplicateKeyException; |
19
|
|
|
use sweelix\oauth2\server\interfaces\JtiModelInterface; |
20
|
|
|
use sweelix\oauth2\server\interfaces\JtiServiceInterface; |
21
|
|
|
use yii\db\Exception as DatabaseException; |
22
|
|
|
use Yii; |
23
|
|
|
use yii\db\Query; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This is the jti service for mySql |
27
|
|
|
* database structure |
28
|
|
|
* * oauth2:jti:<jid> : hash (Jti) |
29
|
|
|
* |
30
|
|
|
* @author Philippe Gaultier <[email protected]> |
31
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
32
|
|
|
* @license http://www.sweelix.net/license license |
33
|
|
|
* @version 1.2.0 |
34
|
|
|
* @link http://www.sweelix.net |
35
|
|
|
* @package sweelix\oauth2\server\services\mySql |
36
|
|
|
* @since 1.0.0 |
37
|
|
|
*/ |
38
|
|
|
class JtiService extends BaseService implements JtiServiceInterface |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var string sql jtis table |
42
|
|
|
*/ |
43
|
|
|
public $jtisTable = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Save Jti |
47
|
|
|
* @param JtiModelInterface $jti |
48
|
|
|
* @param null|array $attributes attributes to save |
49
|
|
|
* @return bool |
50
|
|
|
* @throws DatabaseException |
51
|
|
|
* @throws DuplicateIndexException |
52
|
|
|
* @throws DuplicateKeyException |
53
|
|
|
* @since 1.0.0 |
54
|
|
|
*/ |
55
|
|
|
protected function insert(JtiModelInterface $jti, $attributes) |
56
|
|
|
{ |
57
|
|
|
$result = false; |
58
|
|
|
if (!$jti->beforeSave(true)) { |
59
|
|
|
return $result; |
60
|
|
|
} |
61
|
|
|
$jtiKey = $jti->getKey(); |
62
|
|
|
$entity = (new Query()) |
63
|
|
|
->select('*') |
64
|
|
|
->from($this->jtisTable) |
65
|
|
|
->where('id = :id', [':id' => $jtiKey]) |
66
|
|
|
->one($this->db); |
67
|
|
|
if ($entity !== false) { |
68
|
|
|
throw new DuplicateKeyException('Duplicate key "' . $jtiKey . '"'); |
69
|
|
|
} |
70
|
|
|
$values = $jti->getDirtyAttributes($attributes); |
|
|
|
|
71
|
|
|
$jtisParameters = []; |
72
|
|
|
$this->setAttributesDefinitions($jti->attributesDefinition()); |
73
|
|
|
foreach ($values as $key => $value) { |
74
|
|
|
if ($value !== null) { |
75
|
|
|
$jtisParameters[$key] = $this->convertToDatabase($key, $value); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$jtisParameters['dateCreated'] = date('Y-m-d H:i:s'); |
79
|
|
|
$jtisParameters['dateUpdated'] = date('Y-m-d H:i:s'); |
80
|
|
|
try { |
81
|
|
|
$this->db->createCommand() |
82
|
|
|
->insert($this->jtisTable, $jtisParameters) |
83
|
|
|
->execute(); |
84
|
|
|
} catch (DatabaseException $e) { |
85
|
|
|
// @codeCoverageIgnoreStart |
86
|
|
|
// we have a MYSQL exception, we should not discard |
87
|
|
|
Yii::debug('Error while inserting entity', __METHOD__); |
88
|
|
|
throw $e; |
89
|
|
|
// @codeCoverageIgnoreEnd |
90
|
|
|
} |
91
|
|
|
$changedAttributes = array_fill_keys(array_keys($values), null); |
92
|
|
|
$jti->setOldAttributes($values); |
93
|
|
|
$jti->afterSave(true, $changedAttributes); |
94
|
|
|
$result = true; |
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Update Jti |
100
|
|
|
* @param JtiModelInterface $jti |
101
|
|
|
* @param null|array $attributes attributes to save |
102
|
|
|
* @return bool |
103
|
|
|
* @throws DatabaseException |
104
|
|
|
* @throws DuplicateIndexException |
105
|
|
|
* @throws DuplicateKeyException |
106
|
|
|
*/ |
107
|
|
|
protected function update(JtiModelInterface $jti, $attributes) |
108
|
|
|
{ |
109
|
|
|
if (!$jti->beforeSave(false)) { |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$values = $jti->getDirtyAttributes($attributes); |
|
|
|
|
114
|
|
|
$modelKey = $jti->key(); |
115
|
|
|
if (isset($values[$modelKey]) === true) { |
116
|
|
|
$entity = (new Query()) |
117
|
|
|
->select('*') |
118
|
|
|
->from($this->jtisTable) |
119
|
|
|
->where('id = :id', [':id' => $values[$modelKey]]) |
120
|
|
|
->one($this->db); |
121
|
|
|
if ($entity !== false) { |
122
|
|
|
throw new DuplicateKeyException('Duplicate key "' . $values[$modelKey] . '"'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
$jtiKey = isset($values[$modelKey]) ? $values[$modelKey] : $jti->getKey(); |
126
|
|
|
|
127
|
|
|
$jtiParameters = []; |
128
|
|
|
$this->setAttributesDefinitions($jti->attributesDefinition()); |
129
|
|
|
foreach ($values as $key => $value) { |
130
|
|
|
$jtiParameters[$key] = ($value !== null) ? $this->convertToDatabase($key, $value) : null; |
131
|
|
|
} |
132
|
|
|
$jtiParameters['dateUpdated'] = date('Y-m-d H:i:s'); |
133
|
|
|
try { |
134
|
|
|
if (array_key_exists($modelKey, $values) === true) { |
135
|
|
|
$oldJtiKey = $jti->getOldKey(); |
136
|
|
|
$this->db->createCommand() |
137
|
|
|
->update($this->jtisTable, $jtiParameters, 'id = :id', [':id' => $oldJtiKey]) |
138
|
|
|
->execute(); |
139
|
|
|
} else { |
140
|
|
|
$this->db->createCommand() |
141
|
|
|
->update($this->jtisTable, $jtiParameters, 'id = :id', [':id' => $jtiKey]) |
142
|
|
|
->execute(); |
143
|
|
|
} |
144
|
|
|
} catch (DatabaseException $e) { |
145
|
|
|
// @codeCoverageIgnoreStart |
146
|
|
|
// we have a MYSQL exception, we should not discard |
147
|
|
|
Yii::debug('Error while updating entity', __METHOD__); |
148
|
|
|
throw $e; |
149
|
|
|
// @codeCoverageIgnoreEnd |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$changedAttributes = []; |
153
|
|
|
foreach ($values as $name => $value) { |
154
|
|
|
$oldAttributes = $jti->getOldAttributes(); |
155
|
|
|
$changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null; |
156
|
|
|
$jti->setOldAttribute($name, $value); |
157
|
|
|
} |
158
|
|
|
$jti->afterSave(false, $changedAttributes); |
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
|
|
public function save(JtiModelInterface $jti, $attributes) |
166
|
|
|
{ |
167
|
|
|
if ($jti->getIsNewRecord()) { |
168
|
|
|
$result = $this->insert($jti, $attributes); |
169
|
|
|
} else { |
170
|
|
|
$result = $this->update($jti, $attributes); |
171
|
|
|
} |
172
|
|
|
return $result; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritdoc |
177
|
|
|
*/ |
178
|
|
|
public function findOne($key) |
179
|
|
|
{ |
180
|
|
|
$record = null; |
181
|
|
|
$jtiData = (new Query()) |
182
|
|
|
->select('*') |
183
|
|
|
->from($this->jtisTable) |
184
|
|
|
->where('id = :id', [':id' => $key]) |
185
|
|
|
->one($this->db); |
186
|
|
|
if ($jtiData !== false) { |
187
|
|
|
$record = Yii::createObject('sweelix\oauth2\server\interfaces\JtiModelInterface'); |
188
|
|
|
/** @var JtiModelInterface $record */ |
189
|
|
|
$properties = $record->attributesDefinition(); |
190
|
|
|
$this->setAttributesDefinitions($properties); |
191
|
|
|
$attributes = []; |
192
|
|
|
foreach ($jtiData as $key => $value) { |
193
|
|
|
if (isset($properties[$key]) === true) { |
194
|
|
|
$jtiData[$key] = $this->convertToModel($key, $value); |
195
|
|
|
$record->setAttribute($key, $jtiData[$key]); |
196
|
|
|
$attributes[$key] = $jtiData[$key]; |
197
|
|
|
// @codeCoverageIgnoreStart |
198
|
|
|
} elseif ($record->canSetProperty($key)) { |
199
|
|
|
// TODO: find a way to test attribute population |
200
|
|
|
$record->{$key} = $value; |
201
|
|
|
} |
202
|
|
|
// @codeCoverageIgnoreEnd |
203
|
|
|
} |
204
|
|
|
if (empty($attributes) === false) { |
205
|
|
|
$record->setOldAttributes($attributes); |
206
|
|
|
} |
207
|
|
|
$record->afterFind(); |
208
|
|
|
} |
209
|
|
|
return $record; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritdoc |
214
|
|
|
*/ |
215
|
|
|
public function delete(JtiModelInterface $jti) |
216
|
|
|
{ |
217
|
|
|
$result = false; |
218
|
|
|
if ($jti->beforeDelete()) { |
219
|
|
|
//TODO: check results to return correct information |
220
|
|
|
$this->db->createCommand() |
221
|
|
|
->delete($this->jtisTable, 'id = :id', [':id' => $jti->getKey()]) |
222
|
|
|
->execute(); |
223
|
|
|
$jti->setIsNewRecord(true); |
224
|
|
|
$jti->afterDelete(); |
225
|
|
|
$result = true; |
226
|
|
|
} |
227
|
|
|
return $result; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @inheritdoc |
232
|
|
|
*/ |
233
|
|
|
public function findAllBySubject($subject) |
234
|
|
|
{ |
235
|
|
|
$jtisList = (new Query()) |
236
|
|
|
->select('*') |
237
|
|
|
->from($this->jtisTable) |
238
|
|
|
->where('subject = :subject', [':subject' => $subject]) |
239
|
|
|
->all($this->db); |
240
|
|
|
$jtis = []; |
241
|
|
|
foreach ($jtisList as $jti) { |
242
|
|
|
$result = $this->findOne($jti['id']); |
243
|
|
|
if ($result instanceof JtiModelInterface) { |
244
|
|
|
$jtis[] = $result; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
return $jtis; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @inheritdoc |
252
|
|
|
*/ |
253
|
|
|
public function deleteAllBySubject($subject) |
254
|
|
|
{ |
255
|
|
|
$jtis = $this->findAllBySubject($subject); |
256
|
|
|
foreach ($jtis as $jti) { |
257
|
|
|
$this->delete($jti); |
258
|
|
|
} |
259
|
|
|
return true; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @inheritdoc |
264
|
|
|
*/ |
265
|
|
|
public function findAllByClientId($clientId) |
266
|
|
|
{ |
267
|
|
|
$jtisList = (new Query()) |
268
|
|
|
->select('*') |
269
|
|
|
->from($this->jtisTable) |
270
|
|
|
->where('clientId = :clientId', [':clientId' => $clientId]) |
271
|
|
|
->all($this->db); |
272
|
|
|
$jtis = []; |
273
|
|
|
foreach ($jtisList as $jti) { |
274
|
|
|
$result = $this->findOne($jti['id']); |
275
|
|
|
if ($result instanceof JtiModelInterface) { |
276
|
|
|
$jtis[] = $result; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
return $jtis; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @inheritdoc |
284
|
|
|
*/ |
285
|
|
|
public function deleteAllByClientId($clientId) |
286
|
|
|
{ |
287
|
|
|
$jtis = $this->findAllByClientId($clientId); |
288
|
|
|
foreach ($jtis as $jti) { |
289
|
|
|
$this->delete($jti); |
290
|
|
|
} |
291
|
|
|
return true; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @inheritdoc |
296
|
|
|
*/ |
297
|
|
|
public function deleteAllExpired() |
298
|
|
|
{ |
299
|
|
|
$jtis = (new Query())->select('*') |
300
|
|
|
->from($this->jtisTable) |
301
|
|
|
->where('expires < :date', [':date' => date('Y-m-d H:i:s')]) |
302
|
|
|
->all($this->db); |
303
|
|
|
foreach ($jtis as $jtiQuery) { |
304
|
|
|
$jti = $this->findOne($jtiQuery['id']); |
305
|
|
|
if ($jti instanceof JtiModelInterface) { |
306
|
|
|
$this->delete($jti); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
return true; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @inheritdoc |
314
|
|
|
*/ |
315
|
|
|
public function findAll() |
316
|
|
|
{ |
317
|
|
|
$jtisList = (new Query()) |
318
|
|
|
->select('*') |
319
|
|
|
->from($this->jtisTable) |
320
|
|
|
->all($this->db); |
321
|
|
|
$jtis = []; |
322
|
|
|
foreach ($jtisList as $jti) { |
323
|
|
|
$result = $this->findOne($jti['id']); |
324
|
|
|
if ($result instanceof JtiModelInterface) { |
325
|
|
|
$jtis[] = $result; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
return $jtis; |
329
|
|
|
} |
330
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.