Organized password reset and account activation
This commit is contained in:
parent
83239a492d
commit
893e841a87
3 changed files with 91 additions and 83 deletions
|
@ -149,6 +149,13 @@ $userValidation =
|
||||||
\Chibi\Router::register(['UserController', 'registrationView'], 'GET', '/register', $userValidation);
|
\Chibi\Router::register(['UserController', 'registrationView'], 'GET', '/register', $userValidation);
|
||||||
\Chibi\Router::register(['UserController', 'registrationAction'], 'POST', '/register', $userValidation);
|
\Chibi\Router::register(['UserController', 'registrationAction'], 'POST', '/register', $userValidation);
|
||||||
|
|
||||||
|
\Chibi\Router::register(['UserController', 'activationView'], 'GET', '/activation', $userValidation);
|
||||||
|
\Chibi\Router::register(['UserController', 'activationAction'], 'POST', '/activation', $userValidation);
|
||||||
|
\Chibi\Router::register(['UserController', 'activationAction'], 'GET', '/activation/{token}', $userValidation);
|
||||||
|
\Chibi\Router::register(['UserController', 'passwordResetView'], 'GET', '/password-reset', $userValidation);
|
||||||
|
\Chibi\Router::register(['UserController', 'passwordResetAction'], 'POST', '/password-reset', $userValidation);
|
||||||
|
\Chibi\Router::register(['UserController', 'passwordResetAction'], 'GET', '/password-reset/{token}', $userValidation);
|
||||||
|
|
||||||
\Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{name}/flag', $userValidation);
|
\Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{name}/flag', $userValidation);
|
||||||
\Chibi\Router::register(['UserController', 'banAction'], 'POST', '/user/{name}/ban', $userValidation);
|
\Chibi\Router::register(['UserController', 'banAction'], 'POST', '/user/{name}/ban', $userValidation);
|
||||||
\Chibi\Router::register(['UserController', 'unbanAction'], 'POST', '/user/{name}/unban', $userValidation);
|
\Chibi\Router::register(['UserController', 'unbanAction'], 'POST', '/user/{name}/unban', $userValidation);
|
||||||
|
@ -161,12 +168,6 @@ foreach (['GET', 'POST'] as $method)
|
||||||
{
|
{
|
||||||
\Chibi\Router::register(['TagController', 'massTagRedirectAction'], $method, '/mass-tag-redirect', $tagValidation);
|
\Chibi\Router::register(['TagController', 'massTagRedirectAction'], $method, '/mass-tag-redirect', $tagValidation);
|
||||||
|
|
||||||
\Chibi\Router::register(['UserController', 'activationAction'], $method, '/activation/{token}', $userValidation);
|
|
||||||
\Chibi\Router::register(['UserController', 'activationProxyAction'], $method, '/activation-proxy', $userValidation);
|
|
||||||
\Chibi\Router::register(['UserController', 'activationProxyAction'], $method, '/activation-proxy/{token}', $userValidation);
|
|
||||||
\Chibi\Router::register(['UserController', 'passwordResetAction'], $method, '/password-reset/{token}', $userValidation);
|
|
||||||
\Chibi\Router::register(['UserController', 'passwordResetProxyAction'], $method, '/password-reset-proxy', $userValidation);
|
|
||||||
\Chibi\Router::register(['UserController', 'passwordResetProxyAction'], $method, '/password-reset-proxy/{token}', $userValidation);
|
|
||||||
\Chibi\Router::register(['UserController', 'toggleSafetyAction'], $method, '/user/toggle-safety/{safety}', $userValidation);
|
\Chibi\Router::register(['UserController', 'toggleSafetyAction'], $method, '/user/toggle-safety/{safety}', $userValidation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -216,18 +216,44 @@ class UserController
|
||||||
Messenger::message($message);
|
Messenger::message($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function activationView()
|
||||||
|
{
|
||||||
|
$context = getContext();
|
||||||
|
$context->viewName = 'user-select';
|
||||||
|
Assets::setSubTitle('account activation');
|
||||||
|
}
|
||||||
|
|
||||||
public function activationAction($token)
|
public function activationAction($token)
|
||||||
{
|
{
|
||||||
$context = getContext();
|
$context = getContext();
|
||||||
$context->viewName = 'message';
|
$context->viewName = 'message';
|
||||||
Assets::setSubTitle('account activation');
|
Assets::setSubTitle('account activation');
|
||||||
|
|
||||||
|
if (empty($token))
|
||||||
|
{
|
||||||
|
$name = InputHelper::get('name');
|
||||||
|
$user = UserModel::findByNameOrEmail($name);
|
||||||
|
if (empty($user->emailUnconfirmed))
|
||||||
|
{
|
||||||
|
if (!empty($user->emailConfirmed))
|
||||||
|
throw new SimpleException('E-mail was already confirmed; activation skipped');
|
||||||
|
else
|
||||||
|
throw new SimpleException('This user has no e-mail specified; activation cannot proceed');
|
||||||
|
}
|
||||||
|
EditUserEmailJob::sendEmail($user);
|
||||||
|
Messenger::message('Activation e-mail resent.');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$dbToken = TokenModel::findByToken($token);
|
$dbToken = TokenModel::findByToken($token);
|
||||||
TokenModel::checkValidity($dbToken);
|
TokenModel::checkValidity($dbToken);
|
||||||
|
|
||||||
$dbUser = $dbToken->getUser();
|
$dbUser = $dbToken->getUser();
|
||||||
|
if (empty($dbUser->emailConfirmed))
|
||||||
|
{
|
||||||
$dbUser->emailConfirmed = $dbUser->emailUnconfirmed;
|
$dbUser->emailConfirmed = $dbUser->emailUnconfirmed;
|
||||||
$dbUser->emailUnconfirmed = null;
|
$dbUser->emailUnconfirmed = null;
|
||||||
|
}
|
||||||
$dbToken->used = true;
|
$dbToken->used = true;
|
||||||
TokenModel::save($dbToken);
|
TokenModel::save($dbToken);
|
||||||
UserModel::save($dbUser);
|
UserModel::save($dbUser);
|
||||||
|
@ -243,6 +269,14 @@ class UserController
|
||||||
Auth::setCurrentUser($dbUser);
|
Auth::setCurrentUser($dbUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function passwordResetView()
|
||||||
|
{
|
||||||
|
$context = getContext();
|
||||||
|
$context->viewName = 'user-select';
|
||||||
|
Assets::setSubTitle('password reset');
|
||||||
|
}
|
||||||
|
|
||||||
public function passwordResetAction($token)
|
public function passwordResetAction($token)
|
||||||
{
|
{
|
||||||
|
@ -250,6 +284,18 @@ class UserController
|
||||||
$context->viewName = 'message';
|
$context->viewName = 'message';
|
||||||
Assets::setSubTitle('password reset');
|
Assets::setSubTitle('password reset');
|
||||||
|
|
||||||
|
if (empty($token))
|
||||||
|
{
|
||||||
|
$name = InputHelper::get('name');
|
||||||
|
$user = UserModel::findByNameOrEmail($name);
|
||||||
|
if (empty($user->emailConfirmed))
|
||||||
|
throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed');
|
||||||
|
|
||||||
|
self::sendPasswordResetConfirmation($user);
|
||||||
|
Messenger::message('E-mail sent. Follow instructions to reset password.');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
$dbToken = TokenModel::findByToken($token);
|
$dbToken = TokenModel::findByToken($token);
|
||||||
TokenModel::checkValidity($dbToken);
|
TokenModel::checkValidity($dbToken);
|
||||||
|
|
||||||
|
@ -271,45 +317,6 @@ class UserController
|
||||||
|
|
||||||
Auth::setCurrentUser($dbUser);
|
Auth::setCurrentUser($dbUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function passwordResetProxyAction()
|
|
||||||
{
|
|
||||||
$context = getContext();
|
|
||||||
$context->viewName = 'user-select';
|
|
||||||
Assets::setSubTitle('password reset');
|
|
||||||
|
|
||||||
if (!InputHelper::get('submit'))
|
|
||||||
return;
|
|
||||||
|
|
||||||
$name = InputHelper::get('name');
|
|
||||||
$user = UserModel::findByNameOrEmail($name);
|
|
||||||
if (empty($user->emailConfirmed))
|
|
||||||
throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed');
|
|
||||||
|
|
||||||
self::sendPasswordResetConfirmation($user);
|
|
||||||
Messenger::message('E-mail sent. Follow instructions to reset password.');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function activationProxyAction()
|
|
||||||
{
|
|
||||||
$context = getContext();
|
|
||||||
$context->viewName = 'user-select';
|
|
||||||
Assets::setSubTitle('account activation');
|
|
||||||
|
|
||||||
if (!InputHelper::get('submit'))
|
|
||||||
return;
|
|
||||||
|
|
||||||
$name = InputHelper::get('name');
|
|
||||||
$user = UserModel::findByNameOrEmail($name);
|
|
||||||
if (empty($user->emailUnconfirmed))
|
|
||||||
{
|
|
||||||
if (!empty($user->emailConfirmed))
|
|
||||||
throw new SimpleException('E-mail was already confirmed; activation skipped');
|
|
||||||
else
|
|
||||||
throw new SimpleException('This user has no e-mail specified; activation cannot proceed');
|
|
||||||
}
|
|
||||||
EditUserEmailJob::sendEmail($user);
|
|
||||||
Messenger::message('Activation e-mail resent.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function sendPasswordResetConfirmation($user)
|
private static function sendPasswordResetConfirmation($user)
|
||||||
|
|
|
@ -43,8 +43,8 @@ Assets::addStylesheet('auth.css');
|
||||||
<div>
|
<div>
|
||||||
<p>Problems logging in?</p>
|
<p>Problems logging in?</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="<?= \Chibi\Router::linkTo(['UserController', 'passwordResetProxyAction']) ?>">I don't remember my password</a></li>
|
<li><a href="<?= \Chibi\Router::linkTo(['UserController', 'passwordResetView']) ?>">I don't remember my password</a></li>
|
||||||
<li><a href="<?= \Chibi\Router::linkTo(['UserController', 'activationProxyAction']) ?>">I haven't received activation e-mail</a></li>
|
<li><a href="<?= \Chibi\Router::linkTo(['UserController', 'activationView']) ?>">I haven't received activation e-mail</a></li>
|
||||||
<li><a href="<?= \Chibi\Router::linkTo(['UserController', 'registrationView']) ?>">I don't have an account</a></li>
|
<li><a href="<?= \Chibi\Router::linkTo(['UserController', 'registrationView']) ?>">I don't have an account</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue