fix: Strict types interfaces: VoterHelperInterface, ProvideRoleHierarchyInterface and ProvideRoleInterface.

This commit is contained in:
Pol Dellaiera 2021-11-23 10:40:34 +01:00
parent 05dda33a7a
commit 328b4c4596
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
21 changed files with 187 additions and 255 deletions

View File

@ -75,17 +75,17 @@ class ActivityStatsVoter extends AbstractChillVoter implements ProvideRoleHierar
return $this->helper->userHasAccess($user, $object, $attribute); return $this->helper->userHasAccess($user, $object, $attribute);
} }
public function getRoles() public function getRoles(): array
{ {
return $this->getAttributes(); return $this->getAttributes();
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return $this->getAttributes(); return $this->getAttributes();
} }
public function getRolesWithHierarchy() public function getRolesWithHierarchy(): array
{ {
return ['Activity' => $this->getRoles()]; return ['Activity' => $this->getRoles()];
} }

View File

@ -1,21 +1,6 @@
<?php <?php
/* declare(strict_types=1);
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\ActivityBundle\Security\Authorization; namespace Chill\ActivityBundle\Security\Authorization;
@ -25,9 +10,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Chill\PersonBundle\Security\Authorization\PersonVoter; use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Chill\MainBundle\Security\Authorization\AbstractChillVoter; use Chill\MainBundle\Security\Authorization\AbstractChillVoter;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Security\ProvideRoleHierarchyInterface; use Chill\MainBundle\Security\ProvideRoleHierarchyInterface;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\ActivityBundle\Entity\Activity; use Chill\ActivityBundle\Entity\Activity;
@ -35,9 +18,6 @@ use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
/**
* Voter for Activity class
*/
class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyInterface class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyInterface
{ {
/** /**
@ -46,7 +26,7 @@ class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyIn
* *
* It is safe for usage in template and controller * It is safe for usage in template and controller
*/ */
const CREATE = 'CHILL_ACTIVITY_CREATE'; public const CREATE = 'CHILL_ACTIVITY_CREATE';
/** /**
* role to allow to create an activity associated win an accompanying course. * role to allow to create an activity associated win an accompanying course.
@ -55,7 +35,7 @@ class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyIn
* *
* @internal * @internal
*/ */
const CREATE_ACCOMPANYING_COURSE = 'CHILL_ACTIVITY_CREATE_ACCOMPANYING_COURSE'; public const CREATE_ACCOMPANYING_COURSE = 'CHILL_ACTIVITY_CREATE_ACCOMPANYING_COURSE';
/** /**
* role to allow to create an activity associated with a person * role to allow to create an activity associated with a person
@ -64,13 +44,13 @@ class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyIn
* *
* @internal * @internal
*/ */
const CREATE_PERSON = 'CHILL_ACTIVITY_CREATE_PERSON'; public const CREATE_PERSON = 'CHILL_ACTIVITY_CREATE_PERSON';
const SEE = 'CHILL_ACTIVITY_SEE'; public const SEE = 'CHILL_ACTIVITY_SEE';
const SEE_DETAILS = 'CHILL_ACTIVITY_SEE_DETAILS'; public const SEE_DETAILS = 'CHILL_ACTIVITY_SEE_DETAILS';
const UPDATE = 'CHILL_ACTIVITY_UPDATE'; public const UPDATE = 'CHILL_ACTIVITY_UPDATE';
const DELETE = 'CHILL_ACTIVITY_DELETE'; public const DELETE = 'CHILL_ACTIVITY_DELETE';
const FULL = 'CHILL_ACTIVITY_FULL'; public const FULL = 'CHILL_ACTIVITY_FULL';
private const ALL = [ private const ALL = [
self::CREATE, self::CREATE,
@ -97,13 +77,12 @@ class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyIn
->build(); ->build();
} }
protected function supports($attribute, $subject): bool
protected function supports($attribute, $subject)
{ {
return $this->voterHelper->supports($attribute, $subject); return $this->voterHelper->supports($attribute, $subject);
} }
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{ {
if (!$token->getUser() instanceof User) { if (!$token->getUser() instanceof User) {
return false; return false;
@ -132,7 +111,7 @@ class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyIn
$attribute = self::CREATE_ACCOMPANYING_COURSE; $attribute = self::CREATE_ACCOMPANYING_COURSE;
} }
} else { } else {
throw new \RuntimeException("could not determine context of activity"); throw new \RuntimeException('Could not determine context of activity.');
} }
} elseif ($subject instanceof AccompanyingPeriod) { } elseif ($subject instanceof AccompanyingPeriod) {
if (AccompanyingPeriod::STEP_CLOSED === $subject->getStep()) { if (AccompanyingPeriod::STEP_CLOSED === $subject->getStep()) {
@ -155,8 +134,7 @@ class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyIn
return $this->voterHelper->voteOnAttribute($attribute, $subject, $token); return $this->voterHelper->voteOnAttribute($attribute, $subject, $token);
} }
public function getRoles(): array
public function getRoles()
{ {
return [ return [
self::CREATE_PERSON, self::CREATE_PERSON,
@ -167,14 +145,12 @@ class ActivityVoter extends AbstractChillVoter implements ProvideRoleHierarchyIn
]; ];
} }
public function getRolesWithoutScope(): array
public function getRolesWithoutScope()
{ {
return []; return [];
} }
public function getRolesWithHierarchy(): array
public function getRolesWithHierarchy()
{ {
return ['Activity' => $this->getRoles()]; return ['Activity' => $this->getRoles()];
} }

View File

@ -61,7 +61,7 @@ class BudgetElementVoter extends AbstractChillVoter implements ProvideRoleHierar
->userHasAccess($user, $subject, new Role($attribute)); ->userHasAccess($user, $subject, new Role($attribute));
} }
public function getRoles() public function getRoles(): array
{ {
return self::ROLES; return self::ROLES;
} }
@ -71,7 +71,7 @@ class BudgetElementVoter extends AbstractChillVoter implements ProvideRoleHierar
return ['Budget elements' => self::ROLES]; return ['Budget elements' => self::ROLES];
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return self::ROLES; return self::ROLES;
} }

