src/Security/BlogVoter.php line 14

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Security/BlogVoter.php
  4. //----------------------------------------------------------------------
  5. namespace App\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use App\Entity\Access;
  9. use App\Entity\Blog\Article;
  10. class BlogVoter extends Voter
  11. {
  12.     //--------------------------------------------------------------------------------
  13.     const DASHBOARD "blog_dashboard";
  14.     const ADD_ARTICLE "blog_article_add";
  15.     const VIEW_ARTICLE "blog_article_view";
  16.     const EDIT_ARTICLE "blog_article_edit";
  17.     const PUBLISH_ARTICLE "blog_article_publish";
  18.     const DELETE_ARTICLE "blog_article_delete";
  19.     //--------------------------------------------------------------------------------
  20.     const PERMISSIONS = array(
  21.         self::DASHBOARD,
  22.         self::ADD_ARTICLE,
  23.         self::VIEW_ARTICLE,
  24.         self::EDIT_ARTICLE,
  25.         self::PUBLISH_ARTICLE,
  26.         self::DELETE_ARTICLE,
  27.     );
  28.     //--------------------------------------------------------------------------------
  29.     protected function supports(string $attribute$subject): bool
  30.     {
  31.         // if the attribute isn't one we support, return false
  32.         if (!in_array($attributeself::PERMISSIONS))
  33.         {
  34.             return false;
  35.         }
  36.         // Only vote on Article objects (if subject is not null)
  37.         if ($subject !== null && !$subject instanceof Article)
  38.         {
  39.             return false;
  40.         }
  41.         return true;
  42.     }
  43.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  44.     {
  45.         $user $token->getUser();
  46.         if (!$user instanceof Access)
  47.         {
  48.             // the user must be logged in; if not, deny access
  49.             return false;
  50.         }
  51.         // The user must be the owner of the object
  52.         // if ($subject !== null)
  53.         // {
  54.         //     $owner = $subject->getOwner();
  55.         //     if ($owner === null)
  56.         //         return false;
  57.         //     if (!$owner->equals($user))
  58.         //         return false;
  59.         // }
  60.         switch ($attribute)
  61.         {
  62.             case self::DASHBOARD:
  63.                 return $this->canAccessDashboard($user);
  64.             case self::ADD_ARTICLE:
  65.                 return $this->canAddArticle($user);
  66.             case self::VIEW_ARTICLE:
  67.                 return $this->canViewArticle($user$subject);
  68.             case self::EDIT_ARTICLE:
  69.                 return $this->canEditArticle($user$subject);
  70.             case self::PUBLISH_ARTICLE:
  71.                 return $this->canPublishArticle($user$subject);
  72.             case self::DELETE_ARTICLE:
  73.                 return $this->canDeleteArticle($user$subject);
  74.         }
  75.         throw new \LogicException('This code should not be reached!');
  76.     }
  77.     private function canAccessDashboard(Access $user): bool
  78.     {
  79.         return true;
  80.     }
  81.     private function canAddArticle(Access $user): bool
  82.     {
  83.         return true;
  84.     }
  85.     private function canViewArticle(Access $userArticle $subject): bool
  86.     {
  87.         return true;
  88.     }
  89.     private function canEditArticle(Access $userArticle $subject): bool
  90.     {
  91.         return true;
  92.     }
  93.     private function canPublishArticle(Access $userArticle $subject): bool
  94.     {
  95.         return true;
  96.     }
  97.     private function canDeleteArticle(Access $userArticle $subject): bool
  98.     {
  99.         return true;
  100.     }
  101. }