|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Client.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\behaviors\SplitToArrayBehavior; |
|
19
|
|
|
use sweelix\oauth2\server\interfaces\ClientModelInterface; |
|
20
|
|
|
use Yii; |
|
21
|
|
|
use yii\validators\UrlValidator; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This is the client model |
|
25
|
|
|
* |
|
26
|
|
|
* @author Philippe Gaultier <[email protected]> |
|
27
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
|
28
|
|
|
* @license http://www.sweelix.net/license license |
|
29
|
|
|
* @version 1.1.0 |
|
30
|
|
|
* @link http://www.sweelix.net |
|
31
|
|
|
* @package sweelix\oauth2\server\models |
|
32
|
|
|
* @since 1.0.0 |
|
33
|
|
|
* |
|
34
|
|
|
* @property string $id |
|
35
|
|
|
* @property string $secret |
|
36
|
|
|
* @property string|array $redirectUri |
|
37
|
|
|
* @property array $grantTypes |
|
38
|
|
|
* @property string $userId |
|
39
|
|
|
* @property array $scopes |
|
40
|
|
|
* @property string $name |
|
41
|
|
|
* @property bool $isPublic |
|
42
|
|
|
*/ |
|
43
|
|
|
class Client extends BaseModel implements ClientModelInterface |
|
44
|
|
|
{ |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
21 |
|
public function behaviors() |
|
50
|
|
|
{ |
|
51
|
21 |
|
$behaviors = parent::behaviors(); |
|
52
|
21 |
|
$behaviors['emptyArray'] = [ |
|
53
|
21 |
|
'class' => EmptyArrayBehavior::className(), |
|
54
|
21 |
|
'attributes' => ['scopes', 'grantTypes'], |
|
55
|
|
|
]; |
|
56
|
21 |
|
$behaviors['splitToArray'] = [ |
|
57
|
21 |
|
'class' => SplitToArrayBehavior::className(), |
|
58
|
21 |
|
'attributes' => ['redirectUri'], |
|
59
|
|
|
]; |
|
60
|
21 |
|
return $behaviors; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @inheritdoc |
|
65
|
|
|
*/ |
|
66
|
17 |
|
public function rules() |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
17 |
|
[['id', 'secret', 'name'], 'string'], |
|
70
|
17 |
|
[['redirectUri'], function($attribute, $params) { |
|
|
|
|
|
|
71
|
4 |
|
$data = $this->{$attribute}; |
|
72
|
|
|
|
|
73
|
4 |
|
if (is_array($data) === false) { |
|
74
|
3 |
|
$data = explode(' ', $data); |
|
75
|
3 |
|
} |
|
76
|
4 |
|
foreach($data as $redirectUri) { |
|
77
|
4 |
|
$isLocalhost = strncmp('http://localhost', $redirectUri, 16); |
|
78
|
4 |
|
$isSecureLocalhost = strncmp('https://localhost', $redirectUri, 17); |
|
79
|
4 |
|
if (($isLocalhost !== 0) && ($isSecureLocalhost !== 0)) { |
|
80
|
4 |
|
$validator = new UrlValidator(); |
|
81
|
4 |
|
if ($validator->validate($redirectUri, $error) === false) { |
|
82
|
1 |
|
$this->addError($attribute, $error); |
|
83
|
1 |
|
break; |
|
84
|
|
|
} |
|
85
|
4 |
|
} |
|
86
|
4 |
|
} |
|
87
|
17 |
|
}], |
|
88
|
17 |
|
[['scopes'], 'scope'], |
|
89
|
17 |
|
[['isPublic'], 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true], |
|
90
|
17 |
|
[['id', 'secret', 'isPublic'], 'required'], |
|
91
|
17 |
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return \sweelix\oauth2\server\interfaces\ClientServiceInterface |
|
96
|
|
|
*/ |
|
97
|
21 |
|
protected static function getDataService() |
|
98
|
|
|
{ |
|
99
|
21 |
|
return Yii::createObject('sweelix\oauth2\server\interfaces\ClientServiceInterface'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
*/ |
|
105
|
17 |
|
public function key() |
|
106
|
|
|
{ |
|
107
|
17 |
|
return 'id'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return array definition of model attributes |
|
112
|
|
|
* @since 1.0.0 |
|
113
|
|
|
*/ |
|
114
|
17 |
|
public function attributesDefinition() |
|
115
|
|
|
{ |
|
116
|
|
|
return [ |
|
117
|
17 |
|
'id' => 'string', |
|
118
|
17 |
|
'secret' => 'string', |
|
119
|
17 |
|
'redirectUri' => 'array', |
|
120
|
17 |
|
'grantTypes' => 'array', |
|
121
|
17 |
|
'userId' => 'string', |
|
122
|
17 |
|
'scopes' => 'array', |
|
123
|
17 |
|
'name' => 'string', |
|
124
|
17 |
|
'isPublic' => 'bool', |
|
125
|
17 |
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @inheritdoc |
|
130
|
|
|
*/ |
|
131
|
18 |
|
public static function findOne($id) |
|
132
|
|
|
{ |
|
133
|
18 |
|
return self::getDataService()->findOne($id); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @inheritdoc |
|
138
|
|
|
*/ |
|
139
|
17 |
|
public function save($runValidation = true, $attributes = null) |
|
140
|
|
|
{ |
|
141
|
17 |
|
if ($runValidation && !$this->validate($attributes)) { |
|
142
|
3 |
|
Yii::info('Model not inserted due to validation error.', __METHOD__); |
|
143
|
3 |
|
$result = false; |
|
144
|
3 |
|
} else { |
|
145
|
17 |
|
$result = self::getDataService()->save($this, $attributes); |
|
146
|
|
|
} |
|
147
|
17 |
|
return $result; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @inheritdoc |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function delete() |
|
154
|
|
|
{ |
|
155
|
1 |
|
return self::getDataService()->delete($this); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @inheritdoc |
|
160
|
|
|
*/ |
|
161
|
5 |
|
public function hasUser($userId) |
|
162
|
|
|
{ |
|
163
|
5 |
|
return self::getDataService()->hasUser($this, $userId); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @inheritdoc |
|
168
|
|
|
*/ |
|
169
|
3 |
|
public function addUser($userId) |
|
170
|
|
|
{ |
|
171
|
3 |
|
return self::getDataService()->addUser($this, $userId); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @inheritdoc |
|
176
|
|
|
*/ |
|
177
|
4 |
|
public function removeUser($userId) |
|
178
|
|
|
{ |
|
179
|
4 |
|
return self::getDataService()->removeUser($this, $userId); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @inheritdoc |
|
184
|
|
|
*/ |
|
185
|
1 |
|
public static function findAllByUserId($userId) |
|
186
|
|
|
{ |
|
187
|
1 |
|
return self::getDataService()->findAllByUserId($userId); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.