Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\models;
  4.  
  5. use common\models\User;
  6. use Yii;
  7.  
  8. /**
  9. * Class ChangePasswordForm
  10. *
  11. * @package backend\models
  12. */
  13. class ChangePasswordForm extends User
  14. {
  15. public $new_password;
  16. public $confirm_password;
  17.  
  18. /**
  19. * @return array
  20. */
  21. public function rules()
  22. {
  23. return [
  24. [['new_password', 'confirm_password'], 'required'],
  25. [['new_password', 'confirm_password'], 'string', 'min' => 8, 'max' => 64],
  26. ['new_password', 'compareOldPassword'],
  27. ];
  28. }
  29.  
  30. /**
  31. * @return array
  32. */
  33. public function attributeLabels()
  34. {
  35. return [
  36. 'new_password' => Yii::t('common', 'Новый пароль'),
  37. 'confirm_password' => Yii::t('common', 'Повторите пароль'),
  38. ];
  39. }
  40.  
  41. /**
  42. * @param $attribute
  43. * @return bool
  44. */
  45. public function compareOldPassword($attribute)
  46. {
  47. /** @var User $user */
  48. $user = Yii::$app->user->identity;
  49.  
  50. if ($user->validatePassword($this->$attribute)) {
  51. $this->addError($attribute, Yii::t('common', 'Попробуйте другой пароль'));
  52. Yii::$app->session->setFlash('error', Yii::t('common', 'Попробуйте другой пароль'));
  53.  
  54. return false;
  55. }
  56.  
  57. return true;
  58. }
  59.  
  60. /**
  61. * @return bool
  62. */
  63. public function validateForm()
  64. {
  65. if (!$this->validate()) {
  66. return false;
  67. }
  68.  
  69. if ($this->new_password != $this->confirm_password) {
  70. $this->addError('confirm_password', Yii::t('common', 'Пароли не совпадают'));
  71. Yii::$app->session->setFlash('error', Yii::t('common', 'Пароли не совпадают'));
  72.  
  73. return false;
  74. }
  75.  
  76. return true;
  77. }
  78.  
  79. /**
  80. * @param bool $runValidation
  81. * @param null $attributeNames
  82. * @return bool
  83. */
  84. public function save($runValidation = true, $attributeNames = null)
  85. {
  86. if ($runValidation && !$this->validateForm()) {
  87. return false;
  88. }
  89.  
  90. /** @var User $user */
  91. $user = Yii::$app->user->identity;
  92.  
  93. $user->password = $this->new_password;
  94.  
  95. return $user->save(true, ['password']);
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement