Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password forms improvements #90

Merged
merged 4 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion site/app/Form/ChangePasswordFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace MichalSpacekCz\Form;

use MichalSpacekCz\User\Exceptions\IdentityNotSimpleIdentityException;
use MichalSpacekCz\User\Manager;
use Nette\Application\UI\Form;
use Nette\Security\User;
Expand All @@ -22,16 +23,25 @@ public function __construct(
/**
* @param callable(): void $onSuccess
* @return Form
* @throws IdentityNotSimpleIdentityException
*/
public function create(callable $onSuccess): Form
{
$form = $this->factory->create();
$form->addText('username')
->setDefaultValue($this->authenticator->getIdentityByUser($this->user)->username)
->setHtmlAttribute('passwordrules', 'minlength: 42; required: lower; required: upper; required: digit; required: [ !#$%&*+,./:;=?@_~];')
->setHtmlAttribute('autocomplete', 'username')
->setHtmlAttribute('class', 'hidden');
$form->addPassword('password', 'Současné heslo:')
->setHtmlAttribute('autocomplete', 'current-password')
->setRequired('Zadejte prosím současné heslo');
$newPassword = $form->addPassword('newPassword', 'Nové heslo:')
->setHtmlAttribute('autocomplete', 'new-password')
->setRequired('Zadejte prosím nové heslo')
->addRule($form::MIN_LENGTH, 'Nové heslo musí mít alespoň %d znaků', 6);
->addRule($form::MIN_LENGTH, 'Nové heslo musí mít alespoň %d znaků', 15);
$form->addPassword('newPasswordVerify', 'Nové heslo pro kontrolu:')
->setHtmlAttribute('autocomplete', 'new-password')
->setRequired('Zadejte prosím nové heslo pro kontrolu')
->addRule($form::EQUAL, 'Hesla se neshodují', $newPassword);
$form->addSubmit('save', 'Uložit');
Expand Down
2 changes: 2 additions & 0 deletions site/app/Form/Controls/FormControlsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class FormControlsFactory
public function addSignIn(Container $container): void
{
$container->addText('username', 'Uživatel:')
->setHtmlAttribute('autocomplete', 'username')
->setRequired('Zadejte prosím uživatele');
$container->addPassword('password', 'Heslo:')
->setHtmlAttribute('autocomplete', 'current-password')
->setRequired('Zadejte prosím heslo');
$container->addCheckbox('remember', 'Zůstat přihlášen');
$container->addSubmit('signin', 'Přihlásit');
Expand Down
22 changes: 22 additions & 0 deletions site/app/User/Exceptions/IdentityNotSimpleIdentityException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\User\Exceptions;

use Exception;
use Nette\Security\IIdentity;
use Nette\Security\SimpleIdentity;
use Throwable;

class IdentityNotSimpleIdentityException extends Exception
{

public function __construct(?IIdentity $identity, ?Throwable $previous = null)
{
parent::__construct(
sprintf('Identity is of class %s but should be %s', $identity === null ? '<null>' : $identity::class, SimpleIdentity::class),
previous: $previous,
);
}

}
19 changes: 16 additions & 3 deletions site/app/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use DateTimeInterface;
use Exception;
use MichalSpacekCz\User\Exceptions\IdentityNotSimpleIdentityException;
use Nette\Application\LinkGenerator;
use Nette\Database\Explorer;
use Nette\Database\Row;
Expand Down Expand Up @@ -79,6 +80,19 @@ public function getIdentity(int $id, string $username): SimpleIdentity
}


/**
* @throws IdentityNotSimpleIdentityException
*/
public function getIdentityByUser(User $user): SimpleIdentity
{
$identity = $user->getIdentity();
if (!$identity instanceof SimpleIdentity) {
throw new IdentityNotSimpleIdentityException($identity);
}
return $identity;
}


/**
* @param string $username
* @param string $password
Expand Down Expand Up @@ -123,12 +137,11 @@ private function verifyPassword(string $username, string $password): int
* @param string $newPassword
* @throws AuthenticationException
* @throws HaliteAlert
* @throws IdentityNotSimpleIdentityException
*/
public function changePassword(User $user, string $password, string $newPassword): void
{
/** @var SimpleIdentity $identity */
$identity = $user->getIdentity();
$this->verifyPassword($identity->username, $password);
$this->verifyPassword($this->getIdentityByUser($user)->username, $password);
$this->updatePassword($user->getId(), $newPassword);
$this->clearPermanentLogin($user);
}
Expand Down