src/Security/RecordVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. class RecordVoter extends Voter
  9. {
  10.     private $container;
  11.    public function __construct(ContainerInterface $container)
  12.    {
  13.        $this->container $container;
  14.    }
  15.     /*
  16.      * Record based should support CREATE, VIEW, EDIT, DELETE
  17.      */
  18.     protected function supports($attribute$subject)
  19.     {
  20.         return in_array($attribute, ['RECORD_EDIT''RECORD_VIEW''RECORD_CREATE']);
  21.     }
  22.     /*
  23.      * Check if we return access for user based on the permission set for user role
  24.      * for VIEW, EDIT, DELETE, we should also check securitygroups_records for permission
  25.      */
  26.     protected function voteOnAttribute($attribute$objectTokenInterface $token)
  27.     {
  28.         $module $object['module'];
  29.         if(isset($object['record_id'])){
  30.             $record_id $object['record_id'];
  31.         }
  32.         $has_access false;
  33.         $suiteUtils $this->container->get('strategic_plan_utils');
  34.         $user $token->getUser();
  35.         $result_access = array();
  36.         //For all actions, we need to check access set for role first
  37.         switch ($attribute) {
  38.             case "RECORD_CREATE":
  39.                 $result_access $suiteUtils->hasAccessToRole($user->getID(), $module'create');
  40.                 foreach ($result_access as $row) {
  41.                     if ($row['access_override'] >= 0) {
  42.                         return true;
  43.                     }
  44.                 }
  45.                 break;
  46.             case "RECORD_VIEW":
  47.                 $result_access $suiteUtils->hasAccessToRole($user->getID(), $module'view');
  48.                 break;
  49.             case "RECORD_EDIT":
  50.                 $result_access $suiteUtils->hasAccessToRole($user->getID(), $module'edit');
  51.                 break;
  52.             case "RECORD_DELETE":
  53.                 $result_access $suiteUtils->hasAccessToRole($user->getID(), $module'delete');
  54.                 break;
  55.             default:
  56.         }
  57.         //For view, edit and delete, we also need to check with security groups
  58.         foreach($result_access as $row) {
  59.             switch ($row['access_override']) {
  60.                 case -99//if "none" then deny access
  61.                     $has_access false;
  62.                     break;
  63.                 case 0//if "not set" then grant access
  64.                     $has_access true;
  65.                     break;
  66.                 case 75:  //if "user" then deny access
  67.                     $has_access false;
  68.                     break;
  69.                 case 80:
  70.                     $has_access=$suiteUtils->hasAccessToRecord($user->getID(), $record_id);
  71.                     break;
  72.                 case 90// if "all" then grant access
  73.                     $has_access true;
  74.                     break;
  75.             }
  76.         }
  77.         return $has_access;
  78.     }
  79. }