Advertisement
CalcioNit

UserModel.php

Oct 21st, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.79 KB | None | 0 0
  1. <?php
  2. namespace common\models;
  3.  
  4. use Yii;
  5. use yii\base\NotSupportedException;
  6. use yii\behaviors\TimestampBehavior;
  7. use yii\db\ActiveRecord;
  8. use yii\web\IdentityInterface;
  9.  
  10. /**
  11.  * User model
  12.  *
  13.  * @property integer $id
  14.  * @property string $username
  15.  * @property string $password_hash
  16.  * @property string $password_reset_token
  17.  * @property string $verification_token
  18.  * @property string $email
  19.  * @property string $auth_key
  20.  * @property integer $status
  21.  * @property integer $created_at
  22.  * @property integer $updated_at
  23.  * @property string $password write-only password
  24.  */
  25. class User extends ActiveRecord implements IdentityInterface
  26. {
  27.     const STATUS_DELETED = 0;
  28.     const STATUS_INACTIVE = 9;
  29.     const STATUS_ACTIVE = 10;
  30.     const STATUS_DELETED_STRING = 'Deleted';
  31.     const STATUS_INACTIVE_STRING = 'Inactive';
  32.     const STATUS_ACTIVE_STRING = 'Active';
  33.  
  34.  
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     public static function tableName()
  39.     {
  40.         return '{{%user}}';
  41.     }
  42.  
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public function behaviors()
  47.     {
  48.         return [
  49.             TimestampBehavior::className(),
  50.         ];
  51.     }
  52.  
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function rules()
  57.     {
  58.         return [
  59.             [['username', 'email'], 'trim'],
  60.             [['username', 'password_hash', 'email', 'status'] , 'required'],
  61.             [['username', 'email'], 'unique'],
  62.             ['username', 'string', 'min' => 2, 'max' => 255],
  63.             ['email', 'email'],
  64.             ['email', 'string', 'max' => 255],
  65.             ['password_hash', 'string', 'min' => 6],
  66.             ['status', 'default', 'value' => self::STATUS_ACTIVE],
  67.             ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
  68.         ];
  69.     }
  70.  
  71.     public function scenarios()
  72.     {
  73.         $scenarios = parent::scenarios();
  74.         $scenarios['update'] = ['username', 'email', 'status'];
  75.         $scenarios['change-password'] = ['password_hash'];
  76.  
  77.         return $scenarios;
  78.     }
  79.  
  80.     /**
  81.      * {@inheritdoc}
  82.      */
  83.     public function attributeLabels()
  84.     {
  85.         return [
  86.             'id' => Yii::t('app', 'ID'),
  87.             'username' => Yii::t('app', 'Username'),
  88.             'password_hash' => Yii::t('app', 'Password'),
  89.             'email' => Yii::t('app', 'Email'),
  90.             'status' => Yii::t('app', 'Status'),
  91.         ];
  92.     }
  93.  
  94.     public function getUserStatus()
  95.     {
  96.         return [
  97.             self::STATUS_ACTIVE => self::STATUS_ACTIVE_STRING,
  98.             self::STATUS_INACTIVE => self::STATUS_INACTIVE_STRING,
  99.             self::STATUS_DELETED => self::STATUS_DELETED_STRING,
  100.         ];
  101.     }
  102.  
  103.     public function createUser()
  104.     {
  105.         if (!$this->validate()) {
  106.             return null;
  107.         }
  108.  
  109.         $this->setPassword($this->password_hash);
  110.         $this->generateAuthKey();
  111.  
  112.         return $this->save();
  113.     }
  114.  
  115.     public function changePassword()
  116.     {
  117.         if (!$this->validate()) {
  118.             return null;
  119.         }
  120.  
  121.         $this->setPassword($this->password_hash);
  122.  
  123.         return $this->save();
  124.     }
  125.  
  126.     /**
  127.      * {@inheritdoc}
  128.      */
  129.     public static function findIdentity($id)
  130.     {
  131.         return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  132.     }
  133.  
  134.     /**
  135.      * {@inheritdoc}
  136.      */
  137.     public static function findIdentityByAccessToken($token, $type = null)
  138.     {
  139.         throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  140.     }
  141.  
  142.     /**
  143.      * Finds user by username
  144.      *
  145.      * @param string $username
  146.      * @return static|null
  147.      */
  148.     public static function findByUsername($username)
  149.     {
  150.         return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  151.     }
  152.  
  153.     /**
  154.      * Finds user by password reset token
  155.      *
  156.      * @param string $token password reset token
  157.      * @return static|null
  158.      */
  159.     public static function findByPasswordResetToken($token)
  160.     {
  161.         if (!static::isPasswordResetTokenValid($token)) {
  162.             return null;
  163.         }
  164.  
  165.         return static::findOne([
  166.             'password_reset_token' => $token,
  167.             'status' => self::STATUS_ACTIVE,
  168.         ]);
  169.     }
  170.  
  171.     /**
  172.      * Finds user by verification email token
  173.      *
  174.      * @param string $token verify email token
  175.      * @return static|null
  176.      */
  177.     public static function findByVerificationToken($token) {
  178.         return static::findOne([
  179.             'verification_token' => $token,
  180.             'status' => self::STATUS_INACTIVE
  181.         ]);
  182.     }
  183.  
  184.     /**
  185.      * Finds out if password reset token is valid
  186.      *
  187.      * @param string $token password reset token
  188.      * @return bool
  189.      */
  190.     public static function isPasswordResetTokenValid($token)
  191.     {
  192.         if (empty($token)) {
  193.             return false;
  194.         }
  195.  
  196.         $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  197.         $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  198.         return $timestamp + $expire >= time();
  199.     }
  200.  
  201.     /**
  202.      * {@inheritdoc}
  203.      */
  204.     public function getId()
  205.     {
  206.         return $this->getPrimaryKey();
  207.     }
  208.  
  209.     /**
  210.      * {@inheritdoc}
  211.      */
  212.     public function getAuthKey()
  213.     {
  214.         return $this->auth_key;
  215.     }
  216.  
  217.     /**
  218.      * {@inheritdoc}
  219.      */
  220.     public function validateAuthKey($authKey)
  221.     {
  222.         return $this->getAuthKey() === $authKey;
  223.     }
  224.  
  225.     /**
  226.      * Validates password
  227.      *
  228.      * @param string $password password to validate
  229.      * @return bool if password provided is valid for current user
  230.      */
  231.     public function validatePassword($password)
  232.     {
  233.         return Yii::$app->security->validatePassword($password, $this->password_hash);
  234.     }
  235.  
  236.     /**
  237.      * Generates password hash from password and sets it to the model
  238.      *
  239.      * @param string $password
  240.      */
  241.     public function setPassword($password)
  242.     {
  243.         $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  244.     }
  245.  
  246.     /**
  247.      * Generates "remember me" authentication key
  248.      */
  249.     public function generateAuthKey()
  250.     {
  251.         $this->auth_key = Yii::$app->security->generateRandomString();
  252.     }
  253.  
  254.     /**
  255.      * Generates new password reset token
  256.      */
  257.     public function generatePasswordResetToken()
  258.     {
  259.         $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  260.     }
  261.  
  262.     public function generateEmailVerificationToken()
  263.     {
  264.         $this->verification_token = Yii::$app->security->generateRandomString() . '_' . time();
  265.     }
  266.  
  267.     /**
  268.      * Removes password reset token
  269.      */
  270.     public function removePasswordResetToken()
  271.     {
  272.         $this->password_reset_token = null;
  273.     }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement