src/Security/Voter/AdminDriveVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\ChannelUserData\AdminChannelUserData;
  4. use App\Entity\ChannelUserData\AdminChannelUserData\AdminDriveFile;
  5. use Nellapp\Bundle\SDKBundle\Permission\Enum\ChannelUserPermissionEnum;
  6. use Nellapp\Bundle\SDKBundle\Permission\Security\Voter\UserOwnerVoter;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class AdminDriveVoter extends Voter
  11. {
  12. public const VIEW = 'view';
  13. public const DELETE = 'delete';
  14. public const PUT = 'put';
  15. public const POST = 'post';
  16. public const DOWNLOAD = 'download';
  17. public function __construct(
  18. private Security $security,
  19. ) {}
  20. protected function supports(string $attribute, $subject): bool
  21. {
  22. return ($subject instanceof AdminChannelUserData || $subject instanceof AdminDriveFile) && in_array($attribute, [self::VIEW, self::DELETE, self::PUT, self::POST, self::DOWNLOAD]);
  23. }
  24. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  25. {
  26. if ($subject instanceof AdminChannelUserData) {
  27. $channelUserData = $subject;
  28. $file = null;
  29. } else {
  30. $channelUserData = $subject->getChannelUserData();
  31. $file = $subject;
  32. }
  33. $channel = $channelUserData->getChannel();
  34. $isFileOwner = $file !== null && $this->security->isGranted(UserOwnerVoter::USER_OWNER, $file);
  35. $isDriveOwner = $this->security->isGranted(UserOwnerVoter::USER_OWNER, $channelUserData);
  36. if ($attribute === self::VIEW) {
  37. return
  38. $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DSF_DRIVE_SHOW, $channel)
  39. || $isDriveOwner && (!($file !== null) || $file->isShare())
  40. ;
  41. } else if ($attribute === self::DELETE) {
  42. return
  43. $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DSF_DRIVE_DELETE, $channel)
  44. || $isFileOwner
  45. ;
  46. } else if ($attribute === self::PUT) {
  47. return
  48. $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DSF_DRIVE_DELETE, $channel)
  49. || $isFileOwner
  50. ;
  51. } else if ($attribute === self::POST) {
  52. return
  53. $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DSF_DRIVE_IMPORT, $channel)
  54. || $isDriveOwner
  55. ;
  56. } else if ($attribute === self::DOWNLOAD) {
  57. return
  58. $this->security->isGranted(ChannelUserPermissionEnum::CHANNEL_USER_PERM_DSF_DRIVE_DOWNLOAD, $channel)
  59. || $isDriveOwner && $file?->isShare()
  60. ;
  61. }
  62. return false;
  63. }
  64. }