Advertisement
Guest User

Untitled

a guest
May 7th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.55 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. use Yii;
  6. use yii\base\NotSupportedException;
  7. use yii\behaviors\TimestampBehavior;
  8. use yii\mongodb\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 $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.  * @property AuthAssignment[] $authAssignments
  25.  * @property AuthItem[] $itemNames
  26.  */
  27.  
  28. class User extends ActiveRecord implements IdentityInterface
  29. {
  30.     const STATUS_DELETED = 0;
  31.     const STATUS_ACTIVE = 10;
  32.     const ROLE_USER = 'user';
  33.     const ROLE_ADMIN = 'admin';
  34.  
  35.     public $currentPassword;
  36.     public $newPassword;
  37.     public $newPasswordConfirm;
  38.     public $perfil;
  39.     public $perfil_array = [];
  40.  
  41.     /**
  42.      * @inheritdoc
  43.      */
  44.     public static function collectionName()
  45.     {
  46.         return ['mobiledata_db', 'user'];
  47.     }
  48.  
  49.     /**
  50.      * @inheritdoc
  51.      */
  52.     public function behaviors()
  53.     {
  54.         return [
  55.             TimestampBehavior::className(),
  56.             'attributes' => [
  57.                 ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
  58.                 ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
  59.             ],
  60.         ];
  61.     }
  62.  
  63.     public function attributes()
  64.     {
  65.         return [
  66.             '_id',
  67.             'first_name',
  68.             'last_name',
  69.             'username',
  70.             'password',
  71.             'password_hash',
  72.             'status',
  73.             'created_at',
  74.             'updated_at',
  75.             'email'
  76.         ];
  77.     }
  78.  
  79.     /**
  80.      * @inheritdoc
  81.      */
  82.     public function rules()
  83.     {
  84.         return [
  85.             ['status', 'default', 'value' => self::STATUS_ACTIVE],
  86.             ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  87.             ['role', 'default', 'value' => [self::ROLE_USER]],
  88.             ['role', 'in', 'range' => [self::ROLE_USER, self::ROLE_ADMIN]],
  89.         ];
  90.     }
  91.  
  92.     public function validateCurrentPassword()
  93.     {
  94.         if (!$this->verifyPassword($this->currentPassword)) {
  95.             $this->addError("currentPassword", "Palavra passe atual incorreto");
  96.         }
  97.     }
  98.  
  99.     public function verifyPassword($password)
  100.     {
  101.         $dbPassword = static::findOne(['username' => Yii::$app->user->identity->username, 'status' => self::STATUS_ACTIVE])->password_hash;
  102.  
  103.         return Yii::$app->security->validatePassword($password, $dbPassword);
  104.     }
  105.  
  106.     /**
  107.      * @inheritdoc
  108.      */
  109.     public static function findIdentity($id)
  110.     {
  111.         return static::findOne($id);
  112.     }
  113.  
  114.     /**
  115.      * @inheritdoc
  116.      */
  117.     public static function findIdentityByAccessToken($token, $type = null)
  118.     {
  119.         throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  120.        
  121.     }
  122.  
  123.     /**
  124.      * Finds user by username
  125.      *
  126.      * @param string $username
  127.      * @return static|null
  128.      */
  129.     public static function findByUsername($username)
  130.     {
  131.        return static::/*getCollection()->*/findOne(['username' => $username, 'status' => 1]);
  132.     }
  133.  
  134.     /**
  135.      * @inheritdoc
  136.      */
  137.     public function getId()
  138.     {
  139.         return $this->getPrimaryKey();
  140.     }
  141.  
  142.     /**
  143.      * @inheritdoc
  144.      */
  145.     public function getAuthKey()
  146.     {
  147.         return $this->auth_key;
  148.     }
  149.  
  150.     /**
  151.      * @inheritdoc
  152.      */
  153.     public function validateAuthKey($authKey)
  154.     {
  155.         return $this->getAuthKey() === $authKey;
  156.     }
  157.  
  158.     /**
  159.      * Validates password
  160.      *
  161.      * @param string $password password to validate
  162.      * @return bool if password provided is valid for current user
  163.      */
  164.     public function validatePassword($password)
  165.     {
  166.         return Yii::$app->security->validatePassword($password, $this->password_hash);
  167.     }
  168.  
  169.      public function setPassword($password)
  170.     {
  171.         $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  172.     }
  173.  
  174.     /**
  175.      * Generates "remember me" authentication key
  176.      */
  177.     public function generateAuthKey()
  178.     {
  179.         $this->auth_key = Yii::$app->security->generateRandomString();
  180.     }
  181.  
  182.     public static function findByPasswordResetToken($token)
  183.     {
  184.         if (!static::isPasswordResetTokenValid($token)) {
  185.             return null;
  186.         }
  187.         return static::findOne([
  188.             'password_reset_token' => $token,
  189.             'status' => self::STATUS_ACTIVE,
  190.         ]);
  191.     }
  192.  
  193.     public static function isPasswordResetTokenValid($token)
  194.     {
  195.         if (empty($token)) {
  196.             return false;
  197.         }
  198.         $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  199.         $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  200.         return $timestamp + $expire >= time();
  201.     }
  202.     public function generatePasswordResetToken()
  203.     {
  204.         $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  205.     }
  206.     public function removePasswordResetToken()
  207.     {
  208.         $this->password_reset_token = null;
  209.     }
  210.  
  211.     public function getAuthAssignment() {
  212.         return $this->hasMany(AuthAssignment::className(), ['user_id' => 'id']);
  213.     }
  214.  
  215.    public function getItemNames()
  216.    {      
  217.        return $this->hasMany(AuthItem::className(), ['name' => 'item_name'])->viaTable('auth_assignment', ['user_id' => 'id']);
  218.    }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement