src/Security/Voter/TeamVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Team;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. class TeamVoter extends Voter
  10. {
  11.     public const VIEW 'VIEW';
  12.     public const EDIT 'EDIT';
  13.     public const DELETE 'DELETE';
  14.     public const MANAGE_MEMBERS 'MANAGE_MEMBERS';
  15.     public const MANAGE_SETTINGS 'MANAGE_SETTINGS';
  16.     private $security;
  17.     public function __construct(Security $security)
  18.     {
  19.         $this->security $security;
  20.     }
  21.     protected function supports(string $attributemixed $subject): bool
  22.     {
  23.         // if the attribute isn't one we support, return false
  24.         if (!in_array($attribute, [
  25.             self::VIEW,
  26.             self::EDIT,
  27.             self::DELETE,
  28.             self::MANAGE_MEMBERS,
  29.             self::MANAGE_SETTINGS
  30.         ])) {
  31.             return false;
  32.         }
  33.         // only vote on `Team` objects
  34.         if (!$subject instanceof Team) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  40.     {
  41.         $user $token->getUser();
  42.         // if the user is anonymous, do not grant access
  43.         if (!$user instanceof UserInterface) {
  44.             return false;
  45.         }
  46.         // Admins and company managers can do anything
  47.         if ($this->security->isGranted('ROLE_ADMIN') || $this->security->isGranted('ROLE_COMPANY_MANAGER')) {
  48.             return true;
  49.         }
  50.         /** @var Team $team */
  51.         $team $subject;
  52.         
  53.         /** @var User $user */
  54.         $user $token->getUser();
  55.         
  56.         // Find the user's relationship with this team
  57.         $userTeam null;
  58.         foreach ($team->getUserTeams() as $ut) {
  59.             if ($ut->getUser() === $user) {
  60.                 $userTeam $ut;
  61.                 break;
  62.             }
  63.         }
  64.         
  65.         // If user is not a member of the team, they can only view
  66.         if ($userTeam === null) {
  67.             return $attribute === self::VIEW;
  68.         }
  69.         
  70.         // Check permissions based on role and requested action
  71.         switch ($attribute) {
  72.             case self::VIEW:
  73.                 // All team members can view
  74.                 return true;
  75.                 
  76.             case self::EDIT:
  77.                 // Team captains, coaches, and managers can edit
  78.                 return $userTeam->isTeamLeader();
  79.                 
  80.             case self::MANAGE_MEMBERS:
  81.                 // Only coaches and managers can manage members
  82.                 return $userTeam->canManageMembers();
  83.                 
  84.             case self::MANAGE_SETTINGS:
  85.                 // Only managers can manage team settings
  86.                 return $userTeam->canManageTeam();
  87.                 
  88.             case self::DELETE:
  89.                 // Only managers can delete teams
  90.                 return $userTeam->canManageTeam();
  91.         }
  92.         
  93.         return false;
  94.     }
  95. }