[php] New user model

Viewer

copydownloadembedprintName: New user model
  1. <?php
  2.  
  3. namespace common\models;
  4.  
  5. use common\models\query\UserQuery;
  6. use Yii;
  7. use yii\base\NotSupportedException;
  8. use yii\behaviors\TimestampBehavior;
  9. use yii\db\ActiveRecord;
  10. use yii\helpers\FileHelper;
  11. use yii\imagine\Image;
  12. use yii\web\IdentityInterface;
  13.  
  14. /**
  15.  * This is the model class for table "user".
  16.  *
  17.  * @property int $id
  18.  * @property string $username
  19.  * @property string|null $fullname
  20.  * @property string|null $description
  21.  * @property string|null $age
  22.  * @property string|null $website
  23.  * @property string|null $phone
  24.  * @property string|null $city
  25.  * @property string|null $degree
  26.  * @property int|null $is_freelance
  27.  * @property string|null $picture
  28.  * @property string $auth_key
  29.  * @property string $password_hash
  30.  * @property string|null $password_reset_token
  31.  * @property string $email
  32.  * @property int $status
  33.  * @property int $created_at
  34.  * @property int $updated_at
  35.  * @property string|null $verification_token
  36.  */
  37. class User extends ActiveRecord implements IdentityInterface {
  38.  
  39.     const STATUS_DELETED = 0;
  40.     const STATUS_INACTIVE = 9;
  41.     const STATUS_ACTIVE = 10;
  42.     const FREELANCE_AVAILABLE = 'Available';
  43.     const FREELANCE_AWAY = 'Away';
  44.  
  45.     public $profilePicture; //i.e. if we don't want to create a separate DB table field for saving/keeping picture record. Then we can do this by creating a class attribute for keeping the picture record
  46.  
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public static function tableName() {
  51.         return 'user';
  52.     }
  53.  
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function behaviors() {
  58.         return [
  59.             TimestampBehavior::className(),
  60.         ];
  61.     }
  62.  
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function rules() {
  67.         return [
  68. //            [['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
  69.             [['description'], 'string'],
  70. //            [['age'], 'date', 'format' => 'dd-MM-yyyy'],
  71.             [['status', 'created_at', 'updated_at'], 'integer'],
  72.             [['username', 'password_hash', 'password_reset_token', 'email', 'verification_token'], 'string', 'max' => 255],
  73.             [['fullname', 'facebook', 'twitter', 'instagram', 'skype', 'linkedin' /*, 'picture'*/], 'string', 'max' => 64],
  74.             [['is_freelance', 'website', 'city', 'auth_key', 'phone', 'age', 'degree'], 'string', 'max' => 32],
  75.             ['status', 'default', 'value' => self::STATUS_INACTIVE],
  76.             ['is_freelance', 'default', 'value' => self::FREELANCE_AWAY],
  77.             ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
  78.             ['picture', 'image', /*'extensions' => 'jpg',*/ 'minWidth' => '1280'],
  79.         ];
  80.     }
  81.  
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function attributeLabels() {
  86.         return [
  87.             'id' => 'ID',
  88.             'username' => 'Username',
  89.             'fullname' => 'Fullname',
  90.             'description' => 'Description',
  91.             'age' => 'Age',
  92.             'website' => 'Website',
  93.             'phone' => 'Phone',
  94.             'city' => 'City',
  95.             'degree' => 'Degree',
  96.             'is_freelance' => 'Is Freelance',
  97.             'picture' => 'Picture',
  98.             'auth_key' => 'Auth Key',
  99.             'password_hash' => 'Password Hash',
  100.             'password_reset_token' => 'Password Reset Token',
  101.             'email' => 'Email',
  102.             'status' => 'Status',
  103.             'created_at' => 'Created At',
  104.             'updated_at' => 'Updated At',
  105.             'verification_token' => 'Verification Token',
  106.             'facebook' => 'Facebook ID',
  107.             'twitter' => 'Twitter ID',
  108.             'instagram' => 'Instagram ID',
  109.             'skype' => 'Skype ID',
  110.             'linkedin' => 'LinkedIn ID',
  111.         ];
  112.     }
  113.  
  114.     public function getStatus() {
  115.         return [
  116.             self::STATUS_ACTIVE => 'Active',
  117.             self::STATUS_INACTIVE => 'Inactive',
  118.             self::STATUS_DELETED => 'Deleted',
  119.         ];
  120.     }
  121.  
  122.     public function getFreelancestatus() {
  123.         return [
  124.             self::FREELANCE_AVAILABLE => 'Available',
  125.             self::FREELANCE_AWAY => 'Away',
  126.         ];
  127.     }
  128.     
  129.     /**
  130.      * {@inheritdoc}
  131.      */
  132.     public static function findIdentity($id) {
  133.         return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  134.     }
  135.  
  136.     /**
  137.      * {@inheritdoc}
  138.      */
  139.     public static function findIdentityByAccessToken($token, $type = null) {
  140.         throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  141.     }
  142.  
  143.     /**
  144.      * Finds user by username
  145.      *
  146.      * @param string $username
  147.      * @return static|null
  148.      */
  149.     public static function findByUsername($username) {
  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.         if (!static::isPasswordResetTokenValid($token)) {
  161.             return null;
  162.         }
  163.  
  164.         return static::findOne([
  165.                     'password_reset_token' => $token,
  166.                     'status' => self::STATUS_ACTIVE,
  167.         ]);
  168.     }
  169.  
  170.     /**
  171.      * Finds user by verification email token
  172.      *
  173.      * @param string $token verify email token
  174.      * @return static|null
  175.      */
  176.     public static function findByVerificationToken($token) {
  177.         return static::findOne([
  178.                     'verification_token' => $token,
  179.                     'status' => self::STATUS_INACTIVE
  180.         ]);
  181.     }
  182.  
  183.     /**
  184.      * Finds out if password reset token is valid
  185.      *
  186.      * @param string $token password reset token
  187.      * @return bool
  188.      */
  189.     public static function isPasswordResetTokenValid($token) {
  190.         if (empty($token)) {
  191.             return false;
  192.         }
  193.  
  194.         $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  195.         $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  196.         return $timestamp + $expire >= time();
  197.     }
  198.  
  199.     /**
  200.      * {@inheritdoc}
  201.      */
  202.     public function getId() {
  203.         return $this->getPrimaryKey();
  204.     }
  205.  
  206.     /**
  207.      * {@inheritdoc}
  208.      */
  209.     public function getAuthKey() {
  210.         return $this->auth_key;
  211.     }
  212.  
  213.     /**
  214.      * {@inheritdoc}
  215.      */
  216.     public function validateAuthKey($authKey) {
  217.         return $this->getAuthKey() === $authKey;
  218.     }
  219.  
  220.     /**
  221.      * Validates password
  222.      *
  223.      * @param string $password password to validate
  224.      * @return bool if password provided is valid for current user
  225.      */
  226.     public function validatePassword($password) {
  227.         return Yii::$app->security->validatePassword($password, $this->password_hash);
  228.     }
  229.  
  230.     /**
  231.      * Generates password hash from password and sets it to the model
  232.      *
  233.      * @param string $password
  234.      */
  235.     public function setPassword($password) {
  236.         $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  237.     }
  238.  
  239.     /**
  240.      * Generates "remember me" authentication key
  241.      */
  242.     public function generateAuthKey() {
  243.         $this->auth_key = Yii::$app->security->generateRandomString();
  244.     }
  245.  
  246.     /**
  247.      * Generates new password reset token
  248.      */
  249.     public function generatePasswordResetToken() {
  250.         $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  251.     }
  252.  
  253.     /**
  254.      * Generates new token for email verification
  255.      */
  256.     public function generateEmailVerificationToken() {
  257.         $this->verification_token = Yii::$app->security->generateRandomString() . '_' . time();
  258.     }
  259.  
  260.     /**
  261.      * Removes password reset token
  262.      */
  263.     public function removePasswordResetToken() {
  264.         $this->password_reset_token = null;
  265.     }
  266.  
  267.     /**
  268.      * {@inheritdoc}
  269.      * @return UserQuery the active query used by this AR class.
  270.      */
  271.     public static function find() {
  272.         return new UserQuery(get_called_class());
  273.     }
  274.  
  275.  
  276. }
  277.  

Editor

You can edit this paste and save as new:


File Description
  • New user model
  • Paste Code
  • 18 Feb-2022
  • 8.07 Kb
You can Share it: