Last active
August 29, 2015 14:03
-
-
Save kiaplayer/def3274e6265f8021356 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace backend\models; | |
use Yii; | |
use yii\base\NotSupportedException; | |
use yii\db\ActiveRecord; | |
use yii\db\Expression; | |
use yii\web\IdentityInterface; | |
/** | |
* Модель backend-пользователя | |
* | |
* @property integer $id | |
* @property string $username | |
* @property string $auth_key | |
* @property string $password_hash | |
* @property string $email | |
* @property integer $is_active | |
* @property integer $created_by | |
* @property string $created_at | |
* @property integer $updated_by | |
* @property string $updated_at | |
* | |
* @property BackendUser $createdBy | |
* @property BackendUser $updatedBy | |
*/ | |
class BackendUser extends ActiveRecord implements IdentityInterface | |
{ | |
/** | |
* @var string пароль | |
*/ | |
public $password; | |
/** | |
* @inheritdoc | |
*/ | |
public static function tableName() | |
{ | |
return '{{%backend_user}}'; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function behaviors() | |
{ | |
return [ | |
'timestamp' => [ | |
'class' => 'yii\behaviors\TimestampBehavior', | |
'value' => new Expression('NOW()'), | |
], | |
'blameable' => [ | |
'class' => 'yii\behaviors\BlameableBehavior', | |
], | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['username', 'email', 'is_active'], 'required'], | |
[['password'], 'required', 'on' => ['create']], | |
[['is_active'], 'boolean'], | |
[['email'], 'email'], | |
[['username'], 'string', 'min' => 4, 'max' => 255], | |
[['password'], 'string', 'min' => 4], | |
[['username', 'email'], 'unique'], | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function attributeLabels() | |
{ | |
return [ | |
'id' => 'ID', | |
'username' => 'Логин', | |
'password' => 'Пароль', | |
'email' => 'E-mail', | |
'is_active' => 'Активен', | |
'created_by' => 'Автор', | |
'created_at' => 'Дата создания', | |
'updated_by' => 'Автор последнего изменения', | |
'updated_at' => 'Дата последнего обновления', | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function beforeSave($insert) | |
{ | |
if (parent::beforeSave($insert)) { | |
if ($this->isNewRecord) { | |
$this->auth_key = Yii::$app->getSecurity()->generateRandomKey(); | |
} | |
if ($this->password) { | |
$this->password_hash = Yii::$app->getSecurity()->generatePasswordHash($this->password); | |
} | |
return true; | |
} | |
return false; | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getCreatedBy() | |
{ | |
return $this->hasOne(BackendUser::className(), ['id' => 'created_by']); | |
} | |
/** | |
* @return \yii\db\ActiveQuery | |
*/ | |
public function getUpdatedBy() | |
{ | |
return $this->hasOne(BackendUser::className(), ['id' => 'updated_by']); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getId() | |
{ | |
return $this->getPrimaryKey(); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function getAuthKey() | |
{ | |
return $this->auth_key; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function validateAuthKey($authKey) | |
{ | |
return $this->getAuthKey() === $authKey; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public static function findIdentity($id) | |
{ | |
return static::findOne(['id' => $id, 'is_active' => true]); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public static function findIdentityByAccessToken($token, $type = null) | |
{ | |
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); | |
} | |
/** | |
* Проверка пароля | |
* @param string $password | |
* @return boolean | |
*/ | |
public function validatePassword($password) | |
{ | |
return Yii::$app->getSecurity()->validatePassword($password, $this->password_hash); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment