Remove usage of deprecated Role class

This commit is contained in:
Julien Fastré 2023-08-31 17:08:18 +02:00
parent 76142c1264
commit 7c58880139
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
33 changed files with 55 additions and 80 deletions

View File

@ -43,7 +43,7 @@ class ConsultationController extends \Symfony\Bundle\FrameworkBundle\Controller\
$circles = $authorizationHelper->getReachableCircles(
$this->getUser(),
new Role(ConsultationVoter::SEE),
ConsultationVoter::SEE,
$person->getCenter()
);

View File

@ -87,7 +87,7 @@ class ChillPersonAddAPersonWidget implements WidgetInterface
// show only the person from the authorized centers
$and = $qb->expr()->andX();
$centers = $this->authorizationHelper
->getReachableCenters($this->getUser(), new Role(PersonVoter::SEE));
->getReachableCenters($this->getUser(), PersonVoter::SEE);
$and->add($qb->expr()->in('person.center', ':centers'));
$qb->setParameter('centers', $centers);

View File

@ -10,7 +10,7 @@
<php>
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0" />
<server name="SHELL_VERBOSITY" value="-1" />
<env name="KERNEL_CLASS" value="\App\Kernel" />
</php>

View File

@ -53,7 +53,6 @@ use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Role\Role;
use function in_array;
class ActivityType extends AbstractType
@ -410,7 +409,7 @@ class ActivityType extends AbstractType
$resolver
->setRequired(['center', 'role', 'activityType', 'accompanyingPeriod'])
->setAllowedTypes('center', ['null', Center::class])
->setAllowedTypes('role', [Role::class, 'string'])
->setAllowedTypes('role', ['string'])
->setAllowedTypes('activityType', \Chill\ActivityBundle\Entity\ActivityType::class)
->setAllowedTypes('accompanyingPeriod', [\Chill\PersonBundle\Entity\AccompanyingPeriod::class, 'null']);
}

View File