View File

@ -47,7 +47,7 @@ class AccompanyingCourseDocumentVoter extends AbstractChillVoter implements Prov
->build(); ->build();
} }
public function getRoles() public function getRoles(): array
{ {
return [ return [
self::CREATE, self::CREATE,
@ -91,12 +91,12 @@ class AccompanyingCourseDocumentVoter extends AbstractChillVoter implements Prov
return $this->voterHelper->voteOnAttribute($attribute, $subject, $token); return $this->voterHelper->voteOnAttribute($attribute, $subject, $token);
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return array(); return [];
} }
public function getRolesWithHierarchy() public function getRolesWithHierarchy(): array
{ {
return ['accompanyingCourseDocument' => $this->getRoles()]; return ['accompanyingCourseDocument' => $this->getRoles()];
} }

View File

@ -40,7 +40,7 @@ class PersonDocumentVoter extends AbstractChillVoter implements ProvideRoleHiera
->build(); ->build();
} }
public function getRoles() public function getRoles(): array
{ {
return [ return [
self::CREATE, self::CREATE,
@ -79,13 +79,12 @@ class PersonDocumentVoter extends AbstractChillVoter implements ProvideRoleHiera
return $this->voterHelper->voteOnAttribute($attribute, $subject, $token); return $this->voterHelper->voteOnAttribute($attribute, $subject, $token);
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return array(); return [];
} }
public function getRolesWithHierarchy(): array
public function getRolesWithHierarchy()
{ {
return ['PersonDocument' => $this->getRoles()]; return ['PersonDocument' => $this->getRoles()];
} }

View File

@ -160,12 +160,7 @@ class EventType
$this->roles->removeElement($role); $this->roles->removeElement($role);
} }
/** public function getRoles(): array
* Get roles
*
* @return Collection
*/
public function getRoles()
{ {
return $this->roles; return $this->roles;
} }

View File

@ -128,20 +128,19 @@ class EventVoter extends AbstractChillVoter implements ProvideRoleHierarchyInter
} }
public function getRoles(): array
public function getRoles()
{ {
return self::ROLES; return self::ROLES;
} }
public function getRolesWithHierarchy() public function getRolesWithHierarchy(): array
{ {
return [ return [
'Event' => self::ROLES 'Event' => self::ROLES
]; ];
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return []; return [];
} }

View File

@ -127,19 +127,19 @@ class ParticipationVoter extends AbstractChillVoter implements ProvideRoleHierar
} }
public function getRoles() public function getRoles(): array
{ {
return self::ROLES; return self::ROLES;
} }
public function getRolesWithHierarchy() public function getRolesWithHierarchy(): array
{ {
return [ return [
'Event' => self::ROLES 'Event' => self::ROLES
]; ];
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return []; return [];
} }

