src/Security/ProjectVoter.php line 16

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\Security;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use App\Entity\Project;
  8. use App\Entity\User;
  9. use App\Util\SecurityUtil;
  10. /**
  11.  *
  12.  * @author wendell.zheng <wxzheng@ustc.edu.cn>
  13.  */
  14. class ProjectVoter extends Voter
  15. {
  16.     const APPLICANT_VIEW 'applicant_view';
  17.     const APPLICANT_EDIT 'applicant_edit';
  18.     const APPLICANT_DELETE 'applicant_delete';
  19.     const COLLEGE_VIEW 'college_view';
  20.     const COLLEGE_ACTION 'college_action';
  21.     const COLLEGE_SORT 'college_sort';
  22.     protected $security;
  23.     protected $em;
  24.     public function __construct(Security $securityEntityManagerInterface $em)
  25.     {
  26.         $this->security $security;
  27.         $this->em $em;
  28.     }
  29.     protected function supports($attribute$subject): bool
  30.     {
  31.         if (! in_array($attribute, [
  32.             self::APPLICANT_VIEW,
  33.             self::APPLICANT_EDIT,
  34.             self::APPLICANT_DELETE,
  35.             self::COLLEGE_VIEW,
  36.             self::COLLEGE_ACTION,
  37.             self::COLLEGE_SORT
  38.         ])) {
  39.             return false;
  40.         }
  41.         if (! $subject instanceof Project) {
  42.             return false;
  43.         }
  44.         return true;
  45.     }
  46.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  47.     {
  48.         $user $token->getUser();
  49.         if (! $user instanceof User) {
  50.             return false;
  51.         }
  52.         if ($user->getUserIdentifier() == 'P0336') {
  53.             return true;
  54.         }
  55.         /** @var Project $project */
  56.         $project $subject;
  57.         switch ($attribute) {
  58.             case self::APPLICANT_VIEW:
  59.                 return $this->canApplicantView($project$user);
  60.             case self::APPLICANT_EDIT:
  61.                 return $this->canApplicantEdit($project$user);
  62.             case self::APPLICANT_DELETE:
  63.                 return $this->canApplicantDelete($project$user);
  64.             case self::COLLEGE_VIEW:
  65.                 return $this->canCollegeView($project$user);
  66.             case self::COLLEGE_ACTION:
  67.                 return $this->canCollegeAction($project$user);
  68.             case self::COLLEGE_SORT:
  69.                 return $this->canCollegeSort($project$user);
  70.         }
  71.         throw new \LogicException('This code should not be reached!');
  72.     }
  73.     protected function canApplicantView(Project $projectUser $user): bool
  74.     {
  75.         return $user == $project->getUser();
  76.     }
  77.     protected function canApplicantEdit(Project $projectUser $user): bool
  78.     {
  79.         if (! $this->canApplicantView($project$user)) {
  80.             return false;
  81.         }
  82.         if (! $this->security->isGranted(BatchVoter::APPLICANT_ACTION$project->getBatch())) {
  83.             return false;
  84.         }
  85.         return in_array($project->getStatus(), [
  86.             Project::STATUS_NEW,
  87.             Project::STATUS_SCHOOL_RECOMMEND
  88.         ]);
  89.     }
  90.     protected function canApplicantDelete(Project $projectUser $user): bool
  91.     {
  92.         if (! $this->canApplicantView($project$user)) {
  93.             return false;
  94.         }
  95.         if (! $this->security->isGranted(BatchVoter::APPLICANT_ACTION$project->getBatch())) {
  96.             return false;
  97.         }
  98.         return $project->isNew();
  99.     }
  100.     protected function canCollegeView(Project $projectUser $user): bool
  101.     {
  102.         return $this->security->isGranted('ROLE_ADMIN') && $user->getAdminCollege() == $project->getCollege();
  103.     }
  104.     protected function canCollegeAction(Project $projectUser $user): bool
  105.     {
  106.         $batch $project->getBatch();
  107.         if (! $this->security->isGranted(BatchVoter::COLLEGE_ACTION$batch)) {
  108.             return false;
  109.         }
  110.         if (! $this->canCollegeView($project$user)) {
  111.             return false;
  112.         }
  113.         return in_array($project->getStatus(), [
  114.             Project::STATUS_NEW,
  115.             Project::STATUS_COLLEGE_RECOMMEND
  116.         ]);
  117.     }
  118.     protected function canCollegeSort(Project $projectUser $user): bool
  119.     {
  120.         if (! $this->canCollegeAction($project$user)) {
  121.             return false;
  122.         }
  123.         return $project->getStatus() == Project::STATUS_COLLEGE_RECOMMEND;
  124.     }
  125. }