Class common\models\User

Inheritancecommon\models\User » yii\db\ActiveRecord
Implementsyii\web\IdentityInterface
Source Code https://github.com/yiisoft/yii2-app-advanced/blob/master/common/models/User.php

User model

Public Methods

Hide inherited methods

Method Description Defined By
behaviors() common\models\User
findByPasswordResetToken() Finds user by password reset token common\models\User
findByUsername() Finds user by username common\models\User
findByVerificationToken() Finds user by verification email token common\models\User
findIdentity() common\models\User
findIdentityByAccessToken() common\models\User
generateAuthKey() Generates "remember me" authentication key common\models\User
generateEmailVerificationToken() Generates new token for email verification common\models\User
generatePasswordResetToken() Generates new password reset token common\models\User
getAuthKey() common\models\User
getId() common\models\User
isPasswordResetTokenValid() Finds out if password reset token is valid common\models\User
removePasswordResetToken() Removes password reset token common\models\User
rules() common\models\User
setPassword() Generates password hash from password and sets it to the model common\models\User
tableName() common\models\User
validateAuthKey() common\models\User
validatePassword() Validates password common\models\User

Constants

Hide inherited 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

Hide inherited properties

$auth_key public property
public string $auth_key null
$created_at public property
public integer $created_at null
$email public property
public string $email null
$id public property
public integer $id null
$password public property

write-only password

public string $password null
$password_hash public property
public string $password_hash null
$password_reset_token public property
$status public property
public integer $status null
$updated_at public property
public integer $updated_at null
$username public property
public string $username null
$verification_token public property

Method Details

Hide inherited methods

behaviors() public method

public void behaviors ( )

                public function behaviors()
{
    return [
        TimestampBehavior::class,
    ];
}

            
findByPasswordResetToken() public static method

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,
    ]);
}

            
findByUsername() public static method

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]);
}

            
findByVerificationToken() public static method

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
    ]);
}

            
findIdentity() public static method

public static void findIdentity ( $id )
$id

                public static function findIdentity($id)
{
    return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}

            
findIdentityByAccessToken() public static method

public static void findIdentityByAccessToken ( $token, $type null )
$token
$type

                public static function findIdentityByAccessToken($token, $type = null)
{
    throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}

            
generateAuthKey() public method

Generates "remember me" authentication key

public void generateAuthKey ( )

                public function generateAuthKey()
{
    $this->auth_key = Yii::$app->security->generateRandomString();
}

            
generateEmailVerificationToken() public method

Generates new token for email verification

public void generateEmailVerificationToken ( )

                public function generateEmailVerificationToken()
{
    $this->verification_token = Yii::$app->security->generateRandomString() . '_' . time();
}

            
generatePasswordResetToken() public method

Generates new password reset token

public void generatePasswordResetToken ( )

                public function generatePasswordResetToken()
{
    $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}

            
getAuthKey() public method

public void getAuthKey ( )

                public function getAuthKey()
{
    return $this->auth_key;
}

            
getId() public method

public void getId ( )

                public function getId()
{
    return $this->getPrimaryKey();
}

            
isPasswordResetTokenValid() public static method

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();
}

            
removePasswordResetToken() public method

Removes password reset token

public void removePasswordResetToken ( )

                public function removePasswordResetToken()
{
    $this->password_reset_token = null;
}

            
rules() public method

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]],
    ];
}

            
setPassword() public method

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);
}

            
tableName() public static method

public static void tableName ( )

                public static function tableName()
{
    return '{{%user}}';
}

            
validateAuthKey() public method

public void validateAuthKey ( $authKey )
$authKey

                public function validateAuthKey($authKey)
{
    return $this->getAuthKey() === $authKey;
}

            
validatePassword() public method

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);
}