Household/composition add + fixes household composition editor

This commit is contained in:
2022-01-24 10:59:00 +00:00
parent 2b47868d88
commit 53b3f98bba
35 changed files with 1489 additions and 63 deletions

View File

@@ -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;
}
}