forked from Nodge/yii2-eauth
/
ServiceBase.php
359 lines (318 loc) · 7.09 KB
/
ServiceBase.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/**
* ServiceBase class file.
*
* @author Alexander Nikitin <nikitin-a-g@yandex.ru>
* @link http://github.com/BarBQ/yii2eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace barbq\eauth;
use Yii;
use yii\base\Object;
use yii\helpers\Url;
/**
* EAuthServiceBase is a base class for providers.
*
* @package application.extensions.eauth
*/
abstract class ServiceBase extends Object implements IAuthService
{
/**
* @var string the service name.
*/
protected $name;
/**
*
* @var string the service title to display in views.
*/
protected $title;
/**
* @var string the service type (e.g. OpenID, OAuth).
*/
protected $type;
/**
* @var array arguments for the jQuery.eauth() javascript function.
*/
protected $jsArguments = array();
/**
* @var array authorization attributes.
* @see getAttribute
* @see getItem
*/
protected $attributes = array();
/**
* @var boolean whether user was successfuly authenticated.
* @see getIsAuthenticated
*/
protected $authenticated = false;
/**
* @var boolean whether is attributes was fetched.
*/
private $fetched = false;
/**
* @var EAuth the {@link EAuth} application component.
*/
private $component;
/**
* @var string the redirect url after successful authorization.
*/
private $redirectUrl = '';
/**
* @var string the redirect url after unsuccessful authorization (e.g. user canceled).
*/
private $cancelUrl = '';
/**
* PHP getter magic method.
* This method is overridden so that service attributes can be accessed like properties.
*
* @param string $name property name.
* @return mixed property value.
* @see getAttribute
*/
public function __get($name)
{
if ($this->hasAttribute($name)) {
return $this->getAttribute($name);
} else {
return parent::__get($name);
}
}
/**
* Checks if a attribute value is null.
* This method overrides the parent implementation by checking
* if the attribute is null or not.
*
* @param string $name the attribute name.
* @return boolean whether the attribute value is null.
*/
public function __isset($name)
{
if ($this->hasAttribute($name)) {
return true;
} else {
return parent::__isset($name);
}
}
/**
* Initialize the component.
* Sets the default {@link redirectUrl} and {@link cancelUrl}.
*/
public function init()
{
parent::init();
$this->setRedirectUrl(Yii::$app->getUser()->getReturnUrl());
$service = Yii::$app->getRequest()->getQueryParam('service');
$cancelUrl = Url::to(['', 'service' => $service], true);
$this->setCancelUrl($cancelUrl);
}
/**
* Returns service name(id).
*
* @return string the service name(id).
*/
public function getServiceName()
{
return $this->name;
}
/**
* Returns service title.
*
* @return string the service title.
*/
public function getServiceTitle()
{
return Yii::t('eauth', $this->title);
}
/**
* Returns service type (e.g. OpenID, OAuth).
*
* @return string the service type (e.g. OpenID, OAuth).
*/
public function getServiceType()
{
return $this->type;
}
/**
* Returns arguments for the jQuery.eauth() javascript function.
*
* @return array the arguments for the jQuery.eauth() javascript function.
*/
public function getJsArguments()
{
return $this->jsArguments;
}
/**
* Sets {@link EAuth} application component
*
* @param EAuth $component the application auth component.
*/
public function setComponent($component)
{
$this->component = $component;
}
/**
* Returns the {@link EAuth} application component.
*
* @return EAuth the {@link EAuth} application component.
*/
public function getComponent()
{
return $this->component;
}
/**
* Sets redirect url after successful authorization.
*
* @param string $url to redirect.
*/
public function setRedirectUrl($url)
{
$this->redirectUrl = $url;
}
/**
* @return string the redirect url after successful authorization.
*/
public function getRedirectUrl()
{
return $this->redirectUrl;
}
/**
* Sets redirect url after unsuccessful authorization (e.g. user canceled).
*
* @param string $url
*/
public function setCancelUrl($url)
{
$this->cancelUrl = $url;
}
/**
* @return string the redirect url after unsuccessful authorization (e.g. user canceled).
*/
public function getCancelUrl()
{
return $this->cancelUrl;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Authenticate the user.
*
* @return boolean whether user was successfuly authenticated.
*/
public function authenticate()
{
return $this->getIsAuthenticated();
}
/**
* Whether user was successfuly authenticated.
*
* @return boolean whether user was successfuly authenticated.
*/
public function getIsAuthenticated()
{
return $this->authenticated;
}
/**
* Redirect to the url. If url is null, {@link redirectUrl} will be used.
*
* @param string $url url to redirect.
* @param array $params
*/
public function redirect($url = null, $params = array())
{
$this->component->redirect(isset($url) ? $url : $this->redirectUrl, true, $params);
}
/**
* Redirect to the {@link cancelUrl} or simply close the popup window.
*/
public function cancel($url = null)
{
$this->component->redirect(isset($url) ? $url : $this->cancelUrl, !$this->component->popup);
}
/**
* Fetch attributes array.
*
* @return boolean whether the attributes was successfully fetched.
*/
protected function fetchAttributes()
{
return true;
}
/**
* Fetch attributes array.
* This function is internally used to handle fetched state.
*/
protected function _fetchAttributes()
{
if (!$this->fetched) {
$this->fetched = true;
$result = $this->fetchAttributes();
if (isset($result)) {
$this->fetched = $result;
}
}
}
/**
* Returns the user unique id.
*
* @return mixed the user id.
*/
public function getId()
{
$this->_fetchAttributes();
return $this->attributes['id'];
}
/**
* Returns the array that contains all available authorization attributes.
*
* @return array the attributes.
*/
public function getAttributes()
{
$this->_fetchAttributes();
$attributes = array();
foreach ($this->attributes as $key => $val) {
$attributes[$key] = $this->getAttribute($key);
}
return $attributes;
}
/**
* Returns the authorization attribute value.
*
* @param string $key the attribute name.
* @param mixed $default the default value.
* @return mixed the attribute value.
*/
public function getAttribute($key, $default = null)
{
$this->_fetchAttributes();
$getter = 'get' . $key;
if (method_exists($this, $getter)) {
return $this->$getter();
} else {
return isset($this->attributes[$key]) ? $this->attributes[$key] : $default;
}
}
/**
* Whether the authorization attribute exists.
*
* @param string $key the attribute name.
* @return boolean true if attribute exists, false otherwise.
*/
public function hasAttribute($key)
{
$this->_fetchAttributes();
return isset($this->attributes[$key]);
}
/**
* @return bool
*/
public function getIsInsidePopup()
{
return isset($_GET['js']);
}
}