myCRM/src/Validator/SystemRoleProtectionValidator.php

30 lines
957 B
PHP

<?php
namespace App\Validator;
use App\Entity\Role;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class SystemRoleProtectionValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof SystemRoleProtection) {
throw new UnexpectedTypeException($constraint, SystemRoleProtection::class);
}
if (!$value instanceof Role) {
throw new UnexpectedValueException($value, Role::class);
}
// Check if this is an existing system role being modified
if ($value->getId() !== null && $value->isSystem()) {
$this->context->buildViolation($constraint->message)
->addViolation();
}
}
}