mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-09-24 18:37:52 +02:00
add constructor to PasswordValidator and codereview changes
This commit is contained in:
parent
08e24ad860
commit
3b481e4cbe
@ -36,7 +36,7 @@ class ChangePasswordForm extends Form
|
|||||||
*
|
*
|
||||||
* @param PasswordPolicyHook|null $passwordPolicyObject
|
* @param PasswordPolicyHook|null $passwordPolicyObject
|
||||||
*/
|
*/
|
||||||
public function __construct($passwordPolicyObject = null)
|
public function __construct(?PasswordPolicyHook $passwordPolicyObject = null)
|
||||||
{
|
{
|
||||||
$this->passwordPolicyObject = $passwordPolicyObject;
|
$this->passwordPolicyObject = $passwordPolicyObject;
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@ -84,7 +84,7 @@ class ChangePasswordForm extends Form
|
|||||||
array(
|
array(
|
||||||
'label' => $this->translate('New Password'),
|
'label' => $this->translate('New Password'),
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'validators' => [new PasswordValidator()]
|
'validators' => [new PasswordValidator($this->passwordPolicyObject)]
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->addElement(
|
$this->addElement(
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2025 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
namespace Icinga\Forms\Config\General;
|
namespace Icinga\Forms\Config\General;
|
||||||
|
|
||||||
@ -12,34 +13,14 @@ use Icinga\Web\Form;
|
|||||||
*/
|
*/
|
||||||
class PasswordPolicyConfigForm extends Form
|
class PasswordPolicyConfigForm extends Form
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function init(): void
|
public function init(): void
|
||||||
{
|
{
|
||||||
$this->setName('form_config_general_password_policy');
|
$this->setName('form_config_general_password_policy');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function createElements(array $formData): self
|
||||||
* {@inheritdoc}
|
|
||||||
*
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function createElements(array $formData)
|
|
||||||
{
|
{
|
||||||
$this->addElement(
|
$passwordPolicies = [];
|
||||||
'checkbox',
|
|
||||||
'global_password_policy',
|
|
||||||
array(
|
|
||||||
'label' => $this->translate('Password Policy'),
|
|
||||||
'value' => true,
|
|
||||||
'description' => $this->translate(
|
|
||||||
'Enforce strong password requirements for new passwords'
|
|
||||||
),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
$passwordPolicies = [];
|
|
||||||
|
|
||||||
foreach (Hook::all('passwordpolicy') as $class => $policy) {
|
foreach (Hook::all('passwordpolicy') as $class => $policy) {
|
||||||
$passwordPolicies[$class] = $policy->getName();
|
$passwordPolicies[$class] = $policy->getName();
|
||||||
@ -49,19 +30,16 @@ class PasswordPolicyConfigForm extends Form
|
|||||||
$this->addElement(
|
$this->addElement(
|
||||||
'select',
|
'select',
|
||||||
'global_password_policy',
|
'global_password_policy',
|
||||||
array(
|
[
|
||||||
'description' => $this->translate(
|
'description' => $this->translate(
|
||||||
'Enforce strong password requirements for new passwords'
|
'Enforce strong password requirements for new passwords'
|
||||||
),
|
),
|
||||||
'label' => $this->translate('Password Policy'),
|
'label' => $this->translate('Password Policy'),
|
||||||
'multiOptions' => array_merge(
|
'multiOptions' => array_merge(
|
||||||
['' => sprintf(
|
['' => $this->translate('No Password Policy')],
|
||||||
' - %s - ',
|
|
||||||
$this->translate('No Password Policy')
|
|
||||||
)],
|
|
||||||
$passwordPolicies
|
$passwordPolicies
|
||||||
),
|
),
|
||||||
)
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2025 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
namespace Icinga\Application\Hook;
|
namespace Icinga\Application\Hook;
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2025 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
namespace Icinga\Application\ProvidedHook;
|
namespace Icinga\Application\ProvidedHook;
|
||||||
|
|
||||||
|
@ -1,17 +1,33 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2025 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
namespace Icinga\Authentication;
|
namespace Icinga\Authentication;
|
||||||
|
|
||||||
use Icinga\Application\Config;
|
use Icinga\Application\Hook\PasswordPolicyHook;
|
||||||
use Zend_Validate_Abstract;
|
use Zend_Validate_Abstract;
|
||||||
|
|
||||||
class PasswordValidator extends Zend_Validate_Abstract
|
class PasswordValidator extends Zend_Validate_Abstract
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var PasswordPolicyHook|null
|
||||||
|
*/
|
||||||
|
private ?PasswordPolicyHook $passwordPolicyObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param PasswordPolicyHook|null $passwordPolicyObject
|
||||||
|
*/
|
||||||
|
public function __construct(?PasswordPolicyHook $passwordPolicyObject = null)
|
||||||
|
{
|
||||||
|
$this->passwordPolicyObject = $passwordPolicyObject;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if password matches with password policy
|
* Checks if password matches with password policy
|
||||||
* throws a message if not
|
* throws a message if not
|
||||||
*
|
*
|
||||||
* If no password policy is configured, all passwords are considered valid
|
* If no password policy is set, all passwords are considered valid
|
||||||
*
|
*
|
||||||
* @param mixed $value The password to validate
|
* @param mixed $value The password to validate
|
||||||
*
|
*
|
||||||
@ -21,17 +37,14 @@ class PasswordValidator extends Zend_Validate_Abstract
|
|||||||
public function isValid($value): bool
|
public function isValid($value): bool
|
||||||
{
|
{
|
||||||
$this->_messages = [];
|
$this->_messages = [];
|
||||||
$passwordPolicy = Config::app()
|
|
||||||
->get('global', 'password_policy');
|
|
||||||
|
|
||||||
if (! isset($passwordPolicy) || ! class_exists($passwordPolicy)) {
|
if ($this->passwordPolicyObject === null) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$passwordPolicyObject = new $passwordPolicy();
|
$errorMessage = $this->passwordPolicyObject->validatePassword($value);
|
||||||
$errorMessage = $passwordPolicyObject->validatePassword($value);
|
|
||||||
|
|
||||||
if ($errorMessage != null) {
|
if ($errorMessage !== null) {
|
||||||
$this->_messages[] = $errorMessage;
|
$this->_messages[] = $errorMessage;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user