如何禁用参数转换器中的原则过滤器
我在项目中使用了doctrine softdeleteable扩展,并设置了我的控制器操作。
/**
* @Route("address/{id}/")
* @Method("GET")
* @ParamConverter("address", class="MyBundle:Address")
* @Security("is_granted('view', address)")
*/
public function getAddressAction(Address $address)
{
这很有效,因为如果对象被删除,它会返回NotFound,但是我想授予用户ROLE_ADMIN的访问权限,以便能够看到软删除的内容。
有没有办法让param转换器禁用过滤器,或者我将不得不创建我自己的自定义参数转换器?
目前没有办法做到这一点,但我已经通过创建自己的注释解决了这个问题,它在ParamConverter
完成其工作之前禁用了softdeleteable
过滤器。
AcmeBundle /注释/ IgnoreSoftDelete.php:
namespace AcmeBundleAnnotation;
use DoctrineCommonAnnotationsAnnotation;
/**
* @Annotation
* @Target({"CLASS", "METHOD"})
*/
class IgnoreSoftDelete extends Annotation { }
AcmeBundle /事件监听/ AnnotationListener.php:
namespace AcmeBundleEventListener;
use DoctrineCommonUtilClassUtils;
use DoctrineCommonAnnotationsReader;
use SymfonyComponentHttpKernelEventFilterControllerEvent;
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 = 'AcmeBundleAnnotationIgnoreSoftDelete';
if ($this->readAnnotation($controller, $method, $class)) {
$em = $controller->get('doctrine.orm.entity_manager');
$em->getFilters()->disable('softdeleteable');
}
}
}
AcmeBundle /资源/配置/ services.yml:
services:
acme.annotation_listener:
class: AcmeBundleEventListenerAnnotationListener
arguments: [@annotation_reader]
tags:
- { name: kernel.event_listener, event: kernel.controller }
AcmeBundle /控制器/ DefaultController.php:
namespace AcmeBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationTemplate;
use AcmeBundleAnnotationIgnoreSoftDelete;
use AcmeBundleEntityUser;
class DefaultController extends Controller {
/**
* @Route("/{id}")
* @IgnoreSoftDelete
* @Template
*/
public function indexAction(User $user) {
return ['user' => $user];
}
}
注释可以应用于单个操作方法和整个控制器类。
链接地址: http://www.djcxy.com/p/65033.html上一篇: How to disable a doctrine filter in a param converter
下一篇: Config entries for DoctrineExtensions SoftDeleteable: gedmo/doctrine