[英]Symfony2: How to validate an entity by taking into account the currently logged in user?
我正在使用User实体内部的回调进行自定义验证。
因此,我需要获取当前登录用户的User对象。
在控制器中,我可以这样做:
$user= $this->get('security.context')->getToken()->getUser();
但是我应该如何在User实体中执行此操作?
如果要使用与外部依赖项相关的任何逻辑验证对象(在您的情况下使用@security.context
服务来获取当前用户)...您应该:
@security.context
服务注入验证器 该解决方案在文档章节Validators with Dependencies中进行了描述
此视频显示了为什么拥有以下RedBull验证器是绝对必要的。
型号/实体
use FamilyGuy\Validator\Constraints\Peter as PeterIsNotAllowedToOrder;
class Order
{
/** @PeterIsNotAllowedToOrder/RedBull */
public $drink;
配置
# app/config/services.yml
services:
validator.red_bull:
class: FamilyGuy\Validator\Constraints\Peter\RedBullValidator
# for symfony < 2.6 use @security.context
arguments: ["@security.token_storage"]
tags:
- name: "validator.constraint_validator"
alias: "peter_red_bull_constraint_validator"
约束
use Symfony\Component\Validator\Constraint;
namespace FamilyGuy\Validator\Constraints\Peter;
/**
* @Annotation
*/
class RedBull extends Constraint
{
/** @var string */
public $message = 'Peter... You are not allowed to order %drink%.';
/** @return string */
public function validatedBy()
{
// has to match the validator service's alias !
return 'peter_red_bull_constraint_validator';
}
}
验证器:
// For symfony < 2.6 use SecurityContextInterface
// use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
namespace FamilyGuy\Validator\Constraints\Peter;
class RedBullValidator extends ConstraintValidator
{
/** @var TokenStorageInterface|SecurityContextInterface */
protected $tokenStorage;
/** @param TokenStorageInterface|SecurityContextInterface $token_storage */
public function __construct(TokenStorageInterface $token_storage)
{
$this->tokenStorage = $token_storage;
}
public function validate($drink, Constraint $constraint)
{
$currentUser = $this->tokenStorage->getToken()->getUser()->getName();
if ($currentUser !== "Peter") {
return;
}
if ( $drink !== "RedBull" ) {
return
}
$this->context->buildViolation($constraint->message)
->setParameter('%drink%', $drink)
->addViolation()
;
}
您不能并且不应该使用Callback
约束来验证任何外部依赖项。
不要尝试直接将任何验证依赖项注入您的域模型或实体。
一般来说, 验证逻辑应该不在实体之外 。
注释已经是实体和symfony验证器之间的某种软耦合。 这就是为什么建议通常使用xml配置进行学说和验证映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.