Файл: protected/models/SocialLogin.php
Строк: 117
<?php
/**
* This is the model class for table "social_login".
*
* The followings are the available columns in table 'social_login':
* @property string $social_login_id
* @property string $user_fk
* @property string $provider
* @property string $identifier
*
* The followings are the available model relations:
* @property User $userFk
*/
class SocialLogin extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return SocialLogin the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'social_login';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('user_fk', 'length', 'max'=>10),
array('provider', 'length', 'max'=>16),
array('identifier', 'length', 'max'=>50),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('social_login_id, user_fk, provider, identifier', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_fk'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'social_login_id' => Yii::t('yii', 'Id'),
'user_fk' => Yii::t('yii', 'User'),
'provider' => Yii::t('yii', 'Provider'),
'identifier' => Yii::t('yii', 'Identifier'),
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('social_login_id',$this->social_login_id,true);
$criteria->compare('user_fk',$this->user_fk,true);
$criteria->compare('provider',$this->provider,true);
$criteria->compare('identifier',$this->identifier,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/**
*
* @param HybridAuthIdentity $auth
*/
public function saveLogin($auth) {
$user = null;
if($auth->userProfile->emailVerified) {
if(!$user = User::model()->findByAttributes(array('email' => $auth->userProfile->emailVerified))) {
$user = new User;
$user->username = Utility::uniqueUsername($auth->userProfile->emailVerified);
$user->email = $auth->userProfile->emailVerified;
$user->is_active = 1;
$user->created_at = new CDbExpression('NOW()');
$user->first_name = $auth->userProfile->firstName;
$user->last_name = $auth->userProfile->lastName;
$user->save(false);
}
Yii::app()->user->setId($user->user_id);
}
if(!$login = $this->findByAttributes(array('provider' => $auth->provider, 'identifier' => $auth->userProfile->identifier))) {
$model = new self;
$model->provider = $auth->provider;
$model->identifier = $auth->userProfile->identifier;
if($user) {
$model->user_fk = $user->user_id;
}
$model->save();
}
else if($user && $login->user_fk != $user->user_id){
$login->user_fk = $user->user_id;
$login->save();
}
}
}