我的管理课程中确实有一些自定义操作。我所做的是,只是在管理类中“配置”这些。标准 Sonata\UserBundle\Security\EditableRolesBuilder 调用 Sonata BaseAdmin 类“getSecurityInformation”的公共函数:
foreach ($admin->getSecurityInformation() as $role => $permissions) {
$role = sprintf($baseRole, $role);
if ($isMaster) {
// if the user has the MASTER permission, allow to grant access the admin roles to other users
$roles[$role] = $role;
} elseif ($this->securityContext->isGranted($role)) {
// although the user has no MASTER permission, allow the currently logged in user to view the role
$rolesReadOnly[$role] = $role;
}
}
这就是我要加入的地方。只需覆盖这个函数自己的 Admin 类(我在从 Sonata\AdminBundle\Admin\Admin 扩展的 BaseAdmin 类中完成了这个)
/**
* List here the customized roles actions which are used within the Admin class you have extended. (e.g. the
* CustomerAdmin uses a special function to login as the customer. In this case set the array to array('LOGIN') and
* use at certain points like ->isGranted('LOGIN'). This is also available in templates like
* admin.isGranted('LOGIN', object)).
* The actions you are listing here, will be appended to the standard actions: EDIT, LIST, CREATE, VIEW, DELETE,
* EXPORT, OPERATOR, MASTER.
*
* @see http://sonata-project.org/bundles/admin/master/doc/index.html
*
* @var array
*/
protected $customizedRoles = array();
/**
* {@inheritdoc}
*/
public function getSecurityInformation()
{
$standardAdminRoles = parent::getSecurityInformation();
$customizedAdminRoles = $this->getCustomizedAdminRoles();
$allAdminRoles = array_merge($standardAdminRoles, $customizedAdminRoles);
ksort($allAdminRoles);
return $allAdminRoles;
}
/**
* Get the customized roles set at property of the Admin class 'customizedRoles' prepared to append to the standard
* roles.
*
* @return array
*/
private function getCustomizedAdminRoles()
{
$customizedRoles = array();
if (is_array($this->customizedRoles) && !empty($this->customizedRoles)) {
foreach ($this->customizedRoles as $customizedRole) {
$customizedRole = strtoupper($customizedRole);
$customizedRoles[$customizedRole] = $customizedRole;
}
}
return $customizedRoles;
}
只需通过覆盖将这个数组填充到您的 Admin 类中:
/** @{inheritdoc} */
protected $customizedRoles = array('LOGIN');
而已。努力和设计对我来说似乎很公平。:-)