src/Platform/SecurityBundle/Security/Voter/CmsVoter.php line 21

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Security\Voter;
  3. use App\Entity\Content\AbstractObject;
  4. use App\Entity\Feed\AbstractEntry;
  5. use App\Entity\System\School;
  6. use Cms\ContainerBundle\Entity\Container;
  7. use Cms\ContainerBundle\Entity\Containers\PersonalContainer;
  8. use Cms\ContainerBundle\Entity\Containers\StorageContainer;
  9. use Cms\ModuleBundle\Entity\ModuleEntity;
  10. use Cms\ModuleBundle\Model\ModuleConfig;
  11. use Platform\SecurityBundle\Entity\Identity\Account;
  12. use Platform\SecurityBundle\Model\PlatformSubject;
  13. use Platform\SecurityBundle\Security\PlatformVoter;
  14. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  15. /**
  16.  *
  17.  */
  18. final class CmsVoter extends PlatformVoter
  19. {
  20.     /**
  21.      * {@inheritDoc}
  22.      */
  23.     protected function supports(
  24.         Account $account,
  25.         string $attribute,
  26.         ?PlatformSubject $subject null
  27.     ): bool
  28.     {
  29.         // for this to be a cms vote, the permission should be prefixed a certain way
  30.         if ( ! $this->sentry->isCmsPermission($attribute)) {
  31.             return false;
  32.         }
  33.         // allow passing of a school
  34.         // context for cms should always be a department if any context is given
  35.         // only check this if we are performing a real check
  36.         if ($subject) {
  37.             $context = ($subject->getContext() instanceof School) ? $subject->getContext()->getDepartment() : $subject->getContext();
  38.             if ($context && ! $context instanceof Container) {
  39.                 return false;
  40.             }
  41.         }
  42.         return true;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     protected function poll(
  48.         Account $account,
  49.         string $permission,
  50.         ?PlatformSubject $subject null
  51.     ): int
  52.     {
  53.         // this should have already been checked by the support method...
  54.         $context null;
  55.         if ($subject) {
  56.             $context = ($subject->getContext() instanceof School) ? $subject->getContext()->getDepartment() : $subject->getContext();
  57.             if ($context && ! $context instanceof Container) {
  58.                 throw new \LogicException();
  59.             }
  60.         }
  61.         return $this->sentry->check($account$permission$context)
  62.             ? VoterInterface::ACCESS_GRANTED
  63.             VoterInterface::ACCESS_ABSTAIN;
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     protected function try(
  69.         Account $account,
  70.         string $permission
  71.     ): int
  72.     {
  73.         return $this->sentry->try($account$permission)
  74.             ? VoterInterface::ACCESS_GRANTED
  75.             VoterInterface::ACCESS_ABSTAIN;
  76.     }
  77.     /**
  78.      * {@inheritDoc}
  79.      */
  80.     protected function attributes(
  81.         array $attributes,
  82.         Account $account,
  83.         ?PlatformSubject $subject null
  84.     ): array
  85.     {
  86.         // if we have a personal site, we want to be able to do anything in it
  87.         // therefore, we should also be checking the personal sites permission
  88.         // prepend to the array as in this case, this check is likely to succeed first
  89.         if ($subject && $subject->getContext() instanceof PersonalContainer && $subject->getContext()->getAccount() === $account) {
  90.             $attributes = [
  91.                 'campussuite.cms.me.sites',
  92.                 ...$attributes,
  93.             ];
  94.         }
  95.         // hack for smm stuff
  96.         if ($subject && $subject->getContext() instanceof StorageContainer && $subject->getContext()->getSlug() === 'smm') {
  97.             $attributes = [
  98.                 'campussuite.smm.manage',
  99.                 ...$attributes,
  100.             ];
  101.         }
  102.         // do some extra checks based on the thing we are enforcing permissions on
  103.         $thing $subject $subject->getThing() : null;
  104.         // if we are checking for the module management permission, and have a specific subject, we can also tack on the specific content-type permission
  105.         if ($thing instanceof ModuleEntity && in_array('campussuite.cms.module.manage'$attributestrue)) {
  106.             $attributes[] = sprintf(
  107.                 'campussuite.cms.modules.%s.manage',
  108.                 $this->moduleManager->getModuleConfigurationForEntity($thing)->key(),
  109.             );
  110.         }
  111.         if ($thing instanceof ModuleConfig && in_array('campussuite.cms.module.manage'$attributestrue)) {
  112.             $attributes[] = sprintf(
  113.                 'campussuite.cms.modules.%s.manage',
  114.                 $thing->key(),
  115.             );
  116.         }
  117.         // new object tables
  118.         if ($thing instanceof AbstractObject && in_array('campussuite.cms.module.manage'$attributestrue)) {
  119.             $attributes[] = sprintf(
  120.                 'campussuite.cms.modules.%s.manage',
  121.                 $thing::ROUTING_SLUG,
  122.             );
  123.         }
  124.         // handle schoolnow feed entries
  125.         if ($thing instanceof AbstractEntry && in_array('campussuite.cms.module.manage'$attributestrue)) {
  126.             $attributes = [
  127.                 ...$attributes,
  128.                 AbstractEntry::PERMISSION_MAP[$thing->getType()],
  129.             ];
  130.         }
  131.         return parent::attributes($attributes$account$subject);
  132.     }
  133. }