mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Household/composition add + fixes household composition editor
This commit is contained in:
@@ -11,7 +11,66 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\PersonBundle\Security\Authorization;
|
||||
|
||||
class HouseholdVoter
|
||||
use Chill\PersonBundle\Entity\Household\Household;
|
||||
use Chill\PersonBundle\Entity\Household\HouseholdMember;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use UnexpectedValueException;
|
||||
use function in_array;
|
||||
|
||||
class HouseholdVoter extends Voter
|
||||
{
|
||||
public const SHOW = PersonVoter::SEE;
|
||||
public const EDIT = 'CHILL_PERSON_HOUSEHOLD_EDIT';
|
||||
|
||||
public const SEE = 'CHILL_PERSON_HOUSEHOLD_SEE';
|
||||
|
||||
/**
|
||||
* @deprecated use @see{self::SEE} instead
|
||||
*/
|
||||
public const SHOW = self::SEE;
|
||||
|
||||
private const ALL = [
|
||||
self::EDIT, self::SEE,
|
||||
];
|
||||
|
||||
private Security $security;
|
||||
|
||||
public function __construct(Security $security)
|
||||
{
|
||||
$this->security = $security;
|
||||
}
|
||||
|
||||
protected function supports($attribute, $subject)
|
||||
{
|
||||
return $subject instanceof Household
|
||||
&& in_array($attribute, self::ALL, true);
|
||||
}
|
||||
|
||||
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
|
||||
{
|
||||
switch ($attribute) {
|
||||
case self::SEE:
|
||||
return $this->checkAssociatedMembersRole($subject, PersonVoter::SEE);
|
||||
|
||||
case self::EDIT:
|
||||
return $this->checkAssociatedMembersRole($subject, PersonVoter::UPDATE);
|
||||
|
||||
default:
|
||||
throw new UnexpectedValueException('attribute not supported');
|
||||
}
|
||||
}
|
||||
|
||||
private function checkAssociatedMembersRole(Household $household, string $attribute): bool
|
||||
{
|
||||
foreach ($household->getCurrentMembers()->map(static function (HouseholdMember $member) {
|
||||
return $member->getPerson();
|
||||
}) as $person) {
|
||||
if ($this->security->isGranted($attribute, $person)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user