Symfony 令牌接口$token->;getUser()不会带回对象
我正在用ApiPlatform制作CheeseListing RESTful API 我为我的奶酪列表对象做了一个投票人:Symfony 令牌接口$token->;getUser()不会带回对象,symfony,entity,api-platform.com,symfony4-voter,Symfony,Entity,Api Platform.com,Symfony4 Voter,我正在用ApiPlatform制作CheeseListing RESTful API 我为我的奶酪列表对象做了一个投票人: class CheeseListingVoter extends Voter { ... protected function voteOnAttribute($attribute, $subject, TokenInterface $token) { $user = $token->getUser(); // if the user is ano
class CheeseListingVoter extends Voter
{
...
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
// if the user is anonymous, do not grant access
if (!$user instanceof UserInterface) {
return false;
}
/** $var CheeseListing $subject */
// ... (check conditions and return true to grant permission) ...
switch ($attribute) {
case 'EDIT':
if($subject->getOwner() === $user){
return true;
}
...
当$subject->getOwner()===$user
是一个对象而$subject->getOwner()
是一个Iri/api/users/1“不要使用if()并尝试使用id,因为在您的实体用户中,我认为您没有getOwner()。
试试这个:
开关($attribute){
案例“编辑”:
返回$subject->getId()===$user->getId()
回答:即使ApiResource的/api/CheeseListing/
Get端点返回用户的Iri:
像
所有者字段实际上是一个对象。ApiResource有自己的功能,可以将Iri转换为对象,反之亦然
发布/api/CheeseListing
时也适用同样的情况,您会得到以下信息:
{
"title": "..."
"owner": "/api/users/1"
}
它实际上是从
“/api/users/1”
转换成用户对象我要问一些愚蠢的问题:$subject->getOwner()是一个uri听起来出乎意料,你确定吗?我的意思是,如果它是真的,那么人们会期望两者都是对象,或者两者都是uri,或者bot都是null。那么……你怎么知道呢?
{
"title": "..."
"owner": "/api/users/1"
}