Advertisement
kissarat

Yii2 User model

Jun 10th, 2015
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.71 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 write-only password
  16.  * @property string $password_hash
  17.  * @property string $password_reset_token
  18.  * @property string $auth_key
  19.  * @property integer $status
  20.  * @property integer $created_at
  21.  * @property integer $updated_at
  22.  * @property string $email
  23.  * @property string $first
  24.  * @property string $last
  25.  * @property integer $phone
  26.  * @property string $skype
  27.  * @property string $perfect
  28.  */
  29. class User extends ActiveRecord implements IdentityInterface
  30. {
  31.     const STATUS_DELETED = 0;
  32.     const STATUS_ACTIVE = 10;
  33.  
  34.     /**
  35.      * @inheritdoc
  36.      */
  37.     public static function tableName()
  38.     {
  39.         return '{{%user}}';
  40.     }
  41.  
  42.     /**
  43.      * @inheritdoc
  44.      */
  45.     public function behaviors()
  46.     {
  47.         return [
  48.             TimestampBehavior::className(),
  49.         ];
  50.     }
  51.  
  52.     /**
  53.      * @inheritdoc
  54.      */
  55.     public function rules()
  56.     {
  57.         return [
  58.             [['username', 'email', 'perfect'], 'required'],
  59.             ['username', 'maxlength' => 32],
  60.             ['email', 'email'],
  61.             [['first', 'last'], 'match', 'pattern' => '/^[а-яъёы\']+$/i'],
  62.             ['skype', 'match', 'pattern' => '/^[a-z][a-z0-9\.,\-_]{5,31}+$/i'],
  63.             ['phone', 'number'],
  64.             ['skype', 'match', 'pattern' => '/^[а-яъёы\']+$/i'],
  65.             ['perfect', 'match', 'pattern' => '/^U\d{7}$/', 'message' => 'Формат должен быть U1234567'],
  66.             ['status', 'default', 'value' => self::STATUS_ACTIVE],
  67.             ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  68.         ];
  69.     }
  70.  
  71.     public function attributeLabels() {
  72.         return [
  73.             'id' => 'ID',
  74.             'username' => Yii::t('app', 'Username'),
  75.             'email' => Yii::t('app', 'Email'),
  76.             'first' => Yii::t('app', 'Имя'),
  77.             'last' => Yii::t('app', 'Отчество'),
  78.             'phone' => Yii::t('app', 'Телефон'),
  79.             'skype' => Yii::t('app', 'Skype'),
  80.             'perfect' => Yii::t('app', 'Perfect Money wallet'),
  81.             'account' => Yii::t('app', 'Account'),
  82.             'created_at' => Yii::t('app', 'Created'),
  83.             'updated_at' => Yii::t('app', 'Updated')
  84.         ];
  85.     }
  86.  
  87.     public function __toString() {
  88.         return $this->username;
  89.     }
  90.  
  91.     /**
  92.      * @inheritdoc
  93.      */
  94.     public static function findIdentity($id)
  95.     {
  96.         return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  97.     }
  98.  
  99.     /**
  100.      * @inheritdoc
  101.      */
  102.     public static function findIdentityByAccessToken($token, $type = null)
  103.     {
  104.         throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  105.     }
  106.  
  107.     /**
  108.      * Finds user by username
  109.      *
  110.      * @param string $username
  111.      * @return static|null
  112.      */
  113.     public static function findByUsername($username)
  114.     {
  115.         return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  116.     }
  117.  
  118.     /**
  119.      * Finds user by password reset token
  120.      *
  121.      * @param string $token password reset token
  122.      * @return static|null
  123.      */
  124.     public static function findByPasswordResetToken($token)
  125.     {
  126.         if (!static::isPasswordResetTokenValid($token)) {
  127.             return null;
  128.         }
  129.  
  130.         return static::findOne([
  131.             'password_reset_token' => $token,
  132.             'status' => self::STATUS_ACTIVE,
  133.         ]);
  134.     }
  135.  
  136.     /**
  137.      * Finds out if password reset token is valid
  138.      *
  139.      * @param string $token password reset token
  140.      * @return boolean
  141.      */
  142.     public static function isPasswordResetTokenValid($token)
  143.     {
  144.         if (empty($token)) {
  145.             return false;
  146.         }
  147.         $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  148.         $parts = explode('_', $token);
  149.         $timestamp = (int) end($parts);
  150.         return $timestamp + $expire >= time();
  151.     }
  152.  
  153.     /**
  154.      * @inheritdoc
  155.      */
  156.     public function getId()
  157.     {
  158.         return $this->id;
  159.     }
  160.  
  161.     /**
  162.      * @inheritdoc
  163.      */
  164.     public function getAuthKey()
  165.     {
  166.         return $this->auth_key;
  167.     }
  168.  
  169.     /**
  170.      * @inheritdoc
  171.      */
  172.     public function validateAuthKey($authKey)
  173.     {
  174.         return $this->getAuthKey() === $authKey;
  175.     }
  176.  
  177.     /**
  178.      * Validates password
  179.      *
  180.      * @param string $password password to validate
  181.      * @return boolean if password provided is valid for current user
  182.      */
  183.     public function validatePassword($password)
  184.     {
  185.         return Yii::$app->security->validatePassword($password, $this->password_hash);
  186.     }
  187.  
  188.     /**
  189.      * Generates password hash from password and sets it to the model
  190.      *
  191.      * @param string $password
  192.      */
  193.     public function setPassword($password)
  194.     {
  195.         $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  196.     }
  197.  
  198.     /**
  199.      * Generates "remember me" authentication key
  200.      */
  201.     public function generateAuthKey()
  202.     {
  203.         $this->auth_key = Yii::$app->security->generateRandomString();
  204.     }
  205.  
  206.     /**
  207.      * Generates new password reset token
  208.      */
  209.     public function generatePasswordResetToken()
  210.     {
  211.         $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  212.     }
  213.  
  214.     /**
  215.      * Removes password reset token
  216.      */
  217.     public function removePasswordResetToken()
  218.     {
  219.         $this->password_reset_token = null;
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement