support creating new roles (#1050)

This commit is contained in:
Kevin Papst
2019-08-20 14:02:15 +02:00
committed by GitHub
parent 299fcb091a
commit 7cd17154fc
7 changed files with 78 additions and 23 deletions

View File

@@ -9,7 +9,7 @@
namespace App\Validator\Constraints;
use App\Entity\User;
use App\Security\RoleService;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -17,14 +17,14 @@ use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class RoleValidator extends ConstraintValidator
{
/**
* @var string[]
* @var RoleService
*/
protected $allowedRoles = [
User::ROLE_USER,
User::ROLE_TEAMLEAD,
User::ROLE_ADMIN,
User::ROLE_SUPER_ADMIN
];
private $service;
public function __construct(RoleService $service)
{
$this->service = $service;
}
/**
* {@inheritdoc}
@@ -41,8 +41,10 @@ class RoleValidator extends ConstraintValidator
$roles = [$roles];
}
$allowedRoles = $this->service->getAvailableNames();
foreach ($roles as $role) {
if (!is_string($role) || !in_array($role, $this->allowedRoles)) {
if (!is_string($role) || !in_array($role, $allowedRoles)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($role))
->setCode(Role::ROLE_ERROR)