src/Pumukit/SchemaBundle/Services/MultimediaObjectVoter.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Pumukit\SchemaBundle\Services;
  4. use Pumukit\SchemaBundle\Document\EmbeddedBroadcast;
  5. use Pumukit\SchemaBundle\Document\MultimediaObject;
  6. use Pumukit\SchemaBundle\Document\PermissionProfile;
  7. use Pumukit\SchemaBundle\Document\User;
  8. use Pumukit\WebTVBundle\PumukitWebTVBundle;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. class MultimediaObjectVoter extends Voter
  13. {
  14.     public const EDIT 'edit';
  15.     public const PLAY 'play';
  16.     public const VIEW_METADATA 'view_metadata';
  17.     private $mmobjService;
  18.     private $requestStack;
  19.     public function __construct(MultimediaObjectService $mmobjServiceRequestStack $requestStack)
  20.     {
  21.         $this->mmobjService $mmobjService;
  22.         $this->requestStack $requestStack;
  23.     }
  24.     protected function supports($attribute$subject)
  25.     {
  26.         // if the attribute isn't one we support, return false
  27.         if (!in_array($attribute, [self::EDITself::PLAYself::VIEW_METADATA])) {
  28.             return false;
  29.         }
  30.         // only vote on Post objects inside this voter
  31.         if (!$subject instanceof MultimediaObject) {
  32.             return false;
  33.         }
  34.         return true;
  35.     }
  36.     protected function voteOnAttribute($attribute$multimediaObjectTokenInterface $token)
  37.     {
  38.         $user $token->getUser();
  39.         switch ($attribute) {
  40.             case self::EDIT:
  41.                 return $this->canEdit($multimediaObject$user);
  42.             case self::PLAY:
  43.                 return $this->canPlay($multimediaObject$user);
  44.             case self::VIEW_METADATA:
  45.                 return $this->canViewMetadata($multimediaObject$user);
  46.         }
  47.         throw new \LogicException('This code should not be reached!');
  48.     }
  49.     protected function canEdit(MultimediaObject $multimediaObject$user null)
  50.     {
  51.         if ($user instanceof User && ($user->hasRole(PermissionProfile::SCOPE_GLOBAL) || $user->hasRole('ROLE_SUPER_ADMIN'))) {
  52.             return true;
  53.         }
  54.         if ($user instanceof User && $user->hasRole(PermissionProfile::SCOPE_PERSONAL) && $this->mmobjService->isUserOwner($user$multimediaObject)) {
  55.             return true;
  56.         }
  57.         return false;
  58.     }
  59.     protected function canPlay(MultimediaObject $multimediaObject$user null)
  60.     {
  61.         // Private play
  62.         if ($this->canEdit($multimediaObject$user)) {
  63.             return true;
  64.         }
  65.         // Test broadcast
  66.         $embeddedBroadcast $multimediaObject->getEmbeddedBroadcastNotNull();
  67.         if (EmbeddedBroadcast::TYPE_LOGIN === $embeddedBroadcast->getType()) {
  68.             if (!$user instanceof User || !$this->isViewerOrWithScope($user)) {
  69.                 return false;
  70.             }
  71.         }
  72.         if (EmbeddedBroadcast::TYPE_GROUPS === $embeddedBroadcast->getType()) {
  73.             if (!$user instanceof User || !$this->isViewerOrWithScope($user) || !$this->isUserRelatedToBroadcast($multimediaObject->getEmbeddedBroadcastNotNull(), $user)) {
  74.                 return false;
  75.             }
  76.         }
  77.         /*
  78.          * NOTE: (OUT OF SCOPE) Share URL and password is as easy as share only the URL.
  79.          *      if (EmbeddedBroadcast::TYPE_PASSWORD === $embeddedBroadcast->getType()) {
  80.          *          $password = $this->requestStack->getMasterRequest()->get('broadcast_password');
  81.          *          if ($password != $embeddedBroadcast->getPassword()) {
  82.          *              return false;
  83.          *          }
  84.          *      }
  85.         */
  86.         // Public play
  87.         if ($this->mmobjService->isHidden($multimediaObjectPumukitWebTVBundle::WEB_TV_TAG) || $this->mmobjService->isHidden($multimediaObject'PUCHPODCAST')) {
  88.             return true;
  89.         }
  90.         // Legacy code
  91.         if ($this->mmobjService->isHidden($multimediaObject'PUCHOPENEDX')) {
  92.             return true;
  93.         }
  94.         // TTK-24312 WA to show public videos with Poddium channel
  95.         if ($this->mmobjService->isPublished($multimediaObject'PUCHPODDIUM')) {
  96.             return true;
  97.         }
  98.         return false;
  99.     }
  100.     protected function canViewMetadata(MultimediaObject $multimediaObject$user null)
  101.     {
  102.         // Private play
  103.         if ($this->canEdit($multimediaObject$user)) {
  104.             return true;
  105.         }
  106.         // Public play
  107.         if ($this->mmobjService->isHidden($multimediaObjectPumukitWebTVBundle::WEB_TV_TAG) || $this->mmobjService->isHidden($multimediaObject'PUCHPODCAST')) {
  108.             return true;
  109.         }
  110.         // Legacy code
  111.         if ($this->mmobjService->isHidden($multimediaObject'PUCHOPENEDX')) {
  112.             return true;
  113.         }
  114.         return false;
  115.     }
  116.     protected function isViewerOrWithScope(User $user)
  117.     {
  118.         return $user->hasRole(PermissionProfile::SCOPE_GLOBAL) || $user->hasRole(PermissionProfile::SCOPE_PERSONAL)
  119.                 || $user->hasRole(PermissionProfile::SCOPE_NONE) || $user->hasRole('ROLE_SUPER_ADMIN');
  120.     }
  121.     // Related to EmbeddedBroadcastService::isUserRelatedToMultimediaObject
  122.     protected function isUserRelatedToBroadcast(EmbeddedBroadcast $broadcastUser $user)
  123.     {
  124.         $userGroups $user->getGroups()->toArray();
  125.         $playGroups $broadcast->getGroups()->toArray();
  126.         return array_intersect($playGroups$userGroups);
  127.     }
  128. }