У меня есть простой ACL, конфигурирует в acl.global.php
как это:Как построить ACL Assertion для значения переменной в Zend Framework 2?
return [
'acl' => [
'roles' => [
'guest' => null,
'member' => 'guest',
'admin' => 'member'
],
'resources' => [
'allow' => [
'Application\Controller\Index' => ['all' => 'member'],
'Application\Controller\Error' => ['all' => 'member'],
'Item\Controller\Process' => [
'index' => 'member',
'create' => 'member',
'showItem' => 'member', // website.tld/item/:id
'showList' => 'member' // website.tld/list-items
]
]
],
]
];
синтаксический анализатор итерацию по конфигурации и генерирует из элементов массива вызовы Zend\Permissions\Acl#allow(...)
как $this->allow($role, $controller, $action);
.
Теперь мне нужно дополнительно ограничить доступ пользователей к одному виду объекта (mydomain.tld/item/:id
). Пользователь должен получить доступ только в том случае, если его id
равен item.user_id
(означает: пользователь является автором/владельцем).
Как мне реализовать это требование расширить конфиге
'Item\Controller\Process' => [
'index' => 'member',
'create' => 'member',
'showItem' => [
'role' => 'member',
'assertion' => 'UserIsOwner'
]
'showList' => 'member'
]
и впрыснуть Assertion
к Zend\Permissions\Acl#allow(...)
: $this->allow($role, $controller, $action, $assertion);
.
namespace Authorization\Acl\Assertion;
use ...
class UserIsOwner implements AssertionInterface
{
protected $userId;
// To inject the $userId can be the job of the factory.
public function __construct(int $userId)
{
$this->userId = $userId;
}
public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
{
return return $this->userId === ???;
}
}
Но теперь я понятия не имею, как утверждение должно получить item.user_id
впрыскивается. Пример в docu не имеет этой проблемы, поскольку он имеет активы против $_SERVER['REMOTE_ADDR']
.
можно впрыскивать ItemService
выяснить item.user_id
:
public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
{
return $this->isUserOwner();
}
protected function isUserOwner()
{
$itemId = ???;
$item = $this->itemService->findOne($itemId);
$itemOwnerId = $item->getUser()->getId();
return $this->userId == $itemOwnerId;
}
Хотя тогда мне еще нужно внешние данные - текущий item.id
.
В каком месте могут/должны содержаться данные переменной элемента (в данном случае item.user_id
или item.id
), к утверждению?