Class common\models\User
Inheritance | common\models\User » yii\db\ActiveRecord |
---|---|
Implements | yii\web\IdentityInterface |
Source Code | https://github.com/yiisoft/yii2-app-advanced/blob/master/common/models/User.php |
User model
Public Properties
Public Methods
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
STATUS_ACTIVE | 10 | common\models\User | |
STATUS_DELETED | 0 | common\models\User | |
STATUS_INACTIVE | 9 | common\models\User |
Property Details
Method Details
Finds user by password reset token
public static static|null findByPasswordResetToken ( $token ) | ||
$token | string |
Password reset token |
public static function findByPasswordResetToken($token)
{
if (!static::isPasswordResetTokenValid($token)) {
return null;
}
return static::findOne([
'password_reset_token' => $token,
'status' => self::STATUS_ACTIVE,
]);
}
Finds user by username
public static static|null findByUsername ( $username ) | ||
$username | string |
public static function findByUsername($username)
{
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
}
Finds user by verification email token
public static static|null findByVerificationToken ( $token ) | ||
$token | string |
Verify email token |
public static function findByVerificationToken($token) {
return static::findOne([
'verification_token' => $token,
'status' => self::STATUS_INACTIVE
]);
}
public static void findIdentity ( $id ) | ||
$id |
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
public static void findIdentityByAccessToken ( $token, $type = null ) | ||
$token | ||
$type |
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
Generates "remember me" authentication key
public void generateAuthKey ( ) |
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
Generates new token for email verification
public void generateEmailVerificationToken ( ) |
public function generateEmailVerificationToken()
{
$this->verification_token = Yii::$app->security->generateRandomString() . '_' . time();
}
Generates new password reset token
public void generatePasswordResetToken ( ) |
public function generatePasswordResetToken()
{
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}
Finds out if password reset token is valid
public static boolean isPasswordResetTokenValid ( $token ) | ||
$token | string |
Password reset token |
public static function isPasswordResetTokenValid($token)
{
if (empty($token)) {
return false;
}
$timestamp = (int) substr($token, strrpos($token, '_') + 1);
$expire = Yii::$app->params['user.passwordResetTokenExpire'];
return $timestamp + $expire >= time();
}
Removes password reset token
public void removePasswordResetToken ( ) |
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
public void rules ( ) |
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_INACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
];
}
Generates password hash from password and sets it to the model
public void setPassword ( $password ) | ||
$password | string |
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
public void validateAuthKey ( $authKey ) | ||
$authKey |
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
Validates password
public boolean validatePassword ( $password ) | ||
$password | string |
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);
}