最簡單的方法是編輯查詢並檢查編輯/顯示操作中的訪問權限。
事情是這樣的:
管理類
/**
* {@inheritdoc}
*/
public function createQuery($context = 'list')
{
$user = $this->getConfigurationPool()->getContainer()->get('security.context')->getToken()->getUser();
/** @var \Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery @query */
$query = $this->getModelManager()->createQuery($this->getClass(), 'o');
if (!$this->isGranted('MASTER')) {
$query
->where('entity.user = :user')
->setParameter('user', $user)
;
}
return $query;
}
如果用戶沒有掌握,他只能看到他自己的實體。
您還可以實現管理類等hasSubjectAccess
方法:
/**
* Check whether the user has access to the subject
*
* @return bool
*/
protected function hasSubjectAccess()
{
$user = $this->getConfigurationPool()->getContainer()->get('security.context')->getToken()->getUser();
if (!$this->isGranted('MASTER') && $this->getSubject()->getUser() !== $user) {
return false;
}
return true;
}
,並在編輯和顯示形式執行這種檢查:
/**
* {@inheritdoc}
*/
protected function configureFormFields(FormMapper $formMapper)
{
if (!$this->hasSubjectAccess()) {
throw new AccessDeniedException();
}
// ...
}
另一種方式是實現ACL。您可以閱讀official documentation
謝謝,這是我在尋找,但我仍然有一些疑問。我寫了新的答案,因爲它太長了。 – Angel 2013-04-10 08:26:53