View File

@ -61,7 +61,7 @@ class FamilyMemberVoter extends AbstractChillVoter implements ProvideRoleHierarc
->userHasAccess($user, $subject, new Role($attribute)); ->userHasAccess($user, $subject, new Role($attribute));
} }
public function getRoles() public function getRoles(): array
{ {
return self::ROLES; return self::ROLES;
} }
@ -71,7 +71,7 @@ class FamilyMemberVoter extends AbstractChillVoter implements ProvideRoleHierarc
return ['Family Members' => self::ROLES]; return ['Family Members' => self::ROLES];
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return self::ROLES; return self::ROLES;
} }

View File

@ -203,12 +203,9 @@ class User implements AdvancedUserInterface {
*/ */
public function eraseCredentials() {} public function eraseCredentials() {}
/** public function getRoles(): array
* @return array
*/
public function getRoles()
{ {
return array('ROLE_USER'); return ['ROLE_USER'];
} }
/** /**

View File

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace Chill\MainBundle\Security\Authorization; namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
@ -50,7 +52,7 @@ final class DefaultVoterHelper implements VoterHelperInterface
} }
if (NULL === $subject) { if (NULL === $subject) {
return 0 < count($this->authorizationHelper->getReachableCenters($token->getUser(), $attribute, null)); return [] !== $this->authorizationHelper->getReachableCenters($token->getUser(), $attribute, null);
} }
return $this->authorizationHelper->userHasAccess( return $this->authorizationHelper->userHasAccess(

View File

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace Chill\MainBundle\Security\Authorization; namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;

View File

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace Chill\MainBundle\Security\Authorization; namespace Chill\MainBundle\Security\Authorization;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@ -8,5 +10,5 @@ interface VoterHelperInterface
{ {
public function supports($attribute, $subject): bool; public function supports($attribute, $subject): bool;
public function voteOnAttribute($attribute, $subject, TokenInterface $token); public function voteOnAttribute($attribute, $subject, TokenInterface $token): bool;
} }

View File

@ -1,28 +1,13 @@
<?php <?php
/*
* Copyright (C) 2017 Champs Libres Cooperative <info@champs-libres.coop> declare(strict_types=1);
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Security; namespace Chill\MainBundle\Security;
/** /**
* Give a hierarchy for the role. * Give a hierarchy for the role.
* *
* This hierarchy allow to sort roles, which is useful in UI * This hierarchy allow to sort roles, which is useful in UI
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/ */
interface ProvideRoleHierarchyInterface extends ProvideRoleInterface interface ProvideRoleHierarchyInterface extends ProvideRoleInterface
{ {
@ -36,7 +21,7 @@ interface ProvideRoleHierarchyInterface extends ProvideRoleInterface
* [ 'Title' => [ 'CHILL_FOO_SEE', 'CHILL_FOO_UPDATE' ] ] * [ 'Title' => [ 'CHILL_FOO_SEE', 'CHILL_FOO_UPDATE' ] ]
* ``` * ```
* *
* @return array where keys are the hierarchy, and values an array of roles: `[ 'title' => [ 'CHILL_FOO_SEE', 'CHILL_FOO_UPDATE' ] ]` * @return array<string, array<int, string>> Where keys are the hierarchy, and values an array of roles: `[ 'title' => [ 'CHILL_FOO_SEE', 'CHILL_FOO_UPDATE' ] ]`
*/ */
public function getRolesWithHierarchy(); public function getRolesWithHierarchy(): array;
} }

View File

@ -1,21 +1,6 @@
<?php <?php
/* declare(strict_types=1);
* Copyright (C) 2015 Julien Fastré <julien.fastre@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Security; namespace Chill\MainBundle\Security;
@ -32,22 +17,20 @@ namespace Chill\MainBundle\Security;
* tags: * tags:
* - { name: chill.role } * - { name: chill.role }
* </pre> * </pre>
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/ */
interface ProvideRoleInterface interface ProvideRoleInterface
{ {
/** /**
* return an array of role provided by the object * Return an array of role provided by the object.
* *
* @return string[] array of roles (as string) * @return string[] array of roles (as string)
*/ */
public function getRoles(); public function getRoles(): array;
/** /**
* return roles which doesn't need * Return roles which doesn't need.
* *
* @return string[] array of roles without scopes * @return string[] array of roles without scopes
*/ */
public function getRolesWithoutScope(); public function getRolesWithoutScope(): array;
} }

View File

@ -53,13 +53,10 @@ class RoleProvider
$this->providers[] = $provider; $this->providers[] = $provider;
} }
/** public function getRoles(): array
*
* @return string[] the roles as string
*/
public function getRoles()
{ {
$roles = array(); $roles = [];
foreach ($this->providers as $provider) { foreach ($this->providers as $provider) {
if ($provider->getRoles() !== NULL) { if ($provider->getRoles() !== NULL) {
$roles = array_merge($roles, $provider->getRoles()); $roles = array_merge($roles, $provider->getRoles());
@ -69,13 +66,10 @@ class RoleProvider
return $roles; return $roles;
} }
/** public function getRolesWithoutScopes(): array
*
* @return string[] the roles as string
*/
public function getRolesWithoutScopes()
{ {
$roles = array(); $roles = [];
foreach ($this->providers as $provider) { foreach ($this->providers as $provider) {
if ($provider->getRolesWithoutScope() !== NULL) { if ($provider->getRolesWithoutScope() !== NULL) {
$roles = array_merge($roles, $provider->getRolesWithoutScope()); $roles = array_merge($roles, $provider->getRolesWithoutScope());

View File

@ -94,17 +94,17 @@ class AccompanyingPeriodVoter extends AbstractChillVoter implements ProvideRole
return $this->voterHelper->voteOnAttribute($attribute, $subject, $token); return $this->voterHelper->voteOnAttribute($attribute, $subject, $token);
} }
public function getRoles() public function getRoles(): array
{ {
return self::ALL; return self::ALL;
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return []; return [];
} }
public function getRolesWithHierarchy() public function getRolesWithHierarchy(): array
{ {
return ['Accompanying period' => $this->getRoles()]; return ['Accompanying period' => $this->getRoles()];
} }

View File

@ -69,17 +69,17 @@ class PersonVoter extends AbstractChillVoter implements ProvideRoleHierarchyInte
return array(self::CREATE, self::UPDATE, self::SEE, self::STATS, self::LISTS, self::DUPLICATE); return array(self::CREATE, self::UPDATE, self::SEE, self::STATS, self::LISTS, self::DUPLICATE);
} }
public function getRoles() public function getRoles(): array
{ {
return $this->getAttributes(); return $this->getAttributes();
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return $this->getAttributes(); return $this->getAttributes();
} }
public function getRolesWithHierarchy() public function getRolesWithHierarchy(): array
{ {
return ['Person' => $this->getRoles()]; return ['Person' => $this->getRoles()];
} }

View File

@ -74,18 +74,17 @@ class ReportVoter extends AbstractChillVoter implements ProvideRoleHierarchyInte
return $this->helper->userHasAccess($token->getUser(), $subject, $attribute); return $this->helper->userHasAccess($token->getUser(), $subject, $attribute);
} }
public function getRoles(): array
public function getRoles()
{ {
return [self::CREATE, self::UPDATE, self::SEE, self::LISTS]; return [self::CREATE, self::UPDATE, self::SEE, self::LISTS];
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return array(self::LISTS); return [self::LISTS];
} }
public function getRolesWithHierarchy() public function getRolesWithHierarchy(): array
{ {
return ['Report' => $this->getRoles()]; return ['Report' => $this->getRoles()];
} }

View File

@ -127,7 +127,7 @@ final class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchy
return $this->voter->voteOnAttribute($attribute, $subject, $token); return $this->voter->voteOnAttribute($attribute, $subject, $token);
} }
public function getRoles() public function getRoles(): array
{ {
return self::ROLES; return self::ROLES;
} }
@ -139,7 +139,7 @@ final class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchy
]; ];
} }
public function getRolesWithoutScope() public function getRolesWithoutScope(): array
{ {
return []; return [];
} }

View File

@ -52,9 +52,8 @@ class ThirdPartyVoter extends AbstractChillVoter implements ProvideRoleHierarchy
* @param string $attribute * @param string $attribute
* @param ThirdParty|null $subject * @param ThirdParty|null $subject
* @param TokenInterface $token * @param TokenInterface $token
* @return type
*/ */
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{ {
return true; return true;