@ -354,14 +354,14 @@ final class ActivityControllerTest extends WebTestCase
->get('chill.main.security.authorization.helper')
->getReachableScopes(
$user,
new Role('CHILL_ACTIVITY_UPDATE'),
'CHILL_ACTIVITY_UPDATE',
$center
);
$reachableScopesDelete = self::$kernel->getContainer()
->get('chill.main.security.authorization.helper')
->getReachableScopes(
$user,
new Role('CHILL_ACTIVITY_DELETE'),
'CHILL_ACTIVITY_DELETE',
$center
);
$reachableScopesId = array_intersect(

View File

@ -30,11 +30,6 @@ final class ActivityTypeTest extends KernelTestCase
*/
protected $center;
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* @var \Symfony\Component\Form\FormBuilderInterface
*/
@ -85,7 +80,7 @@ final class ActivityTypeTest extends KernelTestCase
$form = $this->formBuilder
->add('activity', ActivityType::class, [
'center' => $this->center,
'role' => new Role('CHILL_ACTIVITY_CREATE'),
'role' => 'CHILL_ACTIVITY_CREATE',
])
->getForm();
@ -101,7 +96,7 @@ final class ActivityTypeTest extends KernelTestCase
$form = $this->formBuilder
->add('activity', ActivityType::class, [
'center' => $this->center,
'role' => new Role('CHILL_ACTIVITY_CREATE'),
'role' => 'CHILL_ACTIVITY_CREATE',
])
->getForm();
@ -157,7 +152,7 @@ final class ActivityTypeTest extends KernelTestCase
$form = $builder
->add('activity', ActivityType::class, [
'center' => $this->center,
'role' => new Role('CHILL_ACTIVITY_CREATE'),
'role' => 'CHILL_ACTIVITY_CREATE',
])
->getForm();

View File

@ -141,8 +141,8 @@ class TimelineActivityProvider implements TimelineProviderInterface
$parameters = [];
$metadataActivity = $this->em->getClassMetadata(Activity::class);
$associationMapping = $metadataActivity->getAssociationMapping('person');
$role = new Role('CHILL_ACTIVITY_SEE');
$reachableScopes = $this->helper->getReachableScopes($this->user, $role->getRole(), $person->getCenter());
$role = 'CHILL_ACTIVITY_SEE';
$reachableScopes = $this->helper->getReachableScopes($this->user, $role, $person->getCenter());
$whereClause = ' {activity.person_id} = ? AND {activity.scope_id} IN ({scopes_ids}) ';
$scopes_ids = [];

View File

@ -189,7 +189,7 @@ class EventController extends AbstractController
$reachablesCircles = $this->authorizationHelper->getReachableCircles(
$this->getUser(),
new Role(EventVoter::SEE),
EventVoter::SEE,
$person->getCenter()
);
@ -274,7 +274,7 @@ class EventController extends AbstractController
*/
public function newPickCenterAction()
{
$role = new Role('CHILL_EVENT_CREATE');
$role = 'CHILL_EVENT_CREATE';
/**
* @var Center $centers
@ -404,7 +404,7 @@ class EventController extends AbstractController
);
$builder->add('event_id', PickEventType::class, [
'role' => new Role('CHILL_EVENT_CREATE'),
'role' => 'CHILL_EVENT_CREATE',
'centers' => $person->getCenter(),
]);
@ -451,7 +451,7 @@ class EventController extends AbstractController
);
$builder->add('person_id', PickPersonType::class, [
'role' => new Role('CHILL_EVENT_CREATE'),
'role' => 'CHILL_EVENT_CREATE',
'centers' => $event->getCenter(),
]);
@ -630,7 +630,7 @@ class EventController extends AbstractController
$form = $this->createForm(EventType::class, $entity, [
'method' => 'POST',
'center' => $entity->getCenter(),
'role' => new Role('CHILL_EVENT_CREATE'),
'role' => 'CHILL_EVENT_CREATE',
]);
$form->add('submit', SubmitType::class, ['label' => 'Create']);
@ -665,7 +665,7 @@ class EventController extends AbstractController
'action' => $this->generateUrl('chill_event__event_update', ['event_id' => $entity->getId()]),
'method' => 'PUT',
'center' => $entity->getCenter(),
'role' => new Role('CHILL_EVENT_CREATE'),
'role' => 'CHILL_EVENT_CREATE',
]);
$form->remove('center');

View File

@ -59,7 +59,7 @@ class EventType extends AbstractType
$resolver
->setRequired(['center', 'role'])
->setAllowedTypes('center', Center::class)
->setAllowedTypes('role', Role::class);
->setAllowedTypes('role', 'string');
}
/**

View File

@ -74,7 +74,7 @@ final class PickEventType extends AbstractType
->setDefault('centers', null);
$resolver
->setDefined('role')
->addAllowedTypes('role', [Role::class, 'null'])
->addAllowedTypes('role', ['string', 'null'])
->setDefault('role', null);
// add the default options

View File

@ -119,7 +119,7 @@ class EventVoter extends AbstractChillVoter implements ProvideRoleHierarchyInter
// subject is null. We check that at least one center is reachable
$centers = $this->authorizationHelper
->getReachableCenters($token->getUser(), new Role($attribute));
->getReachableCenters($token->getUser(), $attribute);
return count($centers) > 0;

View File

@ -116,7 +116,7 @@ class ParticipationVoter extends AbstractChillVoter implements ProvideRoleHierar
// subject is null. We check that at least one center is reachable
$centers = $this->authorizationHelper
->getReachableCenters($token->getUser(), new Role($attribute));
->getReachableCenters($token->getUser(), $attribute);
return count($centers) > 0;

View File

@ -701,7 +701,7 @@ class CRUDController extends AbstractController
/**
* @return \Chill\MainBundle\Entity\Center[]
*/
protected function getReachableCenters(Role $role, ?Scope $scope = null)
protected function getReachableCenters(string $role, ?Scope $scope = null)
{
return $this->getAuthorizationHelper()
->getReachableCenters($this->getUser(), $role, $scope);

View File

@ -52,7 +52,7 @@ class NotificationType extends AbstractType
'empty_collection_explain' => 'notification.Any email',
'entry_options' => [
'constraints' => [
new NotNull(), new NotBlank(), new Email(['checkMX' => true]),
new NotNull(), new NotBlank(), new Email(),
],
'label' => 'Email',
],

View File

@ -97,7 +97,7 @@ trait AppendScopeChoiceTypeTrait
*/
protected function appendScopeChoices(
FormBuilderInterface $builder,
Role $role,
string $role,
Center $center,
User $user,
AuthorizationHelper $authorizationHelper,

View File

@ -52,7 +52,7 @@ class ScopePickerType extends AbstractType
array_filter(
$this->authorizationHelper->getReachableScopes(
$this->security->getUser(),
$options['role'] instanceof Role ? $options['role']->getRole() : $options['role'],
$options['role'],
$options['center']
),
static fn (Scope $s) => $s->isActive()
@ -92,6 +92,6 @@ class ScopePickerType extends AbstractType
->setAllowedTypes('center', [Center::class, 'array', 'null'])
// create ``role` option
->setRequired('role')
->setAllowedTypes('role', ['string', Role::class]);
->setAllowedTypes('role', ['string']);
}
}

View File

@ -63,7 +63,7 @@ class UserPickerType extends AbstractType
->setAllowedTypes('center', [\Chill\MainBundle\Entity\Center::class, 'null', 'array'])
// create ``role` option
->setRequired('role')
->setAllowedTypes('role', ['string', \Symfony\Component\Security\Core\Role\Role::class]);
->setAllowedTypes('role', ['string']);
$resolver
->setDefault('having_permissions_group_flag', null)
@ -74,11 +74,7 @@ class UserPickerType extends AbstractType
->setDefault('scope', null)
->setAllowedTypes('scope', [Scope::class, 'array', 'null'])
->setNormalizer('choices', function (Options $options) {
if ($options['role'] instanceof Role) {
$role = $options['role']->getRole();
} else {
$role = $options['role'];
}
$role = $options['role'];
$users = $this->userACLAwareRepository
->findUsersByReachedACL($role, $options['center'], $options['scope'], true);

View File

@ -159,7 +159,7 @@ class WorkflowStepType extends AbstractType
'empty_collection_explain' => 'workflow.Any email',
'entry_options' => [
'constraints' => [
new NotNull(), new NotBlank(), new Email(['checkMX' => true]),
new NotNull(), new NotBlank(), new Email(),
],
'label' => 'Email',
],

View File

@ -19,7 +19,6 @@ use Chill\MainBundle\Security\ParentRoleHelper;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\MainBundle\Security\Resolver\ScopeResolverDispatcher;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\User\UserInterface;
use Traversable;
use UnexpectedValueException;
@ -48,10 +47,6 @@ class AuthorizationHelper implements AuthorizationHelperInterface
{
$results = [];
if ($role instanceof Role) {
$role = $role->getRole();
}
foreach ($centers as $center) {
if ($this->userCanReachCenter($user, $center)) {
$results[] = $center;
@ -94,10 +89,6 @@ class AuthorizationHelper implements AuthorizationHelperInterface
*/
public function getReachableCenters(UserInterface $user, string $role, ?Scope $scope = null): array
{
if ($role instanceof Role) {
$role = $role->getRole();
}
if (!$user instanceof User) {
return [];
}
@ -135,7 +126,7 @@ class AuthorizationHelper implements AuthorizationHelperInterface
* @param Center|Center[] $center
* @return Scope[]
*/
public function getReachableCircles(UserInterface $user, \Symfony\Component\Security\Core\Role\Role|string $role, \Chill\MainBundle\Entity\Center|array $center)
public function getReachableCircles(UserInterface $user, string $role, \Chill\MainBundle\Entity\Center|array $center)
{
$scopes = [];
@ -147,10 +138,6 @@ class AuthorizationHelper implements AuthorizationHelperInterface
return $scopes;
}
if ($role instanceof Role) {
$role = $role->getRole();
}
foreach ($user->getGroupCenters() as $groupCenter) {
if ($center->getId() === $groupCenter->getCenter()->getId()) {
//iterate on permissionGroup
@ -219,11 +206,11 @@ class AuthorizationHelper implements AuthorizationHelperInterface
* the scope is taken into account.
*
* @param mixed $entity the entity may also implement HasScopeInterface
* @param Role|string $attribute
* @param string $attribute
*
* @return bool true if the user has access
*/
public function userHasAccess(User $user, mixed $entity, \Symfony\Component\Security\Core\Role\Role|string $attribute)
public function userHasAccess(User $user, mixed $entity, string $attribute)
{
$centers = $this->centerResolverManager->resolveCenters($entity);

View File

@ -418,7 +418,7 @@ final class ExportManagerTest extends KernelTestCase
$export = $this->prophet->prophesize();
$export->willImplement(ExportInterface::class);
$export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY'));
$export->requiredRole()->willReturn('CHILL_STAT_DUMMY');
$result = $exportManager->isGrantedForElement($export->reveal(), null, [$center, $centerB]);
@ -439,7 +439,7 @@ final class ExportManagerTest extends KernelTestCase
$export = $this->prophet->prophesize();
$export->willImplement(\Chill\MainBundle\Export\ExportInterface::class);
$export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY'));
$export->requiredRole()->willReturn('CHILL_STAT_DUMMY');
$result = $exportManager->isGrantedForElement($export->reveal(), null, []);
@ -473,7 +473,7 @@ final class ExportManagerTest extends KernelTestCase
$export = $this->prophet->prophesize();
$export->willImplement(ExportInterface::class);
$export->requiredRole()->willReturn(new Role('CHILL_STAT_DUMMY'));
$export->requiredRole()->willReturn('CHILL_STAT_DUMMY');
$result = $exportManager->isGrantedForElement(
$modifier->reveal(),

View File

@ -294,9 +294,7 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI
*
*
* @ORM\Column(type="text", nullable=true)
* @Assert\Email(
* checkMX=true
* )
* @Assert\Email()
*/
private string $email = '';

View File

@ -81,7 +81,7 @@ class AccompanyingPeriodType extends AbstractType
if ('visible' === $this->config['user']) {
$builder->add('user', UserPickerType::class, [
'center' => $options['center'],
'role' => new Role(PersonVoter::SEE),
'role' => PersonVoter::SEE,
]);
}

View File

@ -107,7 +107,7 @@ class PickPersonType extends AbstractType
->addAllowedTypes('centers', ['array', Center::class, 'null'])
->setDefault('centers', null)
->setDefined('role')
->addAllowedTypes('role', [Role::class, 'null'])
->addAllowedTypes('role', ['string', 'null'])
->setDefault('role', null);
// add the default options

View File

@ -133,7 +133,7 @@ final class PickPersonTypeTest extends KernelTestCase
$this->markTestSkipped('need to inject locale into url generator without request');
$form = $this->formFactory
->createBuilder(PickPersonType::class, null, [
'role' => new \Symfony\Component\Security\Core\Role\Role('INVALID'),
'role' => 'INVALID',
])
->getForm();

View File

@ -58,7 +58,7 @@ class PersonListWidget implements WidgetInterface
// show only the person from the authorized centers
$and = $qb->expr()->andX();
$centers = $this->authorizationHelper
->getReachableCenters($this->getUser(), new Role(PersonVoter::SEE));
->getReachableCenters($this->getUser(), PersonVoter::SEE);
$and->add($qb->expr()->in('person.center', ':centers'));
$qb->setParameter('centers', $centers);

View File

@ -216,7 +216,7 @@ class ReportController extends AbstractController
$reachableScopes = $this->authorizationHelper
->getReachableScopes(
$this->getUser(),
new Role('CHILL_REPORT_SEE'),
'CHILL_REPORT_SEE',
$person->getCenter()
);
@ -529,7 +529,7 @@ class ReportController extends AbstractController
),
'method' => 'POST',
'cFGroup' => $cFGroup,
'role' => new Role('CHILL_REPORT_CREATE'),
'role' => 'CHILL_REPORT_CREATE',
'center' => $person->getCenter(),
]);
}
@ -551,7 +551,7 @@ class ReportController extends AbstractController
),
'method' => 'PUT',
'cFGroup' => $entity->getCFGroup(),
'role' => new Role('CHILL_REPORT_UPDATE'),
'role' => 'CHILL_REPORT_UPDATE',
'center' => $entity->getPerson()->getCenter(),
]);
}

View File

@ -73,7 +73,7 @@ class ReportSearch extends AbstractSearch implements ContainerAwareInterface
//adding join
$qb->join('r.person', 'p');
$role = new Role('CHILL_REPORT_SEE');
$role = 'CHILL_REPORT_SEE';
$reachableCenters = $this->helper->getReachableCenters($this->user, $role);
$whereElement = $qb->expr()->orX();

View File

@ -116,7 +116,7 @@ class SingleTaskListType extends AbstractType
'centers' => $this->authorizationHelper
->getReachableCenters(
$this->tokenStorage->getToken()->getUser(),
new Role(TaskVoter::SHOW)
TaskVoter::SHOW
),
'required' => false,
'label' => 'Associated person',
@ -160,7 +160,7 @@ class SingleTaskListType extends AbstractType
protected function getReachablesCenters()
{
$user = $this->tokenStorage->getToken()->getUser();
$role = new Role(TaskVoter::SHOW);
$role = TaskVoter::SHOW;
return $this->authorizationHelper->getReachableCenters($user, $role);
}
@ -169,7 +169,7 @@ class SingleTaskListType extends AbstractType
{
$qb = $this->em->createQueryBuilder();
$user = $this->tokenStorage->getToken()->getUser();
$role = new Role(TaskVoter::SHOW);
$role = TaskVoter::SHOW;
$centers = $this->authorizationHelper->getReachableCenters($user, $role);
$qb->select('DISTINCT task.type AS type')
@ -237,7 +237,7 @@ class SingleTaskListType extends AbstractType
{
$qb = $this->em->createQueryBuilder();
$user = $this->tokenStorage->getToken()->getUser();
$role = new Role(TaskVoter::SHOW);
$role = TaskVoter::SHOW;
$centers = $this->authorizationHelper->getReachableCenters($user, $role);
$qb->select('DISTINCT user')

View File

@ -78,6 +78,6 @@ class SingleTaskType extends AbstractType
{
$resolver
->setRequired('role')
->setAllowedTypes('role', [Role::class, 'string']);
->setAllowedTypes('role', ['string']);
}
}

View File

@ -159,7 +159,7 @@ class SingleTaskRepository extends EntityRepository
. '`setAuthorizationHelper`');
}
$role = new Role(TaskVoter::SHOW);
$role = TaskVoter::SHOW;
$qb->join('st.person', 'p');
$centers = $this->authorizationHelper

View File

@ -168,7 +168,7 @@ class TaskLifeCycleEventTimelineProvider implements TimelineProviderInterface
foreach (
$this->authorizationHelper->getReachableCenters(
$this->security->getUser(),
new Role(ActivityVoter::SEE_DETAILS)
ActivityVoter::SEE_DETAILS
) as $center
) {
if (false === in_array($center, $centers, true)) {
@ -181,7 +181,7 @@ class TaskLifeCycleEventTimelineProvider implements TimelineProviderInterface
// we loop over circles
$circles = $this->authorizationHelper->getReachableCircles(
$this->security->getUser(),
new Role(ActivityVoter::SEE_DETAILS),
ActivityVoter::SEE_DETAILS,
$center
);
$circleIds = [];
@ -234,7 +234,7 @@ class TaskLifeCycleEventTimelineProvider implements TimelineProviderInterface
// we loop over circles
$circles = $this->authorizationHelper->getReachableCircles(
$this->security->getUser(),
new Role(ActivityVoter::SEE_DETAILS),
ActivityVoter::SEE_DETAILS,
$personArg->getCenter()
);

View File

@ -194,7 +194,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface, \Strin
/**
* @ORM\Column(name="email", type="string", length=255, nullable=true)
* @Assert\Email(checkMX=false)
* @Assert\Email()
* @Groups({"read", "write", "docgen:read", "docgen:read:3party:parent"})
*/
private ?string $email = null;

View File

@ -91,7 +91,7 @@ class ThirdPartyVoter extends AbstractChillVoter implements ProvideRoleHierarchy
return true;
$centers = $this->authorizationHelper
->getReachableCenters($user, new Role($attribute));
->getReachableCenters($user, $attribute);
if (null === $subject) {
return count($centers) > 0;