[php] Yii built-in User model

Viewer

copydownloadembedprintName: Yii built-in User model
  1. <?php
  2.  
  3. namespace common\models;
  4.  
  5. use Yii;
  6. use yii\base\NotSupportedException;
  7. use yii\behaviors\TimestampBehavior;
  8. use yii\db\ActiveRecord;
  9. use yii\web\IdentityInterface;
  10.  
  11. /**
  12.  * User model
  13.  *
  14.  * @property integer $id
  15.  * @property string $username
  16.  * @property string $password_hash
  17.  * @property string $password_reset_token
  18.  * @property string $verification_token
  19.  * @property string $email
  20.  * @property string $auth_key
  21.  * @property integer $status
  22.  * @property integer $created_at
  23.  * @property integer $updated_at
  24.  * @property string $password write-only password
  25.  */
  26. class User extends ActiveRecord implements IdentityInterface
  27. {
  28.     const STATUS_DELETED = 0;
  29.     const STATUS_INACTIVE = 9;
  30.     const STATUS_ACTIVE = 10;
  31.  
  32.  
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public static function tableName()
  37.     {
  38.         return '{{%user}}';
  39.     }
  40.  
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function behaviors()
  45.     {
  46.         return [
  47.             TimestampBehavior::className(),
  48.         ];
  49.     }
  50.  
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function rules()
  55.     {
  56.         return [
  57.             ['status', 'default', 'value' => self::STATUS_INACTIVE],
  58.             ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
  59.         ];
  60.     }
  61.  
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public static function findIdentity($id)
  66.     {
  67.         return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  68.     }
  69.  
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public static function findIdentityByAccessToken($token, $type = null)
  74.     {
  75.         throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  76.     }
  77.  
  78.     /**
  79.      * Finds user by username
  80.      *
  81.      * @param string $username
  82.      * @return static|null
  83.      */
  84.     public static function findByUsername($username)
  85.     {
  86.         return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  87.     }
  88.  
  89.     /**
  90.      * Finds user by password reset token
  91.      *
  92.      * @param string $token password reset token
  93.      * @return static|null
  94.      */
  95.     public static function findByPasswordResetToken($token)
  96.     {
  97.         if (!static::isPasswordResetTokenValid($token)) {
  98.             return null;
  99.         }
  100.  
  101.         return static::findOne([
  102.             'password_reset_token' => $token,
  103.             'status' => self::STATUS_ACTIVE,
  104.         ]);
  105.     }
  106.  
  107.     /**
  108.      * Finds user by verification email token
  109.      *
  110.      * @param string $token verify email token
  111.      * @return static|null
  112.      */
  113.     public static function findByVerificationToken($token) {
  114.         return static::findOne([
  115.             'verification_token' => $token,
  116.             'status' => self::STATUS_INACTIVE
  117.         ]);
  118.     }
  119.  
  120.     /**
  121.      * Finds out if password reset token is valid
  122.      *
  123.      * @param string $token password reset token
  124.      * @return bool
  125.      */
  126.     public static function isPasswordResetTokenValid($token)
  127.     {
  128.         if (empty($token)) {
  129.             return false;
  130.         }
  131.  
  132.         $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  133.         $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  134.         return $timestamp + $expire >= time();
  135.     }
  136.  
  137.     /**
  138.      * {@inheritdoc}
  139.      */
  140.     public function getId()
  141.     {
  142.         return $this->getPrimaryKey();
  143.     }
  144.  
  145.     /**
  146.      * {@inheritdoc}
  147.      */
  148.     public function getAuthKey()
  149.     {
  150.         return $this->auth_key;
  151.     }
  152.  
  153.     /**
  154.      * {@inheritdoc}
  155.      */
  156.     public function validateAuthKey($authKey)
  157.     {
  158.         return $this->getAuthKey() === $authKey;
  159.     }
  160.  
  161.     /**
  162.      * Validates password
  163.      *
  164.      * @param string $password password to validate
  165.      * @return bool if password provided is valid for current user
  166.      */
  167.     public function validatePassword($password)
  168.     {
  169.         return Yii::$app->security->validatePassword($password, $this->password_hash);
  170.     }
  171.  
  172.     /**
  173.      * Generates password hash from password and sets it to the model
  174.      *
  175.      * @param string $password
  176.      */
  177.     public function setPassword($password)
  178.     {
  179.         $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  180.     }
  181.  
  182.     /**
  183.      * Generates "remember me" authentication key
  184.      */
  185.     public function generateAuthKey()
  186.     {
  187.         $this->auth_key = Yii::$app->security->generateRandomString();
  188.     }
  189.  
  190.     /**
  191.      * Generates new password reset token
  192.      */
  193.     public function generatePasswordResetToken()
  194.     {
  195.         $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  196.     }
  197.  
  198.     /**
  199.      * Generates new token for email verification
  200.      */
  201.     public function generateEmailVerificationToken()
  202.     {
  203.         $this->verification_token = Yii::$app->security->generateRandomString() . '_' . time();
  204.     }
  205.  
  206.     /**
  207.      * Removes password reset token
  208.      */
  209.     public function removePasswordResetToken()
  210.     {
  211.         $this->password_reset_token = null;
  212.     }
  213. }
  214.  

Editor

You can edit this paste and save as new:


File Description
  • Yii built-in User model
  • Paste Code
  • 18 Feb-2022
  • 5.04 Kb
You can Share it: