/backend/models/User.php

https://github.com/funson86/funshop · PHP · 181 lines · 102 code · 18 blank · 61 comment · 9 complexity · 0998369958888973af58c20750f54592 MD5 · raw file

  1. <?php
  2. namespace backend\models;
  3. use funson86\auth\models\AuthRole;
  4. use Yii;
  5. use yii\helpers\ArrayHelper;
  6. /**
  7. * User model
  8. *
  9. * @property integer $id
  10. * @property string $username
  11. * @property string $password_hash
  12. * @property string $password_reset_token
  13. * @property string $email
  14. * @property string $auth_key
  15. * @property integer $role
  16. * @property integer $status
  17. * @property integer $created_at
  18. * @property integer $updated_at
  19. * @property string $password write-only password
  20. */
  21. class User extends \common\models\User
  22. {
  23. public $password;
  24. public $repassword;
  25. public $oldpassword;
  26. private $_statusLabel;
  27. private $_roleLabel;
  28. private $_authRoleLabel;
  29. /**
  30. * @inheritdoc
  31. */
  32. public function getStatusLabel()
  33. {
  34. if ($this->_statusLabel === null) {
  35. $statuses = self::getArrayStatus();
  36. $this->_statusLabel = $statuses[$this->status];
  37. }
  38. return $this->_statusLabel;
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public static function getArrayStatus()
  44. {
  45. return [
  46. self::STATUS_ACTIVE => Yii::t('app', 'STATUS_ACTIVE'),
  47. self::STATUS_INACTIVE => Yii::t('app', 'STATUS_INACTIVE'),
  48. self::STATUS_DELETED => Yii::t('app', 'STATUS_DELETED'),
  49. ];
  50. }
  51. /*public static function getArrayRole()
  52. {
  53. return ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description');
  54. }
  55. public function getRoleLabel()
  56. {
  57. if ($this->_roleLabel === null) {
  58. $roles = self::getArrayRole();
  59. $this->_roleLabel = $roles[$this->role];
  60. }
  61. return $this->_roleLabel;
  62. }*/
  63. public static function getArrayAuthRole()
  64. {
  65. return ArrayHelper::map(AuthRole::find()->all(), 'id', 'name');
  66. }
  67. public function getAuthRoleLabel()
  68. {
  69. if ($this->_authRoleLabel === null) {
  70. $roles = self::getArrayAuthRole();
  71. $this->_authRoleLabel = $this->auth_role ? $roles[$this->auth_role] : '-';
  72. }
  73. return $this->_authRoleLabel;
  74. }
  75. /**
  76. * @inheritdoc
  77. */
  78. public function rules()
  79. {
  80. return [
  81. [['username', 'email'], 'required', 'on' => ['admin-create', 'admin-update']],
  82. [['password', 'repassword'], 'required', 'on' => ['admin-create']],
  83. [['password', 'repassword', 'oldpassword'], 'required', 'on' => ['admin-change-password']],
  84. [['username', 'email', 'password', 'repassword'], 'trim', 'on' => ['admin-create', 'admin-update']],
  85. [['password', 'repassword'], 'string', 'min' => 6, 'max' => 30, 'on' => ['admin-create', 'admin-update']],
  86. [['password', 'repassword', 'oldpassword'], 'string', 'min' => 6, 'max' => 30, 'on' => ['admin-change-password']],
  87. // Unique
  88. [['username', 'email'], 'unique', 'on' => ['admin-create', 'admin-update']],
  89. // Username
  90. ['username', 'match', 'pattern' => '/^[a-zA-Z0-9_-]+$/', 'on' => ['admin-create', 'admin-update']],
  91. ['username', 'string', 'min' => 3, 'max' => 30, 'on' => ['admin-create', 'admin-update']],
  92. // E-mail
  93. ['email', 'string', 'max' => 100, 'on' => ['admin-create', 'admin-update']],
  94. ['email', 'email', 'on' => ['admin-create', 'admin-update']],
  95. // Repassword
  96. ['repassword', 'compare', 'compareAttribute' => 'password'],
  97. //['status', 'default', 'value' => self::STATUS_ACTIVE],
  98. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
  99. // Status
  100. //['role', 'in', 'range' => array_keys(self::getArrayRole())],
  101. ['oldpassword', 'validateOldPassword', 'on' => ['admin-change-password']],
  102. ];
  103. }
  104. /**
  105. * @inheritdoc
  106. */
  107. public function scenarios()
  108. {
  109. return [
  110. 'admin-create' => ['username', 'email', 'password', 'repassword', 'status', 'auth_role'],
  111. 'admin-update' => ['username', 'email', 'password', 'repassword', 'status', 'auth_role'],
  112. 'admin-change-password' => ['oldpassword', 'password', 'repassword'],
  113. ];
  114. }
  115. /**
  116. * @inheritdoc
  117. */
  118. public function attributeLabels()
  119. {
  120. $labels = parent::attributeLabels();
  121. return array_merge(
  122. $labels,
  123. [
  124. 'password' => Yii::t('app', 'Password'),
  125. 'repassword' => Yii::t('app', 'Repassword'),
  126. 'oldpassword' => Yii::t('app', 'Oldpassword'),
  127. ]
  128. );
  129. }
  130. /**
  131. * @inheritdoc
  132. */
  133. public function beforeSave($insert)
  134. {
  135. if (parent::beforeSave($insert)) {
  136. if ($this->isNewRecord || (!$this->isNewRecord && $this->password)) {
  137. $this->setPassword($this->password);
  138. $this->generateAuthKey();
  139. $this->generatePasswordResetToken();
  140. }
  141. return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * Validates the password.
  147. * This method serves as the inline validation for password.
  148. *
  149. * @param string $attribute the attribute currently being validated
  150. * @param array $params the additional name-value pairs given in the rule
  151. */
  152. public function validateOldPassword($attribute, $params)
  153. {
  154. if (!$this->hasErrors()) {
  155. $user = self::findOne(Yii::$app->user->identity->id);
  156. if (!$user || !$user->validatePassword($this->oldpassword)) {
  157. $this->addError($attribute, Yii::t('app', 'Incorrect old password.'));
  158. }
  159. }
  160. }
  161. }