|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AccessToken.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.1.0 |
|
11
|
|
|
* @link http://www.sweelix.net |
|
12
|
|
|
* @package sweelix\oauth2\server\models |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace sweelix\oauth2\server\models; |
|
16
|
|
|
|
|
17
|
|
|
use sweelix\oauth2\server\behaviors\EmptyArrayBehavior; |
|
18
|
|
|
use sweelix\oauth2\server\interfaces\AccessTokenModelInterface; |
|
19
|
|
|
use Yii; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This is the access token model |
|
23
|
|
|
* |
|
24
|
|
|
* @author Philippe Gaultier <[email protected]> |
|
25
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
|
26
|
|
|
* @license http://www.sweelix.net/license license |
|
27
|
|
|
* @version 1.1.0 |
|
28
|
|
|
* @link http://www.sweelix.net |
|
29
|
|
|
* @package sweelix\oauth2\server\models |
|
30
|
|
|
* @since 1.0.0 |
|
31
|
|
|
* |
|
32
|
|
|
* @property string $id |
|
33
|
|
|
* @property string $clientId |
|
34
|
|
|
* @property string $userId |
|
35
|
|
|
* @property string $expiry |
|
36
|
|
|
* @property array $scopes |
|
37
|
|
|
*/ |
|
38
|
|
|
class AccessToken extends BaseModel implements AccessTokenModelInterface |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
12 |
|
public function behaviors() |
|
45
|
|
|
{ |
|
46
|
12 |
|
$behaviors = parent::behaviors(); |
|
47
|
12 |
|
$behaviors['emptyArray'] = [ |
|
48
|
12 |
|
'class' => EmptyArrayBehavior::className(), |
|
49
|
12 |
|
'attributes' => ['scopes'], |
|
50
|
|
|
]; |
|
51
|
12 |
|
return $behaviors; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
11 |
|
public function rules() |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
60
|
11 |
|
[['id', 'clientId'], 'string'], |
|
61
|
11 |
|
[['scopes'], 'scope'], |
|
62
|
11 |
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return \sweelix\oauth2\server\interfaces\AccessTokenServiceInterface |
|
67
|
|
|
*/ |
|
68
|
13 |
|
protected static function getDataService() |
|
69
|
|
|
{ |
|
70
|
13 |
|
return Yii::createObject('sweelix\oauth2\server\interfaces\AccessTokenServiceInterface'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @inheritdoc |
|
75
|
|
|
*/ |
|
76
|
11 |
|
public function key() |
|
77
|
|
|
{ |
|
78
|
11 |
|
return 'id'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return array definition of model attributes |
|
83
|
|
|
* @since 1.0.0 |
|
84
|
|
|
*/ |
|
85
|
11 |
|
public function attributesDefinition() |
|
86
|
|
|
{ |
|
87
|
|
|
return [ |
|
88
|
11 |
|
'id' => 'string', |
|
89
|
11 |
|
'clientId' => 'string', |
|
90
|
11 |
|
'userId' => 'string', |
|
91
|
11 |
|
'expiry' => 'string', |
|
92
|
11 |
|
'scopes' => 'array', |
|
93
|
11 |
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
9 |
|
public static function findOne($id) |
|
100
|
|
|
{ |
|
101
|
9 |
|
return self::getDataService()->findOne($id); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritdoc |
|
106
|
|
|
*/ |
|
107
|
11 |
|
public function save($runValidation = true, $attributes = null) |
|
108
|
|
|
{ |
|
109
|
11 |
|
if ($runValidation && !$this->validate($attributes)) { |
|
110
|
1 |
|
Yii::info('Model not inserted due to validation error.', __METHOD__); |
|
111
|
1 |
|
$result = false; |
|
112
|
1 |
|
} else { |
|
113
|
11 |
|
$result = self::getDataService()->save($this, $attributes); |
|
114
|
|
|
} |
|
115
|
11 |
|
return $result; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @inheritdoc |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function delete() |
|
122
|
|
|
{ |
|
123
|
2 |
|
return self::getDataService()->delete($this); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @inheritdoc |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public static function findAllByUserId($userId) |
|
130
|
|
|
{ |
|
131
|
3 |
|
return self::getDataService()->findAllByUserId($userId); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @inheritdoc |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public static function deleteAllByUserId($userId) |
|
138
|
|
|
{ |
|
139
|
1 |
|
return self::getDataService()->deleteAllByUserId($userId); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @inheritdoc |
|
144
|
|
|
*/ |
|
145
|
3 |
|
public static function findAllByClientId($clientId) |
|
146
|
|
|
{ |
|
147
|
3 |
|
return self::getDataService()->findAllByClientId($clientId); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @inheritdoc |
|
152
|
|
|
*/ |
|
153
|
2 |
|
public static function deleteAllByClientId($clientId) |
|
154
|
|
|
{ |
|
155
|
2 |
|
return self::getDataService()->deleteAllByClientId($clientId); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|