简体   繁体   English

动态查看路线Symfony2

[英]Review the route dynamically Symfony2

Hello I need get the route and review if the have access to this, reviewing on the database but a level really abstract and automatic. 您好,我需要得到路线,并检查是否有权访问它,在数据库上进行检查,但实际上是一个抽象和自动的级别。 By now i am making that on this form: 现在,我在此表格上进行此操作:

$accesos = MenuQuery::create()
            ->useAccesoMenuQuery()
            ->usePerfilQuery()
            ->usePerfilUsuarioQuery()
            ->filterByUsuarioId($this->getUser()->getId())
            ->endUse()
            ->endUse()
            ->endUse()
            ->orderBy('menu.orden')
            ->groupBy('menu.id')
            ->find();
    $permiso = false;
    foreach ($accesos as $acceso) {
        if (($acceso->getDireccion() == $ruta) || ($permiso)) {
            $permiso = true;
            break;
        }
    }
    return $permiso;
}

I am using this php function for make that. 我正在使用此php函数。 But i need to make that on the firewalls of symfony2 or another form but abstract. 但是我需要在symfony2或其他抽象形式的防火墙上进行加密。

One way, and if the routes are generic, set up ACL in the security.yml where you can specify that for example ^/admin/.* require ROLE_ADMIN. 一种方法,如果路由是通用的,请在security.yml中设置ACL,您可以在其中指定^ / admin /.*需要ROLE_ADMIN。 If you have special roles, for example, ACCOUNT_ADMIN where it's depending on what is the selected account, then you need to write your own VOTER, where you can decide that at a specific point, is the rights are enough or not. 如果您具有特殊角色,例如ACCOUNT_ADMIN(取决于所选择的帐户),那么您需要编写自己的VOTER,您可以在其中确定在特定时间权限是否足够。

The form that I did it was using a Event Listener of Symfon2 Here I leave to yours my own code. 我做的表单使用的是Symfon2的事件监听器。在这里,我留给您自己的代码。

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class SoporteListener {

private $container;
private $acceso = 0;

public function __construct(ContainerInterface $container) {
    $this->container = $container;
}

public function onKernelRequest(GetResponseEvent $event) {
    $request = $this->container->get('request');
    $routeName = $request->get('_route');
    $securityContext = $this->container->get('security.context');
    if (($securityContext->isGranted('ROLE_USUARIO')) && 
            ($routeName != 'done_login') &&
            ($routeName != 'done_logout')) {
        $usuario = $this->container->get('security.context')->getToken()->getUser();
        $permisos = MenuQuery::create()
                ->useAccesoMenuQuery()
                ->usePerfilQuery()
                ->usePerfilUsuarioQuery()
                ->filterByUsuarioId($usuario->getId())
                ->endUse()
                ->endUse()
                ->endUse()
                ->groupBy('menu.id')
                ->find();
        foreach ($permisos as $permiso) {
            if (($permiso->getDireccion() == $routeName)) {
                $this->acceso = 1;
                break;
            }
        }
        if ($this->acceso == 0) {
            $event->setResponse($this->container->get('templating')->renderResponse('::error.html.twig', array('error' => 'Permiso denegado')));
        } else {
            return;    
        }
    } else {
        return;
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM