1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ScopeService.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\ScopeModelInterface; |
20
|
|
|
use sweelix\oauth2\server\interfaces\ScopeServiceInterface; |
21
|
|
|
use yii\db\Exception as DatabaseException; |
22
|
|
|
use Yii; |
23
|
|
|
use yii\db\Query; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This is the scope service for mySql |
27
|
|
|
* database structure |
28
|
|
|
* * oauth2:scopes:<sid> : hash (Scope) |
29
|
|
|
* * oauth2:scopes:keys : set scopeIds |
30
|
|
|
* * oauth2:scopes:defaultkeys : set default scopeIds |
31
|
|
|
* |
32
|
|
|
* @author Philippe Gaultier <[email protected]> |
33
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
34
|
|
|
* @license http://www.sweelix.net/license license |
35
|
|
|
* @version 1.2.0 |
36
|
|
|
* @link http://www.sweelix.net |
37
|
|
|
* @package sweelix\oauth2\server\services\mySql |
38
|
|
|
* @since 1.0.0 |
39
|
|
|
*/ |
40
|
|
|
class ScopeService extends BaseService implements ScopeServiceInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var string sql scope client table |
44
|
|
|
*/ |
45
|
|
|
public $scopeClientTable = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string sql scopes table |
49
|
|
|
*/ |
50
|
|
|
public $scopesTable = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Save Scope |
54
|
|
|
* @param ScopeModelInterface $scope |
55
|
|
|
* @param null|array $attributes attributes to save |
56
|
|
|
* @return bool |
57
|
|
|
* @throws DatabaseException |
58
|
|
|
* @throws DuplicateIndexException |
59
|
|
|
* @throws DuplicateKeyException |
60
|
|
|
* @since 1.0.0 |
61
|
|
|
*/ |
62
|
|
|
protected function insert(ScopeModelInterface $scope, $attributes) |
63
|
|
|
{ |
64
|
|
|
$result = false; |
65
|
|
|
if (!$scope->beforeSave(true)) { |
66
|
|
|
return $result; |
67
|
|
|
} |
68
|
|
|
$entity = (new Query()) |
69
|
|
|
->select('*') |
70
|
|
|
->from($this->scopesTable) |
71
|
|
|
->where('id = :id', [':id' => $scope->id]) |
|
|
|
|
72
|
|
|
->one($this->db); |
73
|
|
|
if ($entity !== false) { |
74
|
|
|
throw new DuplicateKeyException('Duplicate key "' . $scope->id . '"'); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
$values = $scope->getDirtyAttributes($attributes); |
|
|
|
|
77
|
|
|
$scopeParameters = []; |
78
|
|
|
$this->setAttributesDefinitions($scope->attributesDefinition()); |
79
|
|
|
foreach ($values as $key => $value) { |
80
|
|
|
if ($value !== null) { |
81
|
|
|
$scopeParameters[$key] = $this->convertToDatabase($key, $value); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
$scopeParameters['dateCreated'] = date('Y-m-d H:i:s'); |
85
|
|
|
$scopeParameters['dateUpdated'] = date('Y-m-d H:i:s'); |
86
|
|
|
try { |
87
|
|
|
$this->db->createCommand() |
88
|
|
|
->insert($this->scopesTable, $scopeParameters) |
89
|
|
|
->execute(); |
90
|
|
|
} catch (DatabaseException $e) { |
91
|
|
|
// @codeCoverageIgnoreStart |
92
|
|
|
// we have a MYSQL exception, we should not discard |
93
|
|
|
Yii::debug('Error while inserting entity', __METHOD__); |
94
|
|
|
throw $e; |
95
|
|
|
// @codeCoverageIgnoreEnd |
96
|
|
|
} |
97
|
|
|
$changedAttributes = array_fill_keys(array_keys($values), null); |
98
|
|
|
$scope->setOldAttributes($values); |
99
|
|
|
$scope->afterSave(true, $changedAttributes); |
100
|
|
|
$result = true; |
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Update ScopeModelInterface |
106
|
|
|
* @param ScopeModelInterface $scope |
107
|
|
|
* @param null|array $attributes attributes to save |
108
|
|
|
* @return bool |
109
|
|
|
* @throws DatabaseException |
110
|
|
|
* @throws DuplicateIndexException |
111
|
|
|
* @throws DuplicateKeyException |
112
|
|
|
*/ |
113
|
|
|
protected function update(ScopeModelInterface $scope, $attributes) |
114
|
|
|
{ |
115
|
|
|
if (!$scope->beforeSave(false)) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$values = $scope->getDirtyAttributes($attributes); |
|
|
|
|
120
|
|
|
$modelKey = $scope->key(); |
121
|
|
|
if (isset($values[$modelKey]) === true) { |
122
|
|
|
$entity = (new Query()) |
123
|
|
|
->select('*') |
124
|
|
|
->from($this->scopesTable) |
125
|
|
|
->where('id = :id', [':id' => $values[$modelKey]]) |
126
|
|
|
->one($this->db); |
127
|
|
|
if ($entity !== false) { |
128
|
|
|
throw new DuplicateKeyException('Duplicate key "' . $values[$modelKey] . '"'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
$scopeKey = isset($values[$modelKey]) ? $values[$modelKey] : $scope->getKey(); |
132
|
|
|
|
133
|
|
|
$scopeParameters = []; |
134
|
|
|
$this->setAttributesDefinitions($scope->attributesDefinition()); |
135
|
|
|
foreach ($values as $key => $value) { |
136
|
|
|
$scopeParameters[$key] = ($value !== null) ? $this->convertToDatabase($key, $value) : null; |
137
|
|
|
} |
138
|
|
|
$scopeParameters['dateUpdated'] = date('Y-m-d H:i:s'); |
139
|
|
|
try { |
140
|
|
|
if (array_key_exists($modelKey, $values) === true) { |
141
|
|
|
$oldScopeKey = $scope->getOldKey(); |
142
|
|
|
$this->db->createCommand() |
143
|
|
|
->update($this->scopesTable, $scopeParameters, 'id = :id', [':id' => $oldScopeKey]) |
144
|
|
|
->execute(); |
145
|
|
|
} else { |
146
|
|
|
$this->db->createCommand() |
147
|
|
|
->update($this->scopesTable, $scopeParameters, 'id = :id', [':id' => $scopeKey]) |
148
|
|
|
->execute(); |
149
|
|
|
} |
150
|
|
|
} catch (DatabaseException $e) { |
151
|
|
|
// @codeCoverageIgnoreStart |
152
|
|
|
// we have a MYSQL exception, we should not discard |
153
|
|
|
Yii::debug('Error while updating entity', __METHOD__); |
154
|
|
|
throw $e; |
155
|
|
|
// @codeCoverageIgnoreEnd |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$changedAttributes = []; |
159
|
|
|
foreach ($values as $name => $value) { |
160
|
|
|
$oldAttributes = $scope->getOldAttributes(); |
161
|
|
|
$changedAttributes[$name] = isset($oldAttributes[$name]) ? $oldAttributes[$name] : null; |
162
|
|
|
$scope->setOldAttribute($name, $value); |
163
|
|
|
} |
164
|
|
|
$scope->afterSave(false, $changedAttributes); |
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @inheritdoc |
170
|
|
|
*/ |
171
|
|
|
public function save(ScopeModelInterface $scope, $attributes) |
172
|
|
|
{ |
173
|
|
|
if ($scope->getIsNewRecord()) { |
174
|
|
|
$result = $this->insert($scope, $attributes); |
175
|
|
|
} else { |
176
|
|
|
$result = $this->update($scope, $attributes); |
177
|
|
|
} |
178
|
|
|
return $result; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @inheritdoc |
183
|
|
|
*/ |
184
|
|
|
public function findOne($key) |
185
|
|
|
{ |
186
|
|
|
$record = null; |
187
|
|
|
$scopeData = (new Query()) |
188
|
|
|
->select('*') |
189
|
|
|
->from($this->scopesTable) |
190
|
|
|
->where('id = :id', [':id' => $key]) |
191
|
|
|
->one($this->db); |
192
|
|
|
if ($scopeData !== false) { |
193
|
|
|
$record = Yii::createObject('sweelix\oauth2\server\interfaces\ScopeModelInterface'); |
194
|
|
|
/** @var ScopeModelInterface $record */ |
195
|
|
|
$properties = $record->attributesDefinition(); |
196
|
|
|
$this->setAttributesDefinitions($properties); |
197
|
|
|
$attributes = []; |
198
|
|
|
foreach ($scopeData as $key => $value) { |
199
|
|
|
if (isset($properties[$key]) === true) { |
200
|
|
|
$scopeData[$key] = $this->convertToModel($key, $value); |
201
|
|
|
$record->setAttribute($key, $scopeData[$key]); |
202
|
|
|
$attributes[$key] = $scopeData[$key]; |
203
|
|
|
// @codeCoverageIgnoreStart |
204
|
|
|
} elseif ($record->canSetProperty($key)) { |
205
|
|
|
// TODO: find a way to test attribute population |
206
|
|
|
$record->{$key} = $value; |
207
|
|
|
} |
208
|
|
|
// @codeCoverageIgnoreEnd |
209
|
|
|
} |
210
|
|
|
if (empty($attributes) === false) { |
211
|
|
|
$record->setOldAttributes($attributes); |
212
|
|
|
} |
213
|
|
|
$record->afterFind(); |
214
|
|
|
} |
215
|
|
|
return $record; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @inheritdoc |
220
|
|
|
*/ |
221
|
|
|
public function delete(ScopeModelInterface $scope) |
222
|
|
|
{ |
223
|
|
|
$result = false; |
224
|
|
|
if ($scope->beforeDelete()) { |
225
|
|
|
//TODO: check results to return correct information |
226
|
|
|
$this->db->createCommand() |
227
|
|
|
->delete($this->scopesTable, 'id = :id', [':id' => $scope->getKey()]) |
228
|
|
|
->execute(); |
229
|
|
|
$scope->setIsNewRecord(true); |
230
|
|
|
$scope->afterDelete(); |
231
|
|
|
$result = true; |
232
|
|
|
} |
233
|
|
|
return $result; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @inheritdoc |
238
|
|
|
*/ |
239
|
|
|
public function findAvailableScopeIds() |
240
|
|
|
{ |
241
|
|
|
$tmpScopes = (new Query()) |
242
|
|
|
->select('id') |
243
|
|
|
->from($this->scopesTable) |
244
|
|
|
->all($this->db); |
245
|
|
|
$scopes = []; |
246
|
|
|
foreach ($tmpScopes as $scope) { |
247
|
|
|
$scopes[] = $scope['id']; |
248
|
|
|
} |
249
|
|
|
return $scopes; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @inheritdoc |
254
|
|
|
*/ |
255
|
|
|
public function findDefaultScopeIds($clientId = null) |
256
|
|
|
{ |
257
|
|
|
//TODO: add default scopes for clients |
258
|
|
|
$defaultScopeIds = []; |
259
|
|
|
if ($clientId !== null) { |
260
|
|
|
$tmpScopeIds = (new Query()) |
261
|
|
|
->select('scopeId as ID') |
262
|
|
|
->from($this->scopeClientTable) |
263
|
|
|
->where('clientId = :clientId', [':clientId' => $clientId]) |
264
|
|
|
->all($this->db); |
265
|
|
|
} else { |
266
|
|
|
$tmpScopeIds = (new Query()) |
267
|
|
|
->select('id as ID') |
268
|
|
|
->from($this->scopesTable) |
269
|
|
|
->where('isDefault = :isDefault', [':isDefault' => true]) |
270
|
|
|
->all($this->db); |
271
|
|
|
} |
272
|
|
|
foreach ($tmpScopeIds as $tmpScopeId) { |
273
|
|
|
$defaultScopeIds[] = $tmpScopeId['ID']; |
274
|
|
|
} |
275
|
|
|
return $defaultScopeIds; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: