value * @return static|null the newly created model, or null on failure */ public static function create($attributes) { /** @var User $user */ $user = new static(); $user->setAttributes($attributes); $user->setPassword($attributes['password']); $user->generateAuthKey(); if ($user->save()) { return $user; } else { return null; } } /** * @inheritdoc */ public function behaviors() { return [ 'timestamp' => [ 'class' => 'yii\behaviors\TimestampBehavior', 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], ], ], ]; } /** * @inheritdoc */ public static function findIdentity($id) { return static::find($id); } /** * Finds user by username * * @param string $username * @return static|null */ public static function findByUsername($username) { return static::find(['username' => $username, 'status' => self::STATUS_ACTIVE]); } /** * Finds user by password reset token * * @param string $token password reset token * @return static|null */ public static function findByPasswordResetToken($token) { $expire = \Yii::$app->params['user.passwordResetTokenExpire']; $parts = explode('_', $token); $timestamp = (int)end($parts); if ($timestamp + $expire < time()) { // token expired return null; } return static::find([ 'password_reset_token' => $token, '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 bool if password provided is valid for current user */ public function validatePassword($password) { return 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 = Security::generatePasswordHash($password); } /** * Generates "remember me" authentication key */ public function generateAuthKey() { $this->auth_key = Security::generateRandomKey(); } /** * Generates new password reset token */ public function generatePasswordResetToken() { $this->password_reset_token = Security::generateRandomKey() . '_' . time(); } /** * Removes password reset token */ public function removePasswordResetToken() { $this->password_reset_token = null; } /** * @inheritdoc */ public function rules() { return [ ['status', 'default', 'value' => self::STATUS_ACTIVE], ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], ['role', 'default', 'value' => self::ROLE_USER], ['role', 'in', 'range' => [self::ROLE_USER]], ['username', 'filter', 'filter' => 'trim'], ['username', 'required'], ['username', 'string', 'min' => 2, 'max' => 255], ['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'unique'], ]; } }