'create'], [['status'], 'default', 'value' => self::STATUS_ACTIVE], [['status'], 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'username' => 'Логин', 'password' => 'Пароль', 'email' => 'Email', 'status' => 'Статус', 'group' => 'Группа', 'api_key' => 'API ключ', 'created_at' => 'Дата создания', 'updated_at' => 'Дата изменения', ]; } /** * @return array */ public static function getStatusesArray() { return [ self::STATUS_ACTIVE => 'Активен', self::STATUS_DELETED => 'Удален', ]; } /** * @return string */ public function getStatusLabel() { return self::getStatusesArray()[$this->status]; } /** * @inheritdoc */ public static function findIdentity($id) { return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); } /** * @inheritdoc */ public static function findIdentityByAccessToken($token, $type = null) { throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); } /** * Finds user by username * * @param string $username * @return static|null */ public static function findByUsername($username) { return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); } /** * @inheritdoc */ public function getId() { return $this->getPrimaryKey(); } /** * @inheritdoc */ public function getAuthKey() { return $this->auth_key; } /** * @inheritdoc */ public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } /** * Validates password * * @param string $password password to validate * @return boolean if password provided is valid for current user */ public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password_hash); } /** * Generates password hash from password and sets it to the model * * @param string $password */ public function setPassword($password) { $this->password_hash = Yii::$app->security->generatePasswordHash($password); } /** * Generates "remember me" authentication key */ public function generateAuthKey() { $this->auth_key = Yii::$app->security->generateRandomString(); } public function getAvatar() { $hash = md5($this->email); return 'https://www.gravatar.com/avatar/' . $hash . '?s=45'; } /** * @return \yii\db\ActiveQuery */ public function getHistories() { return $this->hasMany(History::className(), ['user_id' => 'id'])->inverseOf('user'); } }