vendor/simpledot/cms-bundle/AWCmsBundle/Voter/UserVoter.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: albert
  5.  * Date: 1/12/17
  6.  * Time: 17:52
  7.  */
  8. namespace AWCmsBundle\Voter;
  9. use AWEcommerceBundle\Entity\Order;
  10. use AWCmsBundle\Entity\User;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. class UserVoter extends Voter
  15. {
  16.     const EDIT 'edit';
  17.     /**
  18.      * @var AccessDecisionManagerInterface
  19.      */
  20.     protected $decisionManager;
  21.     /**
  22.      * AlbumVoter constructor.
  23.      * @param AccessDecisionManagerInterface $decisionManager
  24.      */
  25.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  26.     {
  27.         $this->decisionManager $decisionManager;
  28.     }
  29.     /**
  30.      * Determines if the attribute and subject are supported by this voter.
  31.      *
  32.      * @param string $attribute An attribute
  33.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  34.      *
  35.      * @return bool True if the attribute and subject are supported, false otherwise
  36.      */
  37.     protected function supports($attribute$subject)
  38.     {
  39.         
  40.         if (!$subject instanceof User or !in_array($attribute, array(self::EDIT))) {
  41.             return false;
  42.         }
  43.         
  44.         return true;
  45.     }
  46.     /**
  47.      * Perform a single access check operation on a given attribute, subject and token.
  48.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  49.      *
  50.      * @param string $attribute
  51.      * @param User $userSubject
  52.      * @param TokenInterface $token
  53.      *
  54.      * @return bool
  55.      */
  56.     protected function voteOnAttribute($attribute$userSubjectTokenInterface $token)
  57.     {
  58.         // Current User is super admin, can do every thing
  59.         if ($this->decisionManager->decide($token, [User::ROLE_SUPER_ADMIN])) {
  60.             return true;
  61.         }
  62.         
  63.         // Only super admin can edit super admin
  64.         if ($userSubject->isSuperAdmin()) {
  65.             return false;
  66.         }
  67.         
  68.         // Admin can edit other users
  69.         if ($this->decisionManager->decide($token, [User::ROLE_ADMIN])) {
  70.             return true;
  71.         }
  72.         
  73.         return false;
  74.     }
  75. }