plugins/SurveyPlugin/Listener/SurveyPluginKernelRequestListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace SurveyPlugin\Listener;
  3. use AppBundle\Common\Exception\AccessDeniedException;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. class SurveyPluginKernelRequestListener
  6. {
  7.     /**
  8.      * @var mixed
  9.      */
  10.     private $container;
  11.     public function __construct($container)
  12.     {
  13.         $this->container $container;
  14.     }
  15.     public function onKernelController(ControllerEvent $event)
  16.     {
  17.         $request $event->getRequest();
  18.         $pathInfo $request->getPathInfo();
  19.         if (=== stripos($pathInfo'/survey_manage') || === stripos($pathInfo'/questionnaire_manage')
  20.             || === stripos($pathInfo'/survey_member')) {
  21.             $currentUser $this->getCurrentUser();
  22.             if (!$this->hasManageRole($currentUser) && !$currentUser->hasPermission('admin_train_survey_manage') && !$currentUser->hasPermission('admin_train_questionnaire_manage')) {
  23.                 throw new AccessDeniedException('您无权查看和操作该问卷');
  24.             }
  25.         }
  26.     }
  27.     protected function hasManageRole($user)
  28.     {
  29.         return $this->isSuperAdmin($user['roles']) || $this->isTrainingAdmin($user['roles']);
  30.     }
  31.     protected function isSuperAdmin($roles)
  32.     {
  33.         if (count(array_intersect($roles, ['ROLE_SUPER_ADMIN'])) > 0) {
  34.             return true;
  35.         }
  36.         return false;
  37.     }
  38.     protected function isTrainingAdmin($roles)
  39.     {
  40.         if (count(array_intersect($roles, ['ROLE_TRAINING_ADMIN'])) > 0) {
  41.             return true;
  42.         }
  43.         return false;
  44.     }
  45.     protected function getCurrentUser()
  46.     {
  47.         $biz $this->getBiz();
  48.         return $biz['user'];
  49.     }
  50.     protected function getBiz()
  51.     {
  52.         return $this->container->get('biz');
  53.     }
  54. }