Problems with building automated permission control + menu + breadcrumbs in symfony2
PHPz
PHPz 2017-05-16 16:44:27
0
1
629

I have always used other frameworks such as TP and CI to meet the above requirements, and it can be easily realized. Recently, I also want to implement it on SF2 and write an adminBundle to automatically create a backend to provide common functions. However, I encountered the following problem:
Since I want to detect user access permissions globally in the background, I listened to the controller. Obtain the request information before each controller is triggered and check whether the user has given the corresponding permissions. code show as below:

class RunActListener {
protected $securityContext;
   public function __construct(SecurityContextInterface $securityContext)
       $this->securityContext = $securityContext;
   }
   public function onRunAct(FilterControllerEvent $event) {
      $request = $event->getRequest();
      $ajax = $request->isXmlHttpRequest();
      if (preg_match('/^\/admin/', $request->getRequestUri())) {
      $routeName = $request->get('_route');
      if ($this->securityContext->isGranted(array($routeName))) {
      } else {
     $reffeer = $request->server->get('HTTP_REFERER');
           $data['info'] = '您没有权限操作!';
           $data['status'] = false;
           if ($ajax) {
           $event->setController(
              function() use ($data) {
                return new JsonResponse($data);
                });
                } else {
                $event->setController(
                function() use ($data) {
                return new Response('<b>您没有足够的访问权限!</b><script>setTimeout("window.history.back(-1)",2000)</script>');
                });
                //。。。。。

Then I introduced 2 renders in the background layout. The menu and breadcrumb codes are as follows:

 <nav id="navigation" class="collapse">
    <ul>
     {{ render(controller( 'CwpUtilBundle:Menu:showMenu')) }}
    </ul>
 </nav>
 </aside>
 <p id="sidebar-separator"></p>
   <section id="main" class="clearfix">
   <p id="main-header" class="page-header">
    {{ render(controller( 'CwpUtilBundle:Menu:showCrumb')) }}
  </p>
  <p id="main-content">
   {% block main %}
   {% endblock %}
   </p>
  </section>

In this way, two sub_requests were initiated, but my requirement is to obtain the url of the parent request in the sub_request to implement my breadcrumbs and menu. There are some troubles in SF. Some experts suggest using get request stack in sub action

/**
* 显示面包屑
* @return type
*/
public function showCrumbAction() {
$request = $this->container->get('request');
$path = $request->server->get('REDIRECT_URL');
$path_arr = explode('/', $path);
$path_len = count($path_arr);
$top_index = (int) ($path_len - 2);
$path_top = $path_arr[$top_index];
$route_arr = $this->get('router')->match($path);
$route = $route_arr['_route'];
$em = $this->getDoctrine()->getManager();
//获取菜单结果集
$top_menu = $em->getRepository('CwpUtilBundle:Menu')->findOneByNode($path_top);
$sec_menu = $em->getRepository('CwpUtilBundle:Menu')->findOneByNode($route);
return $this->render('CwpUtilBundle:Layout:crumb.html.twig', array(
'top_menu' => $top_menu,
'sec_menu' => $sec_menu,
));
}

I tried to get request_stack in the above action, but once I get, it seems that a request was initiated in this action, my listener was triggered, and then executed else, jumping to the prompt that you have insufficient permissions. I was confused and didn't know why.

PHPz
PHPz

学习是最好的投资!

reply all(1)
伊谢尔伦

The listener that monitors kernel.controller, add a judgment, and only check the permissions when the main request is made:

if ($event->isMasterRequest()) {
    // 才做检查
}

FilterControllerEvent in 2.3 does not have this method, just refer to this logic and write it yourself:
https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/HttpKernel/Event/KernelEvent.php#L100

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!