forked from yiisoft/yii2-authclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseClient.php
285 lines (256 loc) · 7.79 KB
/
BaseClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\authclient;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\base\NotSupportedException;
use yii\helpers\Inflector;
use yii\helpers\StringHelper;
/**
* BaseClient is a base Auth Client class.
*
* @see ClientInterface
*
* @property string $id Service id.
* @property string $name Service name.
* @property array $normalizeUserAttributeMap Normalize user attribute map.
* @property string $title Service title.
* @property array $userAttributes List of user attributes.
* @property array $viewOptions View options in format: optionName => optionValue.
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
abstract class BaseClient extends Component implements ClientInterface
{
/**
* @var string auth service id.
* This value mainly used as HTTP request parameter.
*/
private $_id;
/**
* @var string auth service name.
* This value may be used in database records, CSS files and so on.
*/
private $_name;
/**
* @var string auth service title to display in views.
*/
private $_title;
/**
* @var array authenticated user attributes.
*/
private $_userAttributes;
/**
* @var array map used to normalize user attributes fetched from external auth service
* in format: normalizedAttributeName => sourceSpecification
* 'sourceSpecification' can be:
* - string, raw attribute name
* - array, pass to raw attribute value
* - callable, PHP callback, which should accept array of raw attributes and return normalized value.
*
* For example:
*
* ```php
* 'normalizeUserAttributeMap' => [
* 'about' => 'bio',
* 'language' => ['languages', 0, 'name'],
* 'fullName' => function ($attributes) {
* return $attributes['firstName'] . ' ' . $attributes['lastName'];
* },
* ],
* ```
*/
private $_normalizeUserAttributeMap;
/**
* @var array view options in format: optionName => optionValue
*/
private $_viewOptions;
/**
* @param string $id service id.
*/
public function setId($id)
{
$this->_id = $id;
}
/**
* @return string service id
*/
public function getId()
{
if (empty($this->_id)) {
$this->_id = $this->getName();
}
return $this->_id;
}
/**
* @param string $name service name.
*/
public function setName($name)
{
$this->_name = $name;
}
/**
* @return string service name.
*/
public function getName()
{
if ($this->_name === null) {
$this->_name = $this->defaultName();
}
return $this->_name;
}
/**
* @param string $title service title.
*/
public function setTitle($title)
{
$this->_title = $title;
}
/**
* @return string service title.
*/
public function getTitle()
{
if ($this->_title === null) {
$this->_title = $this->defaultTitle();
}
return $this->_title;
}
/**
* @param array $userAttributes list of user attributes
*/
public function setUserAttributes($userAttributes)
{
$this->_userAttributes = $this->normalizeUserAttributes($userAttributes);
}
/**
* @return array list of user attributes
*/
public function getUserAttributes()
{
if ($this->_userAttributes === null) {
$this->_userAttributes = $this->normalizeUserAttributes($this->initUserAttributes());
}
return $this->_userAttributes;
}
/**
* @param array $normalizeUserAttributeMap normalize user attribute map.
*/
public function setNormalizeUserAttributeMap($normalizeUserAttributeMap)
{
$this->_normalizeUserAttributeMap = $normalizeUserAttributeMap;
}
/**
* @return array normalize user attribute map.
*/
public function getNormalizeUserAttributeMap()
{
if ($this->_normalizeUserAttributeMap === null) {
$this->_normalizeUserAttributeMap = $this->defaultNormalizeUserAttributeMap();
}
return $this->_normalizeUserAttributeMap;
}
/**
* @param array $viewOptions view options in format: optionName => optionValue
*/
public function setViewOptions($viewOptions)
{
$this->_viewOptions = $viewOptions;
}
/**
* @return array view options in format: optionName => optionValue
*/
public function getViewOptions()
{
if ($this->_viewOptions === null) {
$this->_viewOptions = $this->defaultViewOptions();
}
return $this->_viewOptions;
}
/**
* Generates service name.
* @return string service name.
*/
protected function defaultName()
{
return Inflector::camel2id(StringHelper::basename(get_class($this)));
}
/**
* Generates service title.
* @return string service title.
*/
protected function defaultTitle()
{
return StringHelper::basename(get_class($this));
}
/**
* Initializes authenticated user attributes.
* @return array auth user attributes.
*/
protected function initUserAttributes()
{
throw new NotSupportedException('Method "' . get_class($this) . '::' . __FUNCTION__ . '" not implemented.');
}
/**
* Returns the default [[normalizeUserAttributeMap]] value.
* Particular client may override this method in order to provide specific default map.
* @return array normalize attribute map.
*/
protected function defaultNormalizeUserAttributeMap()
{
return [];
}
/**
* Returns the default [[viewOptions]] value.
* Particular client may override this method in order to provide specific default view options.
* @return array list of default [[viewOptions]]
*/
protected function defaultViewOptions()
{
return [];
}
/**
* Normalize given user attributes according to [[normalizeUserAttributeMap]].
* @param array $attributes raw attributes.
* @throws InvalidConfigException on incorrect normalize attribute map.
* @return array normalized attributes.
*/
protected function normalizeUserAttributes($attributes)
{
foreach ($this->getNormalizeUserAttributeMap() as $normalizedName => $actualName) {
if (is_scalar($actualName)) {
if (array_key_exists($actualName, $attributes)) {
$attributes[$normalizedName] = $attributes[$actualName];
}
} else {
if (is_callable($actualName)) {
$attributes[$normalizedName] = call_user_func($actualName, $attributes);
} elseif (is_array($actualName)) {
$haystack = $attributes;
$searchKeys = $actualName;
$isFound = true;
while (($key = array_shift($searchKeys)) !== null) {
if (is_array($haystack) && array_key_exists($key, $haystack)) {
$haystack = $haystack[$key];
} else {
$isFound = false;
break;
}
}
if ($isFound) {
$attributes[$normalizedName] = $haystack;
}
} else {
throw new InvalidConfigException('Invalid actual name "' . gettype($actualName) . '" specified at "' . get_class($this) . '::normalizeUserAttributeMap"');
}
}
}
return $attributes;
}
}