Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\modules\cms\modules\user\models;
- use app\modules\admin\modules\entries\models\Client;
- use Yii;
- use app\components\ModelBase;
- use yii\web\CookieCollection;
- use yii\web\IdentityInterface;
- /**
- * This is the model class for table "{{%users}}".
- *
- * @property int $id
- * @property int $group
- * @property string $name
- * @property string $email
- * @property string $password
- * @property string $access_token
- * @property string $auth_key
- * @property int $status
- * @property int $deleted
- * @property string $created_at
- * @property string $updated_at
- * @property int $created_by
- *
- * @property User $createdBy
- * @property User[] $users
- * @property Client $client
- */
- class User extends ModelBase implements IdentityInterface
- {
- const GROUP_ADMIN = 1;
- const GROUP_CONTENT_EDITOR = 2;
- /**
- * @var string
- */
- public $repeatPassword;
- /**
- * @var string atributo para ser utilizado ao atualizar
- * a senha de acesso do usuario
- */
- public $currentPassword;
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%users}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['group', 'name', 'email', 'password', 'created_at', 'created_by'], 'required'],
- [['group', 'status', 'deleted', 'created_by', 'client_id'], 'integer'],
- [['created_at', 'updated_at'], 'safe'],
- [['name', 'email', 'password'], 'string', 'max' => 60],
- [['access_token', 'auth_key'], 'string', 'max' => 100],
- [
- 'created_by',
- 'exist',
- 'skipOnError' => true,
- 'targetClass' => User::class,
- 'targetAttribute' => ['created_by' => 'id']
- ],
- [['password', 'repeatPassword'], 'required', 'on' => 'create'],
- ['repeatPassword', 'compare', 'compareAttribute' => 'password', 'skipOnEmpty' => false],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => $this->idLabel,
- 'client_id' => 'Cliente',
- 'group' => 'Grupo de Usuário',
- 'name' => 'Name',
- 'email' => 'Email',
- 'password' => 'Password',
- 'repeatPassword' => 'Repita sua senha',
- 'access_token' => 'access_token',
- 'auth_key' => 'Auth Key',
- 'status' => $this->statusLabel,
- 'created_at' => $this->createdAtLabel,
- 'updated_at' => $this->updateAtLabel,
- 'created_by' => $this->createdByLabel
- ];
- }
- /**
- * @inheritdoc
- */
- public function afterFind()
- {
- $this->currentPassword = $this->password;
- if (!$this->isNewRecord) {
- $this->password = '';
- }
- parent::afterFind();
- }
- /**
- * @inheritdoc
- */
- public function beforeSave($insert)
- {
- if ($this->isNewRecord) {
- $this->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);
- $this->repeatPassword = $this->password;
- $this->auth_key = Yii::$app->getSecurity()->generateRandomString(70);
- } else {
- if (empty($this->password)) {
- $this->password = $this->currentPassword;
- $this->repeatPassword = $this->currentPassword;
- } else {
- $this->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);
- $this->repeatPassword = $this->password;
- }
- }
- return parent::beforeSave($insert);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getCreatedBy()
- {
- return $this->hasOne(User::class, ['id' => 'created_by']);
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getUsers()
- {
- return $this->hasMany(User::class, ['created_by' => 'id']);
- }
- public function getClient()
- {
- return $this->hasOne(Client::class, ['id' => 'client_id']);
- }
- /**
- * Finds an identity by the given ID.
- * @param string|integer $id the ID to be looked for
- * @return IdentityInterface the identity object that matches the given ID.
- * Null should be returned if such an identity cannot be found
- * or the identity is not in an active state (disabled, deleted, etc.)
- */
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- /**
- * Finds an identity by the given access_token.
- * @param mixed $access_token the access_token to be looked for
- * @param mixed $type the type of the access_token. The value of this parameter depends on the implementation.
- * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
- * @return IdentityInterface the identity object that matches the given access_token.
- * Null should be returned if such an identity cannot be found
- * or the identity is not in an active state (disabled, deleted, etc.)
- */
- public static function findIdentityByAccessToken($access_token, $type = null)
- {
- return static::findOne(['access_token' => $access_token]);
- }
- /**
- * Returns an ID that can uniquely identify a user identity.
- * @return string|integer an ID that uniquely identifies a user identity.
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Returns a key that can be used to check the validity of a given identity ID.
- *
- * The key should be unique for each individual user, and should be persistent
- * so that it can be used to check the validity of the user identity.
- *
- * The space of such keys should be big enough to defeat potential identity attacks.
- *
- * This is required if [[User::enableAutoLogin]] is enabled.
- * @return string a key that is used to check the validity of a given identity ID.
- * @see validateAuthKey()
- */
- public function getAuthKey()
- {
- return $this->auth_key;
- }
- /**
- * Validates the given auth key.
- *
- * This is required if [[User::enableAutoLogin]] is enabled.
- * @param string $authKey the given auth key
- * @return boolean whether the given auth key is valid.
- * @see getAuthKey()
- */
- public function validateAuthKey($authKey)
- {
- return $authKey === $this->auth_key;
- }
- public function validatePassword(string $password)
- {
- return Yii::$app->getSecurity()->validatePassword($password, $this->currentPassword);
- }
- public function destroyCookies()
- {
- /** @var CookieCollection $cookies */
- $cookies = Yii::$app->response->cookies;
- $cookies->remove('user_site_id');
- $cookies->remove('user_site_name');
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement