vendor/nellapp/sdk-bundle/src/Permission/Security/Voter/ChannelUserDataVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace Nellapp\Bundle\SDKBundle\Permission\Security\Voter;
  3. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  4. use Nellapp\Bundle\SDKBundle\Channel\Entity\ChannelInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class ChannelUserDataVoter extends Voter
  8. {
  9. const IS_CHANNEL_USER_DATA = 'IS_CHANNEL_USER_DATA';
  10. const IS_ADMIN = 'IS_ADMIN'; // ChannelUserData that are not learner
  11. const IS_LEARNER = 'IS_LEARNER';
  12. public function supportsType(string $subjectType): bool
  13. {
  14. return is_subclass_of($subjectType, ChannelInterface::class);
  15. }
  16. public function supportsAttribute(string $attribute): bool
  17. {
  18. return $attribute === self::IS_CHANNEL_USER_DATA || $attribute === self::IS_LEARNER || $attribute === self::IS_ADMIN;
  19. }
  20. protected function supports(string $attribute, $subject): bool
  21. {
  22. return $this->supportsAttribute($attribute) && $this->supportsType($subject::class);
  23. }
  24. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  25. {
  26. $user = $token->getUser();
  27. if (!$user instanceof UserInterface) {
  28. return false;
  29. }
  30. $channelUserData = $user->getChannelUserDataByChannel($subject);
  31. if ($channelUserData === null) {
  32. return false;
  33. }
  34. if ($channelUserData->isActive() !== true) {
  35. return false;
  36. }
  37. if ($attribute === self::IS_CHANNEL_USER_DATA) {
  38. return true;
  39. }
  40. if ($attribute === self::IS_ADMIN) {
  41. return $channelUserData->isLearner() === false;
  42. }
  43. return $channelUserData->isLearner();
  44. }
  45. }