Я использую динамическое изменение контекста сериализации в мое приложение для применения группы admin:write
, когда пользователь является администратором. Так что пользователь admin сможет обновить это свойство.
Конструктор контекста имеет такую конфигурацию:
<?php
namespace App\Serializer;
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
final class AdminContextBuilder implements SerializerContextBuilderInterface
{
private $decorated;
private $authorizationChecker;
public function __construct(SerializerContextBuilderInterface $decorated, AuthorizationCheckerInterface $authorizationChecker)
{
$this->decorated = $decorated;
$this->authorizationChecker = $authorizationChecker;
}
public function createFromRequest(Request $request, bool $normalization, ?array $extractedAttributes = null): array
{
$context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
if (isset($context['groups']) && $this->authorizationChecker->isGranted('ROLE_ADMIN') && false === $normalization) {
$context['groups'][] = 'admin:write';
}
if (isset($context['groups']) && $this->authorizationChecker->isGranted('ROLE_ADMIN') && true === $normalization) {
$context['groups'][] = 'admin:read';
}
return $context;
}
}
Я хочу показать это свойство админу:
abstract class User implements UserInterface
{
/**
* @ORM\Column(name="account_status", type="string", length=8)
* @Groups({"read", "admin:write"})
*/
protected $accountStatus;
}
Данные возвращены успешно, и я вижу строку в представлении таблицы или элемента в панели администратора.
Но документация, созданная API-платформой на …/api/docs.jsonld
, не раскрывает это свойство: свойство недоступно для записи:
{
"@type": "hydra:SupportedProperty",
"hydra:property": {
"@id": "#User/accountStatus",
"@type": "rdf:Property",
"rdfs:label": "accountStatus",
"domain": "#User",
"range": "xmls:string"
},
"hydra:title": "accountStatus",
"hydra:required": false,
"hydra:readable": true,
"hydra:writable": false
},
Думаю, это мешает показывать поле в администрации.
Как я могу добавить это свойство в документацию и в конечном итоге использовать response-admin?
Я пробовал любую конфигурацию, которую только мог придумать:
abstract class User implements UserInterface
{
/**
* @ORM\Column(name="account_status", type="string", length=8)
* @Groups({"read", "admin:write"})
* @ApiProperty(writable=true)
*/
protected $accountStatus;
}