Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\models;
- use Yii;
- use yii\base\NotSupportedException;
- use yii\behaviors\TimestampBehavior;
- use yii\mongodb\ActiveRecord;
- use yii\web\IdentityInterface;
- /**
- * User model
- *
- * @property integer $id
- * @property string $username
- * @property string $password_hash
- * @property string $password_reset_token
- * @property string $email
- * @property string $auth_key
- * @property integer $status
- * @property integer $created_at
- * @property integer $updated_at
- * @property string $password write-only password
- * @property AuthAssignment[] $authAssignments
- * @property AuthItem[] $itemNames
- */
- class User extends ActiveRecord implements IdentityInterface
- {
- const STATUS_DELETED = 0;
- const STATUS_ACTIVE = 10;
- const ROLE_USER = 'user';
- const ROLE_ADMIN = 'admin';
- public $currentPassword;
- public $newPassword;
- public $newPasswordConfirm;
- public $perfil;
- public $perfil_array = [];
- /**
- * @inheritdoc
- */
- public static function collectionName()
- {
- return ['mobiledata_db', 'user'];
- }
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- TimestampBehavior::className(),
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
- ],
- ];
- }
- public function attributes()
- {
- return [
- '_id',
- 'first_name',
- 'last_name',
- 'username',
- 'password',
- 'password_hash',
- 'status',
- 'created_at',
- 'updated_at',
- 'email'
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- ['status', 'default', 'value' => self::STATUS_ACTIVE],
- ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
- ['role', 'default', 'value' => [self::ROLE_USER]],
- ['role', 'in', 'range' => [self::ROLE_USER, self::ROLE_ADMIN]],
- ];
- }
- public function validateCurrentPassword()
- {
- if (!$this->verifyPassword($this->currentPassword)) {
- $this->addError("currentPassword", "Palavra passe atual incorreto");
- }
- }
- public function verifyPassword($password)
- {
- $dbPassword = static::findOne(['username' => Yii::$app->user->identity->username, 'status' => self::STATUS_ACTIVE])->password_hash;
- return Yii::$app->security->validatePassword($password, $dbPassword);
- }
- /**
- * @inheritdoc
- */
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- /**
- * @inheritdoc
- */
- public static function findIdentityByAccessToken($token, $type = null)
- {
- throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
- }
- /**
- * Finds user by username
- *
- * @param string $username
- * @return static|null
- */
- public static function findByUsername($username)
- {
- return static::/*getCollection()->*/findOne(['username' => $username, 'status' => 1]);
- }
- /**
- * @inheritdoc
- */
- public function getId()
- {
- return $this->getPrimaryKey();
- }
- /**
- * @inheritdoc
- */
- public function getAuthKey()
- {
- return $this->auth_key;
- }
- /**
- * @inheritdoc
- */
- public function validateAuthKey($authKey)
- {
- return $this->getAuthKey() === $authKey;
- }
- /**
- * Validates password
- *
- * @param string $password password to validate
- * @return bool if password provided is valid for current user
- */
- public function validatePassword($password)
- {
- return Yii::$app->security->validatePassword($password, $this->password_hash);
- }
- public function setPassword($password)
- {
- $this->password_hash = Yii::$app->security->generatePasswordHash($password);
- }
- /**
- * Generates "remember me" authentication key
- */
- public function generateAuthKey()
- {
- $this->auth_key = Yii::$app->security->generateRandomString();
- }
- public static function findByPasswordResetToken($token)
- {
- if (!static::isPasswordResetTokenValid($token)) {
- return null;
- }
- return static::findOne([
- 'password_reset_token' => $token,
- 'status' => self::STATUS_ACTIVE,
- ]);
- }
- public static function isPasswordResetTokenValid($token)
- {
- if (empty($token)) {
- return false;
- }
- $timestamp = (int) substr($token, strrpos($token, '_') + 1);
- $expire = Yii::$app->params['user.passwordResetTokenExpire'];
- return $timestamp + $expire >= time();
- }
- public function generatePasswordResetToken()
- {
- $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
- }
- public function removePasswordResetToken()
- {
- $this->password_reset_token = null;
- }
- public function getAuthAssignment() {
- return $this->hasMany(AuthAssignment::className(), ['user_id' => 'id']);
- }
- public function getItemNames()
- {
- return $this->hasMany(AuthItem::className(), ['name' => 'item_name'])->viaTable('auth_assignment', ['user_id' => 'id']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement