如何将已记录的用户相关的关联与上下文构建器中的其他用户进行比较

问题描述 投票:0回答:1

我将首先添加代码,然后我将解释我想从代码中得到什么

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;

所以上面的代码正在工作,它向Admin显示了启用的值,但是我想要的是admin只看到与同一个管理员(同一个组)的用户启用了值,而不是其他组的管理员,因为每个组(关联)都有自己的值管理员

[如果有人可以帮助实现这一目标,我会很高兴。

谢谢

php symfony symfony4 api-platform.com
1个回答
0
投票

我通过自定义Normalizer实现了我想要的功能,谢谢。因为我们无法在上下文生成器中包含对象]

enter link description here

© www.soinside.com 2019 - 2024. All rights reserved.