Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace backend\models;
- use common\models\User;
- use Yii;
- /**
- * Class ChangePasswordForm
- *
- * @package backend\models
- */
- class ChangePasswordForm extends User
- {
- public $new_password;
- public $confirm_password;
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['new_password', 'confirm_password'], 'required'],
- [['new_password', 'confirm_password'], 'string', 'min' => 8, 'max' => 64],
- ['new_password', 'compareOldPassword'],
- ];
- }
- /**
- * @return array
- */
- public function attributeLabels()
- {
- return [
- 'new_password' => Yii::t('common', 'Новый пароль'),
- 'confirm_password' => Yii::t('common', 'Повторите пароль'),
- ];
- }
- /**
- * @param $attribute
- * @return bool
- */
- public function compareOldPassword($attribute)
- {
- /** @var User $user */
- $user = Yii::$app->user->identity;
- if ($user->validatePassword($this->$attribute)) {
- $this->addError($attribute, Yii::t('common', 'Попробуйте другой пароль'));
- Yii::$app->session->setFlash('error', Yii::t('common', 'Попробуйте другой пароль'));
- return false;
- }
- return true;
- }
- /**
- * @return bool
- */
- public function validateForm()
- {
- if (!$this->validate()) {
- return false;
- }
- if ($this->new_password != $this->confirm_password) {
- $this->addError('confirm_password', Yii::t('common', 'Пароли не совпадают'));
- Yii::$app->session->setFlash('error', Yii::t('common', 'Пароли не совпадают'));
- return false;
- }
- return true;
- }
- /**
- * @param bool $runValidation
- * @param null $attributeNames
- * @return bool
- */
- public function save($runValidation = true, $attributeNames = null)
- {
- if ($runValidation && !$this->validateForm()) {
- return false;
- }
- /** @var User $user */
- $user = Yii::$app->user->identity;
- $user->password = $this->new_password;
- return $user->save(true, ['password']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement