add missing roles and adapt role voter for exports houshold and activity

This commit is contained in:
2022-09-11 22:44:09 +02:00
parent 78ea990189
commit d716e0c2c2
3 changed files with 47 additions and 32 deletions

View File

@@ -11,6 +11,10 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Security\Authorization;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Security\Authorization\VoterHelperFactoryInterface;
use Chill\MainBundle\Security\Authorization\VoterHelperInterface;
use Chill\MainBundle\Security\ProvideRoleHierarchyInterface;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -19,7 +23,7 @@ use Symfony\Component\Security\Core\Security;
use UnexpectedValueException;
use function in_array;
class HouseholdVoter extends Voter
class HouseholdVoter extends Voter implements ProvideRoleHierarchyInterface
{
public const EDIT = 'CHILL_PERSON_HOUSEHOLD_EDIT';
@@ -36,17 +40,40 @@ class HouseholdVoter extends Voter
self::EDIT, self::SEE,
];
private VoterHelperInterface $helper;
private Security $security;
public function __construct(Security $security)
public function __construct(Security $security, VoterHelperFactoryInterface $voterHelperFactory)
{
$this->security = $security;
$this->helper = $voterHelperFactory
->generate(self::class)
->addCheckFor(Center::class, [self::STATS])
->build();
}
public function getRolesWithHierarchy(): array
{
return [ 'Person' => $this->getRoles() ];
}
public function getRoles(): array
{
return [self::STATS];
}
public function getRolesWithoutScope(): array
{
return $this->getRoles();
}
protected function supports($attribute, $subject)
{
return $subject instanceof Household
&& in_array($attribute, self::ALL, true);
return ($subject instanceof Household
&& in_array($attribute, self::ALL, true))
|| $this->helper->supports($attribute, $subject)
;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
@@ -58,6 +85,9 @@ class HouseholdVoter extends Voter
case self::EDIT:
return $this->checkAssociatedMembersRole($subject, PersonVoter::UPDATE);
case self::STATS:
return $this->voteOnAttribute($attribute, $subject, $token);
default:
throw new UnexpectedValueException('attribute not supported');
}