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 findByPasswordResetToken( string $token ): common\models\User|null | ||
| $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 findByUsername( string $username ): common\models\User|null | ||
| $username | string | |
public static function findByUsername($username)
{
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
}
Finds user by verification email token
| public static findByVerificationToken( string $token ): common\models\User|null | ||
| $token | string |
Verify email token |
public static function findByVerificationToken($token)
{
return static::findOne([
'verification_token' => $token,
'status' => self::STATUS_INACTIVE
]);
}
| public static findIdentity( mixed $id ): | ||
| $id | mixed | |
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
| public static findIdentityByAccessToken( mixed $token, mixed $type = null ): | ||
| $token | mixed | |
| $type | mixed | |
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
Generates "remember me" authentication key
| public generateAuthKey( ): mixed |
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
Generates new token for email verification
| public generateEmailVerificationToken( ): mixed |
public function generateEmailVerificationToken()
{
$this->verification_token = Yii::$app->security->generateRandomString() . '_' . time();
}
Generates new password reset token
| public generatePasswordResetToken( ): mixed |
public function generatePasswordResetToken()
{
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}
Finds out if password reset token is valid
| public static isPasswordResetTokenValid( string $token ): boolean | ||
| $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 removePasswordResetToken( ): mixed |
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
| public 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 setPassword( string $password ): mixed | ||
| $password | string | |
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
| public validateAuthKey( mixed $authKey ): | ||
| $authKey | mixed | |
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
Validates password
| public validatePassword( string $password ): boolean | ||
| $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);
}