我正在使用 ApiPlatform 制作 CheeseListing RESTful API。
我为我的 CheeseListing 对象做了一个投票者:
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;
}
...
为什么当是一个对象并且是一个Iri“/api/users/1”$subject->getOwner() === $user
时会返回true$token->getUser()
$subject->getOwner()