fixes on email change strategies + fix Settigns form

This commit is contained in:
Antonio Ramirez
2016-12-10 20:38:21 +01:00
parent d3ad2c4f90
commit 1c863f815a
2 changed files with 11 additions and 74 deletions

View File

@ -2,7 +2,7 @@
namespace Da\User\Form;
use Da\User\Factory\TokenFactory;
use Da\User\Factory\EmailChangeStrategyFactory;
use Da\User\Helper\SecurityHelper;
use Da\User\Model\User;
use Da\User\Traits\ContainerTrait;
@ -117,83 +117,20 @@ class SettingsForm extends Model
$this->user->password = $this->new_password;
if ($this->email == $this->user->email && $this->user->unconfirmed_email != null) {
$this->user->unconfirmed_email = null;
} elseif ($this->email != $this->user->email) {
switch ($this->module->emailChangeStrategy) {
case Module::STRATEGY_INSECURE:
$this->insecureEmailChange();
break;
case Module::STRATEGY_DEFAULT:
$this->defaultEmailChange();
break;
case Module::STRATEGY_SECURE:
$this->secureEmailChange();
break;
default:
throw new \OutOfBoundsException('Invalid email changing strategy');
}
}
return $this->user->save();
} elseif ($this->email != $this->user->email) {
$strategy = EmailChangeStrategyFactory::makeByStrategyType(
$this->getModule()->emailChangeStrategy,
$this
);
return $strategy->run();
}
}
return false;
}
/**
* Changes user's email address to given without any confirmation.
*/
protected function insecureEmailChange()
{
$this->user->email = $this->email;
Yii::$app->session->setFlash('success', Yii::t('user', 'Your email address has been changed'));
}
/**
* Sends a confirmation message to user's email address with link to confirm changing of email.
*/
protected function defaultEmailChange()
{
$this->user->unconfirmed_email = $this->email;
/** @var Token $token */
$token = TokenFactory::makeConfirmNewMailToken($this->user->id);
$this->mailer->sendReconfirmationMessage($this->user, $token);
Yii::$app->session->setFlash(
'info',
Yii::t('user', 'A confirmation message has been sent to your new email address')
);
}
/**
* Sends a confirmation message to both old and new email addresses with link to confirm changing of email.
*
* @throws \yii\base\InvalidConfigException
*/
protected function secureEmailChange()
{
$this->defaultEmailChange();
/** @var Token $token */
$token = Yii::createObject(
[
'class' => Token::className(),
'user_id' => $this->user->id,
'type' => Token::TYPE_CONFIRM_OLD_EMAIL,
]
);
$token->save(false);
$this->mailer->sendReconfirmationMessage($this->user, $token);
// unset flags if they exist
$this->user->flags &= ~User::NEW_EMAIL_CONFIRMED;
$this->user->flags &= ~User::OLD_EMAIL_CONFIRMED;
$this->user->save(false);
Yii::$app->session->setFlash(
'info',
Yii::t(
'user',
'We have sent confirmation links to both old and new email addresses. You must click both links to complete your request'
)
);
}
}

View File

@ -33,7 +33,7 @@ class DefaultEmailChangeStrategy implements MailChangeStrategyInterface
->session
->setFlash('info', Yii::t('user', 'A confirmation message has been sent to your new email address'));
return true;
return $this->form->getUser()->save();
}
return false;