<?php
namespace SurveyPlugin\Listener;
use AppBundle\Common\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
class SurveyPluginKernelRequestListener
{
/**
* @var mixed
*/
private $container;
public function __construct($container)
{
$this->container = $container;
}
public function onKernelController(ControllerEvent $event)
{
$request = $event->getRequest();
$pathInfo = $request->getPathInfo();
if (0 === stripos($pathInfo, '/survey_manage') || 0 === stripos($pathInfo, '/questionnaire_manage')
|| 0 === stripos($pathInfo, '/survey_member')) {
$currentUser = $this->getCurrentUser();
if (!$this->hasManageRole($currentUser) && !$currentUser->hasPermission('admin_train_survey_manage') && !$currentUser->hasPermission('admin_train_questionnaire_manage')) {
throw new AccessDeniedException('您无权查看和操作该问卷');
}
}
}
protected function hasManageRole($user)
{
return $this->isSuperAdmin($user['roles']) || $this->isTrainingAdmin($user['roles']);
}
protected function isSuperAdmin($roles)
{
if (count(array_intersect($roles, ['ROLE_SUPER_ADMIN'])) > 0) {
return true;
}
return false;
}
protected function isTrainingAdmin($roles)
{
if (count(array_intersect($roles, ['ROLE_TRAINING_ADMIN'])) > 0) {
return true;
}
return false;
}
protected function getCurrentUser()
{
$biz = $this->getBiz();
return $biz['user'];
}
protected function getBiz()
{
return $this->container->get('biz');
}
}