mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-24 18:58:46 +02:00
Added some tests to constraint validators
This commit is contained in:
parent
e72b120c12
commit
2ebb4fef4c
12 changed files with 719 additions and 14 deletions
|
@ -63,7 +63,7 @@ class NoneOfItsChildrenValidator extends ConstraintValidator
|
|||
|
||||
// Check if the targeted parent is the object itself:
|
||||
$entity_id = $entity->getID();
|
||||
if (null !== $entity_id && $entity_id === $value->getID()) {
|
||||
if ($entity === $value || (null !== $entity_id && $entity_id === $value->getID())) {
|
||||
//Set the entity to a valid state
|
||||
$entity->setParent(null);
|
||||
$this->context->buildViolation($constraint->self_message)->addViolation();
|
||||
|
|
|
@ -53,7 +53,7 @@ class SelectableValidator extends ConstraintValidator
|
|||
|
||||
//Check type of value. Validating only works for StructuralDBElements
|
||||
if (!$value instanceof AbstractStructuralDBElement) {
|
||||
throw new UnexpectedValueException($value, 'StructuralDBElement');
|
||||
throw new UnexpectedValueException($value, AbstractStructuralDBElement::class);
|
||||
}
|
||||
|
||||
//Check if the value is not selectable -> show error message then.
|
||||
|
|
|
@ -69,9 +69,12 @@ class UniqueObjectCollectionValidator extends ConstraintValidator
|
|||
|
||||
$violation = $this->context->buildViolation($constraint->message);
|
||||
|
||||
$violation->atPath('[' . $key . ']' . '.' . $constraint->fields[0]);
|
||||
//Use the first supplied field as the target field, or the first defined field name of the element if none is supplied
|
||||
$target_field = $constraint->fields[0] ?? array_keys($element)[0];
|
||||
|
||||
$violation->setParameter('{{ value }}', $this->formatValue($value))
|
||||
$violation->atPath('[' . $key . ']' . '.' . $target_field);
|
||||
|
||||
$violation->setParameter('{{ object }}', $this->formatValue($object, ConstraintValidator::OBJECT_TO_STRING))
|
||||
->setCode(UniqueObjectCollection::IS_NOT_UNIQUE)
|
||||
->addViolation();
|
||||
|
||||
|
|
|
@ -22,8 +22,20 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
class ValidGoogleAuthCode extends Constraint
|
||||
{
|
||||
/**
|
||||
* @param TwoFactorInterface|null $user The user to use for the validation process, if null, the current user is used
|
||||
*/
|
||||
public function __construct(
|
||||
array $options = null,
|
||||
string $message = null,
|
||||
array $groups = null,
|
||||
public ?TwoFactorInterface $user = null)
|
||||
{
|
||||
parent::__construct($options, $message, $groups);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,8 +23,10 @@ declare(strict_types=1);
|
|||
namespace App\Validator\Constraints;
|
||||
|
||||
use App\Entity\UserSystem\User;
|
||||
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
|
||||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticator;
|
||||
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
@ -36,7 +38,7 @@ use function strlen;
|
|||
|
||||
class ValidGoogleAuthCodeValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(protected GoogleAuthenticatorInterface $googleAuthenticator)
|
||||
public function __construct(private GoogleAuthenticatorInterface $googleAuthenticator, private Security $security)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -56,23 +58,24 @@ class ValidGoogleAuthCodeValidator extends ConstraintValidator
|
|||
|
||||
if (!ctype_digit($value)) {
|
||||
$this->context->addViolation('validator.google_code.only_digits_allowed');
|
||||
return;
|
||||
}
|
||||
|
||||
//Number must have 6 digits
|
||||
if (6 !== strlen($value)) {
|
||||
$this->context->addViolation('validator.google_code.wrong_digit_count');
|
||||
return;
|
||||
}
|
||||
|
||||
//Try to retrieve the user we want to check
|
||||
if ($this->context->getObject() instanceof FormInterface &&
|
||||
$this->context->getObject()->getParent() instanceof FormInterface
|
||||
&& $this->context->getObject()->getParent()->getData() instanceof User) {
|
||||
$user = $this->context->getObject()->getParent()->getData();
|
||||
//Use the current user to check the code
|
||||
$user = $constraint->user ?? $this->security->getUser();
|
||||
if (!$user instanceof TwoFactorInterface) {
|
||||
throw new UnexpectedValueException($user, TwoFactorInterface::class);
|
||||
}
|
||||
|
||||
//Check if the given code is valid
|
||||
if (!$this->googleAuthenticator->checkCode($user, $value)) {
|
||||
$this->context->addViolation('validator.google_code.wrong_code');
|
||||
}
|
||||
//Check if the given code is valid
|
||||
if (!$this->googleAuthenticator->checkCode($user, $value)) {
|
||||
$this->context->addViolation('validator.google_code.wrong_code');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue