如何比较记录的用户相关关联与上下文构建器中的其他用户
How to compare Logged user related association with other users inside context builder
我会先添加代码然后我会解释我想从代码中得到什么
namespace App\Serializer;
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class UserCollectorContextBuilder implements SerializerContextBuilderInterface
{
private SerializerContextBuilderInterface $decorated;
private AuthorizationCheckerInterface $authorizationChecker;
private TokenStorageInterface $token;
public function __construct(
SerializerContextBuilderInterface $decorated,
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $token
)
{
$this->decorated = $decorated;
$this->authorizationChecker = $authorizationChecker;
$this->token = $token;
}
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
{
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
$resourceClass = $context['resource_class'] ?? null;
if (
$resourceClass === User::class &&
isset($context['groups']) &&
$this->authorizationChecker->isGranted('ROLE_ADMIN') &&
$normalization === true
){
$context['groups'][] = 'admin:read';
}
return $context;
}
}
用户实体属性
/**
* @ORM\Column(type="boolean")
* @Groups("admin:read")
*/
private bool $enabled = false;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Association", inversedBy="users", cascade={"persist", "remove"})
* @Groups({"user:read"})
*/
private ?Association $association = null;
所以上面的代码正在工作它向管理员显示启用值但我想要的是管理员只看到来自同一组(关联)的用户启用值作为管理员而不是来自其他组,因为每个组(关联)是有自己的管理员。
如果有人可以帮助实现这一点,我将很高兴。
谢谢
我用自定义 Normalizer 实现了我想要的,谢谢。
因为我们不能在上下文构建器中有对象
我会先添加代码然后我会解释我想从代码中得到什么
namespace App\Serializer;
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class UserCollectorContextBuilder implements SerializerContextBuilderInterface
{
private SerializerContextBuilderInterface $decorated;
private AuthorizationCheckerInterface $authorizationChecker;
private TokenStorageInterface $token;
public function __construct(
SerializerContextBuilderInterface $decorated,
AuthorizationCheckerInterface $authorizationChecker,
TokenStorageInterface $token
)
{
$this->decorated = $decorated;
$this->authorizationChecker = $authorizationChecker;
$this->token = $token;
}
public function createFromRequest(Request $request, bool $normalization, array $extractedAttributes = null): array
{
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
$resourceClass = $context['resource_class'] ?? null;
if (
$resourceClass === User::class &&
isset($context['groups']) &&
$this->authorizationChecker->isGranted('ROLE_ADMIN') &&
$normalization === true
){
$context['groups'][] = 'admin:read';
}
return $context;
}
}
用户实体属性
/**
* @ORM\Column(type="boolean")
* @Groups("admin:read")
*/
private bool $enabled = false;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Association", inversedBy="users", cascade={"persist", "remove"})
* @Groups({"user:read"})
*/
private ?Association $association = null;
所以上面的代码正在工作它向管理员显示启用值但我想要的是管理员只看到来自同一组(关联)的用户启用值作为管理员而不是来自其他组,因为每个组(关联)是有自己的管理员。
如果有人可以帮助实现这一点,我将很高兴。
谢谢
我用自定义 Normalizer 实现了我想要的,谢谢。 因为我们不能在上下文构建器中有对象