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
21 changed files with 187 additions and 255 deletions

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Chill\MainBundle\Security\Authorization;
use Chill\MainBundle\Entity\User;
@@ -50,7 +52,7 @@ final class DefaultVoterHelper implements VoterHelperInterface
}
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(

View File

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

View File

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

View File

@@ -1,42 +1,27 @@
<?php
/*
* Copyright (C) 2017 Champs Libres Cooperative <info@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/>.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Security;
/**
* Give a hierarchy for the role.
*
* This hierarchy allow to sort roles, which is useful in UI
* Give a hierarchy for the role.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* This hierarchy allow to sort roles, which is useful in UI
*/
interface ProvideRoleHierarchyInterface extends ProvideRoleInterface
{
/**
* Return an array of roles, where keys are the hierarchy, and values
* an array of roles.
*
* Example:
*
*
* Example:
*
* ```
* [ '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,53 +1,36 @@
<?php
/*
* 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/>.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Security;
/**
* Declare role
*
*
* The role are added to the configuration at compile time.
*
* The implemented object must be declared as a service and tagged as
*
*
* The implemented object must be declared as a service and tagged as
*
* <pre>
* my_role_declaration:
* # ...
* tags:
* - { name: chill.role }
* </pre>
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
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)
*/
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
*/
public function getRolesWithoutScope();
public function getRolesWithoutScope(): array;
}

View File

@@ -20,7 +20,7 @@
namespace Chill\MainBundle\Security;
/**
*
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
@@ -31,63 +31,57 @@ class RoleProvider
* @var ProvideRoleInterface[]
*/
private $providers = array();
/**
* an array where keys are the role, and value is the title
* for the given role.
*
*
* Null when not initialized.
*
* @var array|null
*/
private $rolesTitlesCache = null;
/**
* Add a role provider
*
*
* @internal This function is called by the dependency injector: it inject provider
* @param \Chill\MainBundle\Security\ProvideRoleInterface $provider
*/
public function addProvider(ProvideRoleInterface $provider)
public function addProvider(ProvideRoleInterface $provider)
{
$this->providers[] = $provider;
}
/**
*
* @return string[] the roles as string
*/
public function getRoles()
public function getRoles(): array
{
$roles = array();
$roles = [];
foreach ($this->providers as $provider) {
if ($provider->getRoles() !== NULL) {
$roles = array_merge($roles, $provider->getRoles());
}
}
return $roles;
}
/**
*
* @return string[] the roles as string
*/
public function getRolesWithoutScopes()
public function getRolesWithoutScopes(): array
{
$roles = array();
$roles = [];
foreach ($this->providers as $provider) {
if ($provider->getRolesWithoutScope() !== NULL) {
$roles = array_merge($roles, $provider->getRolesWithoutScope());
}
}
return $roles;
}
/**
* initialize the array for caching role and titles
*
*
*/
private function initializeRolesTitlesCache()
{
@@ -95,7 +89,7 @@ class RoleProvider
if ($this->rolesTitlesCache !== null) {
return;
}
foreach ($this->providers as $provider) {
if ($provider instanceof ProvideRoleHierarchyInterface) {
foreach ($provider->getRolesWithHierarchy() as $title => $roles) {
@@ -106,31 +100,31 @@ class RoleProvider
} else {
if ($provider->getRoles() !== null) {
$this->rolesTitlesCache = \array_merge(
$this->rolesTitlesCache,
$this->rolesTitlesCache,
\array_fill_keys($provider->getRoles(), null)
);
}
}
}
}
/**
* Get the title for each role.
*
*
* @param string $role
* @return string the title of the role
*/
public function getRoleTitle($role)
{
$this->initializeRolesTitlesCache();
if (! \array_key_exists($role, $this->rolesTitlesCache)) {
// this case might happens when the role is not described in
// this case might happens when the role is not described in
// `getRolesWithHierarchy`
return null;
}
return $this->rolesTitlesCache[$role];
}
}