Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.05 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\modules\cms\modules\user\models;
  4.  
  5. use app\modules\admin\modules\entries\models\Client;
  6. use Yii;
  7. use app\components\ModelBase;
  8. use yii\web\CookieCollection;
  9. use yii\web\IdentityInterface;
  10.  
  11. /**
  12.  * This is the model class for table "{{%users}}".
  13.  *
  14.  * @property int $id
  15.  * @property int $group
  16.  * @property string $name
  17.  * @property string $email
  18.  * @property string $password
  19.  * @property string $access_token
  20.  * @property string $auth_key
  21.  * @property int $status
  22.  * @property int $deleted
  23.  * @property string $created_at
  24.  * @property string $updated_at
  25.  * @property int $created_by
  26.  *
  27.  * @property User $createdBy
  28.  * @property User[] $users
  29.  * @property Client $client
  30.  */
  31. class User extends ModelBase implements IdentityInterface
  32. {
  33.     const GROUP_ADMIN = 1;
  34.     const GROUP_CONTENT_EDITOR = 2;
  35.  
  36.     /**
  37.      * @var string
  38.      */
  39.     public $repeatPassword;
  40.  
  41.     /**
  42.      * @var string atributo para ser utilizado ao atualizar
  43.      * a senha de acesso do usuario
  44.      */
  45.     public $currentPassword;
  46.  
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public static function tableName()
  51.     {
  52.         return '{{%users}}';
  53.     }
  54.  
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function rules()
  59.     {
  60.         return [
  61.             [['group', 'name', 'email', 'password', 'created_at', 'created_by'], 'required'],
  62.             [['group', 'status', 'deleted', 'created_by', 'client_id'], 'integer'],
  63.             [['created_at', 'updated_at'], 'safe'],
  64.             [['name', 'email', 'password'], 'string', 'max' => 60],
  65.             [['access_token', 'auth_key'], 'string', 'max' => 100],
  66.             [
  67.                 'created_by',
  68.                 'exist',
  69.                 'skipOnError' => true,
  70.                 'targetClass' => User::class,
  71.                 'targetAttribute' => ['created_by' => 'id']
  72.             ],
  73.             [['password', 'repeatPassword'], 'required', 'on' => 'create'],
  74.             ['repeatPassword', 'compare', 'compareAttribute' => 'password', 'skipOnEmpty' => false],
  75.         ];
  76.     }
  77.  
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function attributeLabels()
  82.     {
  83.         return [
  84.             'id' => $this->idLabel,
  85.             'client_id' => 'Cliente',
  86.             'group' => 'Grupo de Usuário',
  87.             'name' => 'Name',
  88.             'email' => 'Email',
  89.             'password' => 'Password',
  90.             'repeatPassword' => 'Repita sua senha',
  91.             'access_token' => 'access_token',
  92.             'auth_key' => 'Auth Key',
  93.             'status' => $this->statusLabel,
  94.             'created_at' => $this->createdAtLabel,
  95.             'updated_at' => $this->updateAtLabel,
  96.             'created_by' => $this->createdByLabel
  97.         ];
  98.     }
  99.  
  100.     /**
  101.      * @inheritdoc
  102.      */
  103.     public function afterFind()
  104.     {
  105.         $this->currentPassword = $this->password;
  106.  
  107.         if (!$this->isNewRecord) {
  108.             $this->password = '';
  109.         }
  110.  
  111.         parent::afterFind();
  112.     }
  113.  
  114.      /**
  115.      * @inheritdoc
  116.      */
  117.     public function beforeSave($insert)
  118.     {
  119.         if ($this->isNewRecord) {
  120.             $this->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);
  121.             $this->repeatPassword = $this->password;
  122.             $this->auth_key = Yii::$app->getSecurity()->generateRandomString(70);
  123.         } else {
  124.             if (empty($this->password)) {
  125.                 $this->password = $this->currentPassword;
  126.                 $this->repeatPassword = $this->currentPassword;
  127.             } else {
  128.                 $this->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);
  129.                 $this->repeatPassword = $this->password;
  130.             }
  131.         }
  132.  
  133.         return parent::beforeSave($insert);
  134.     }
  135.  
  136.     /**
  137.      * @return \yii\db\ActiveQuery
  138.      */
  139.     public function getCreatedBy()
  140.     {
  141.         return $this->hasOne(User::class, ['id' => 'created_by']);
  142.     }
  143.  
  144.     /**
  145.      * @return \yii\db\ActiveQuery
  146.      */
  147.     public function getUsers()
  148.     {
  149.         return $this->hasMany(User::class, ['created_by' => 'id']);
  150.     }
  151.    
  152.     public function getClient()
  153.     {
  154.         return $this->hasOne(Client::class, ['id' => 'client_id']);
  155.     }
  156.  
  157.     /**
  158.      * Finds an identity by the given ID.
  159.      * @param string|integer $id the ID to be looked for
  160.      * @return IdentityInterface the identity object that matches the given ID.
  161.      * Null should be returned if such an identity cannot be found
  162.      * or the identity is not in an active state (disabled, deleted, etc.)
  163.      */
  164.     public static function findIdentity($id)
  165.     {
  166.         return static::findOne($id);
  167.     }
  168.  
  169.     /**
  170.      * Finds an identity by the given access_token.
  171.      * @param mixed $access_token the access_token to be looked for
  172.      * @param mixed $type the type of the access_token. The value of this parameter depends on the implementation.
  173.      * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
  174.      * @return IdentityInterface the identity object that matches the given access_token.
  175.      * Null should be returned if such an identity cannot be found
  176.      * or the identity is not in an active state (disabled, deleted, etc.)
  177.      */
  178.     public static function findIdentityByAccessToken($access_token, $type = null)
  179.     {
  180.         return static::findOne(['access_token' => $access_token]);
  181.     }
  182.  
  183.     /**
  184.      * Returns an ID that can uniquely identify a user identity.
  185.      * @return string|integer an ID that uniquely identifies a user identity.
  186.      */
  187.     public function getId()
  188.     {
  189.         return $this->id;
  190.     }
  191.  
  192.     /**
  193.      * Returns a key that can be used to check the validity of a given identity ID.
  194.      *
  195.      * The key should be unique for each individual user, and should be persistent
  196.      * so that it can be used to check the validity of the user identity.
  197.      *
  198.      * The space of such keys should be big enough to defeat potential identity attacks.
  199.      *
  200.      * This is required if [[User::enableAutoLogin]] is enabled.
  201.      * @return string a key that is used to check the validity of a given identity ID.
  202.      * @see validateAuthKey()
  203.      */
  204.     public function getAuthKey()
  205.     {
  206.         return $this->auth_key;
  207.     }
  208.  
  209.     /**
  210.      * Validates the given auth key.
  211.      *
  212.      * This is required if [[User::enableAutoLogin]] is enabled.
  213.      * @param string $authKey the given auth key
  214.      * @return boolean whether the given auth key is valid.
  215.      * @see getAuthKey()
  216.      */
  217.     public function validateAuthKey($authKey)
  218.     {
  219.         return $authKey === $this->auth_key;
  220.     }
  221.  
  222.     public function validatePassword(string $password)
  223.     {
  224.         return Yii::$app->getSecurity()->validatePassword($password, $this->currentPassword);
  225.     }
  226.  
  227.     public function destroyCookies()
  228.     {
  229.         /** @var CookieCollection $cookies */
  230.         $cookies = Yii::$app->response->cookies;
  231.  
  232.         $cookies->remove('user_site_id');
  233.         $cookies->remove('user_site_name');
  234.  
  235.         return true;
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement