forked from SleepWalker/hoauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HOAuthAction.php
495 lines (436 loc) · 15.1 KB
/
HOAuthAction.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* HOAuthAction - this the main class in hoauth extension.
*
* @uses CAction
* @version 1.2.4
* @copyright Copyright © 2013 Sviatoslav Danylenko
* @author Sviatoslav Danylenko <[email protected]>
* @license MIT ({@link http://opensource.org/licenses/MIT})
* @link https://github.com/SleepWalker/hoauth
*/
/**
* HOAuthAction provides simple integration with social network authorization lib Hybridauth in Yii.
*
* HOAuthAction requires, that your user model implements findByEmail() method, that should return user model by its email.
*
* Avaible social networks:
* OpenID, Google, Facebook, Twitter, Yahoo, MySpace, Windows Live, LinkedIn, Foursquare, AOL
* Additional social networks can be found at: {@link http://hybridauth.sourceforge.net/download.html}
*
* Social Auth widget:
* <?php $this->widget('ext.hoauth.HOAuthWidget', array(
* 'controllerId' => 'site', // id of controller where is your oauth action (default: site)
* )); ?>
* uses a little modified Zocial CSS3 buttons: {@link https://github.com/samcollins/css-social-buttons/}
*/
Yii::setPathOfAlias('hoauth', dirname(__FILE__));
class HOAuthAction extends CAction
{
/**
* @var boolean $enabled defines whether the ouath functionality is active. Useful for example for CMS, where user can enable or disable oauth functionality in control panel
*/
public $enabled = true;
/**
* @var string $model yii alias for user model (or class name, when this model class exists in import path)
*/
public $model = 'User';
/**
* @var array $attributes attributes synchronization array (user model attribute => oauth attribute). List of available profile attributes you can see at {@link http://hybridauth.sourceforge.net/userguide/Profile_Data_User_Profile.html "HybridAuth's Documentation"}.
*
* Additional attributes:
* birthDate - The full date of birthday, eg. 1991-09-03
* genderShort - short representation of gender, eg. 'm', 'f'
*
* You can also set attributes, that you need to save in model too, eg.:
* 'attributes' => array(
* 'is_active' => 1,
* 'date_joined' => new CDbExpression('NOW()'),
* ),
*
* @see HOAuthAction::$avaibleAtts
*/
public $attributes;
/**
* @var string $scenario scenario name for the $model (optional)
*/
public $scenario = 'insert';
/**
* @var string $loginAction name of a local login action
*/
public $loginAction = 'actionLogin';
/**
* @var integer $duration how long the script will remember the user
*/
public $duration = 2592000; // 30 days
/**
* @var boolean $useYiiUser enables support of Yii user module
*/
public static $useYiiUser = false;
/**
* @var boolean $alwaysCheckPass flag to control password checking for the scenario,
* when when social network returned email of existing local account. If set to
* `false` user will be automatically logged in without confirming account with password
*/
public $alwaysCheckPass = true;
/**
* @var string $userIdentityClass UserIdentity class that will be used to log user in.
*/
public $userIdentityClass = 'UserIdentity';
/**
* @var string $usernameAttribute you can specify the username attribute, when user must fill it
*/
public $usernameAttribute = false;
/**
* @var string $_emailAttribute
*/
protected $_emailAttribute = false;
/**
* @var array $avaibleAtts Hybridauth attributes that support by this script (this a list of all available attributes in HybridAuth 2.0.11) + additional attributes (see $attributes)
*/
protected $_avaibleAtts = array('identifier', 'profileURL', 'webSiteURL', 'photoURL', 'displayName', 'description', 'firstName', 'lastName', 'gender', 'language', 'age', 'birthDay', 'birthMonth', 'birthYear', 'email', 'emailVerified', 'phone', 'address', 'country', 'region', 'city', 'zip', 'birthDate', 'genderShort');
public function run()
{
// openId login
if($this->enabled)
{
$path = dirname(__FILE__);
// checking if we have `yii-user` module (I think that `UWrelBelongsTo` is unique class name from `yii-user`)
if($this->useYiiUser || file_exists(Yii::getPathOfAlias('application.modules.user.components') . '/UWrelBelongsTo.php'))
{
$this->useYiiUser = true;
// setting up yii-user's user model
Yii::import('application.modules.user.models.*');
Yii::import('hoauth.DummyUserIdentity');
// preparing attributes array for `yii-user` module
if(!is_array($this->attributes))
$this->attributes = array();
$this->attributes = CMap::mergeArray($this->attributes, array(
'email' => 'email',
'username' => 'displayName',
'status' => User::STATUS_ACTIVE,
));
$this->usernameAttribute = 'username';
$this->_emailAttribute = 'email';
}
else
{
Yii::import($this->model, true);
$this->model = substr($this->model, strrpos($this->model, '.'));
if(!method_exists($this->model, 'findByEmail'))
throw new Exception("Model '{$this->model}' must implement the 'findByEmail' method");
$this->_emailAttribute = array_search('email', $this->attributes);
}
if(!isset($this->attributes) || !is_array($this->attributes) || !count($this->attributes))
throw new CException('You must specify the model attributes for ' . __CLASS__);
if(!in_array('email', $this->attributes))
throw new CException("You forgot to bind 'email' field in " . __CLASS__ . "::attributes property.");
if(isset($_GET['provider']))
{
Yii::import('hoauth.models.*');
$this->oAuth($_GET['provider']);
}
else
{
require($path.'/hybridauth/index.php');
Yii::app()->end();
}
}
Yii::app()->controller->{$this->loginAction}();
}
/**
* Initiates authorization with specified $provider and
* then authenticates the user, when all goes fine
*
* @param mixed $provider provider name for HybridAuth
* @access protected
* @return void
*/
protected function oAuth( $provider )
{
try{
// trying to authenticate user via social network
$oAuth = UserOAuth::model()->authenticate( $provider );
$userProfile = $oAuth->profile;
// If we already have a user logged in, associate the authenticated
// provider with the logged-in user
if(!Yii::app()->user->isGuest)
{
$accessCode = 1;
$oAuth->bindTo(Yii::app()->user->id);
}
else
{
$newUser = false;
if($oAuth->isBond)
{
// this social network account is bond to existing local account
Yii::log("Logged in with existing link with '$provider' provider", CLogger::LEVEL_INFO, 'hoauth.'.__CLASS__);
if($this->useYiiUser)
$user = User::model()->findByPk($oAuth->user_id);
else
$user = call_user_func(array($this->model, 'model'))->findByPk($oAuth->user_id);
}
if(!$oAuth->isBond || !$user)
{
if(!empty($userProfile->emailVerified))
{
// checking whether we already have a user with specified email
if($this->useYiiUser)
$user = User::model()->findByAttributes(array('email' => $userProfile->emailVerified));
else
$user = call_user_func(array($this->model, 'model'))->findByEmail($userProfile->emailVerified);
}
if(!isset($user))
{
// registering a new user
$user = new $this->model($this->scenario);
$newUser = true;
}
if($this->alwaysCheckPass || $user->isNewRecord)
if(method_exists($this->controller, 'hoauthProcessUser'))
$user = $this->controller->hoauthProcessUser($user, $newUser);
else
$user = $this->processUser($user, $userProfile);
}
// checking if current user is not banned or anything else
// $accessCode == 0 - user shouldn't get access
// $accessCode == 1 - user may login
// $accessCode == 2 - user may login, but not now (e.g. the email should be verified and activated)
$accessCode = 1;
if(method_exists($this->controller, 'hoauthCheckAccess'))
$accessCode = $this->controller->hoauthCheckAccess($user);
elseif($this->useYiiUser)
$accessCode = $this->yiiUserCheckAccess($user);
if(!$accessCode)
throw new Exception("Something wrong. You can not log in.");
if(!$oAuth->bindTo($user->primaryKey))
throw new Exception("Error, while binding user to provider:\n\n" . var_export($oAuth->errors, true));
// sign user in
if($accessCode === 1)
{
$identity = $this->useYiiUser
? new DummyUserIdentity($user->primaryKey, $user->email)
: new $this->userIdentityClass($user->email, null);
if(!Yii::app()->user->login($identity,$this->duration))
throw new Exception("Can't sign in, something wrong with UserIdentity class.");
// user was successfully logged in
// firing callback
if(method_exists($this->controller, 'hoauthAfterLogin'))
$this->controller->hoauthAfterLogin($user, $newUser);
}
if($accessCode === 2)
Yii::app()->end(); // stopping script to let checkAccess() function render new content
}
if($accessCode === 1)
{
?>
<script>
window.opener.location.reload();
window.close();
</script>
<?php
Yii::app()->end();
}
}
catch( Exception $e ){
$error = "";
// Display the received error
switch( $e->getCode() ){
case 0 : $error = "Unspecified error."; throw $e; break;
case 1 : $error = "Hybriauth configuration error."; break;
case 2 : $error = "Provider not properly configured."; break;
case 3 : $error = "Unknown or disabled provider."; break;
case 4 : $error = "Missing provider application credentials."; break;
case 5 : $error = "Authentication failed. The user has canceled the authentication or the provider refused the connection."; break;
case 6 : $error = "User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again.";
$oAuth->logout();
break;
case 7 : $error = "User not connected to the provider.";
$oAuth->logout();
break;
case 8 : $error = "Provider does not support this feature."; break;
}
$error .= "\n\n<br /><br /><b>Original error message:</b> " . $e->getMessage();
Yii::log($error, CLogger::LEVEL_INFO, 'hoauth.'.__CLASS__);
if(YII_DEBUG)
throw $e;
}
?>
<script>
window.close();
</script>
<?php
}
/**
* Registers new user, collects username and email if needed
*
* @param CActiveRecord $user current user model
* @param object $userProfile social network's user profile object
* @access protected
*/
protected function processUser($user, $userProfile)
{
if($this->useYiiUser)
{
$profile = new Profile();
// enabling register mode
// old versions of yii
$profile->regMode = true;
// new version, when regMode is static property
$prop = new ReflectionProperty('Profile', 'regMode');
if($prop->isStatic())
Profile::$regMode = true;
}
if($user->isNewRecord)
$this->populateModel($user, $userProfile);
// trying to fill email and username fields
// NOTE: we display `username` field in our form only if it is required by the model
if(!$user->isAttributeRequired($this->usernameAttribute))
$this->usernameAttribute = false;
$form = new HUserInfoForm($user, $this->_emailAttribute, $this->usernameAttribute);
if(!$form->validateUser())
{
$this->controller->render('hoauth.views.form', array(
'form' => $form,
));
Yii::app()->end();
}
// updating attributes in $user model (if needed)
$user = $form->validUserModel;
// the model won't be new, if user provided email and password of existing account
if($user->isNewRecord)
{
if(!$user->save())
throw new Exception("Error, while saving {$this->model} model:\n\n" . var_export($user->errors, true));
// trying to send activation email
$this->sendActivationEmail($user);
if($this->useYiiUser)
{
$profile->user_id = $user->primaryKey;
if($profile->hasAttribute('firstname'))
{
// we have an older yii-user version or is used db dump from data directory
$profile->firstname = $userProfile->firstName;
$profile->lastname = $userProfile->lastName;
}
else
{
$profile->first_name = $userProfile->firstName;
$profile->last_name = $userProfile->lastName;
}
if(!$profile->save())
throw new Exception("Error, while saving " . get_class($profile) . " model:\n\n" . var_export($profile->errors, true));
}
}
return $user;
}
/**
* Sends email activation email, when it is needed
*
* @param CActiveRecord $user current user model
* @access protected
* @return void
*/
protected function sendActivationEmail($user)
{
if($this->useYiiUser)
{
// why not to put this code not in controller, but in the User model of `yii-user` module?
// for now I can only copy-paste this code from controller...
if (Yii::app()->getModule('user')->sendActivationMail)
{
$activation_url = Yii::app()->createAbsoluteUrl('/user/activation/activation',array("activkey" => $user->activkey, "email" => $user->email));
UserModule::sendMail($user->email,UserModule::t("You registered on {site_name}",array('{site_name}'=>Yii::app()->name)),UserModule::t("To activate your account, please go to {activation_url}",array('{activation_url}'=>$activation_url)));
}
}
else
{
if(method_exists($user, 'sendActivationEmail'))
$user->sendActivationEmail();
elseif(method_exists($user, 'sendActivationMail')) // TODO: delete in future
$user->sendActivationMail();
}
}
/**
* Populates User model with data from social network profile
*
* @param CActiveRecord $user users model
* @param mixed $profile HybridAuth user profile object
* @access protected
*/
protected function populateModel($user, $profile)
{
foreach($this->attributes as $attribute => $pAtt)
{
if(in_array($pAtt, $this->_avaibleAtts))
{
switch($pAtt)
{
case 'genderShort':
$gender = array('female'=>'f','male'=>'m');
$att = $gender[$profile->gender];
break;
case 'birthDate':
$att = $profile->birthYear
? sprintf("%04d-%02d-%02d", $profile->birthYear, $profile->birthMonth, $profile->birthDay)
: null;
break;
case 'email':
$att = $profile->emailVerified;
break;
default:
$att = $profile->$pAtt;
}
if(!empty($att))
$user->$attribute = $att;
}else{
$user->$attribute = $pAtt;
}
}
}
/**
* Checks whether the $user can be logged in
*
* @param CActiveRecord $user current `yii-user` user's model
* @param boolean $render flag that enables rendering
*/
public function yiiUserCheckAccess($user, $render = true)
{
if($user->status==0&&Yii::app()->getModule('user')->loginNotActiv==false)
{
$error = UserIdentity::ERROR_STATUS_NOTACTIV;
$return = 2;
}
else if($user->status==-1)
{
$error = UserIdentity::ERROR_STATUS_BAN;
$return = 0;
}
else
{
$error = UserIdentity::ERROR_NONE;
$return = 1;
}
if($error && $render)
{
$this->controller->render('hoauth.views.yiiUserError', array(
'errorCode' => $error,
'user' => $user,
));
}
return $return;
}
public function getUseYiiUser()
{
return self::$useYiiUser;
}
public function setUseYiiUser($value)
{
self::$useYiiUser = $value;
}
public static function t($message,$params=array(),$source=null,$language=null)
{
return Yii::t('HOAuthAction.root', $message,$params,$source,$language);
}
}