gpt4 book ai didi

symfony - 如何在参数转换器中禁用原则过滤器

转载 作者:行者123 更新时间:2023-12-05 03:12:59 24 4
gpt4 key购买 nike

我在一个项目上使用 doctrine softdeleteable 扩展,并设置了我的 Controller 操作。

/**
* @Route("address/{id}/")
* @Method("GET")
* @ParamConverter("address", class="MyBundle:Address")
* @Security("is_granted('view', address)")
*/
public function getAddressAction(Address $address)
{

这非常有效,因为如果对象被删除,它会返回 NotFound,但是我想授予具有 ROLE_ADMIN 的用户访问权限,以便能够查看软删除的内容。

是否已经存在让参数转换器禁用过滤器的方法,或者我是否必须创建自己的自定义参数转换器?

最佳答案

没有现成的方法可以做到这一点,但我已经通过创建自己的注释解决了这个问题,该注释在 ParamConverter 完成其工作之前禁用了 softdeleteable 过滤器。

AcmeBundle/Annotation/IgnoreSoftDelete.php:

namespace AcmeBundle\Annotation;

use Doctrine\Common\Annotations\Annotation;

/**
* @Annotation
* @Target({"CLASS", "METHOD"})
*/
class IgnoreSoftDelete extends Annotation { }

AcmeBundle/EventListener/AnnotationListener.php:

namespace AcmeBundle\EventListener;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\Common\Annotations\Reader;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;

class AnnotationListener {

protected $reader;

public function __construct(Reader $reader) {
$this->reader = $reader;
}

public function onKernelController(FilterControllerEvent $event) {
if (!is_array($controller = $event->getController())) {
return;
}

list($controller, $method, ) = $controller;

$this->ignoreSoftDeleteAnnotation($controller, $method);
}

private function readAnnotation($controller, $method, $annotation) {
$classReflection = new \ReflectionClass(ClassUtils::getClass($controller));
$classAnnotation = $this->reader->getClassAnnotation($classReflection, $annotation);

$objectReflection = new \ReflectionObject($controller);
$methodReflection = $objectReflection->getMethod($method);
$methodAnnotation = $this->reader->getMethodAnnotation($methodReflection, $annotation);

if (!$classAnnotation && !$methodAnnotation) {
return false;
}

return [$classAnnotation, $classReflection, $methodAnnotation, $methodReflection];
}

private function ignoreSoftDeleteAnnotation($controller, $method) {
static $class = 'AcmeBundle\Annotation\IgnoreSoftDelete';

if ($this->readAnnotation($controller, $method, $class)) {
$em = $controller->get('doctrine.orm.entity_manager');
$em->getFilters()->disable('softdeleteable');
}
}

}

AcmeBundle/Resources/config/services.yml:

services:
acme.annotation_listener:
class: AcmeBundle\EventListener\AnnotationListener
arguments: [@annotation_reader]
tags:
- { name: kernel.event_listener, event: kernel.controller }

AcmeBundle/Controller/DefaultController.php:

namespace AcmeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use AcmeBundle\Annotation\IgnoreSoftDelete;
use AcmeBundle\Entity\User;

class DefaultController extends Controller {

/**
* @Route("/{id}")
* @IgnoreSoftDelete
* @Template
*/
public function indexAction(User $user) {
return ['user' => $user];
}

}

注释可以应用于单个操作方法和整个 Controller 类。

关于symfony - 如何在参数转换器中禁用原则过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31752891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com