diff --git a/site/app/Form/ChangePasswordFormFactory.php b/site/app/Form/ChangePasswordFormFactory.php index 2bfda4905..ee32691c2 100644 --- a/site/app/Form/ChangePasswordFormFactory.php +++ b/site/app/Form/ChangePasswordFormFactory.php @@ -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; @@ -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'); diff --git a/site/app/Form/Controls/FormControlsFactory.php b/site/app/Form/Controls/FormControlsFactory.php index b5377a264..3d974ff90 100644 --- a/site/app/Form/Controls/FormControlsFactory.php +++ b/site/app/Form/Controls/FormControlsFactory.php @@ -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'); diff --git a/site/app/User/Exceptions/IdentityNotSimpleIdentityException.php b/site/app/User/Exceptions/IdentityNotSimpleIdentityException.php new file mode 100644 index 000000000..fef1dc0da --- /dev/null +++ b/site/app/User/Exceptions/IdentityNotSimpleIdentityException.php @@ -0,0 +1,22 @@ +' : $identity::class, SimpleIdentity::class), + previous: $previous, + ); + } + +} diff --git a/site/app/User/Manager.php b/site/app/User/Manager.php index b2f46fbc4..e9a86f60c 100644 --- a/site/app/User/Manager.php +++ b/site/app/User/Manager.php @@ -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; @@ -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 @@ -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); }