管理者クラス内にカスタマイズされたアクションがいくつかあります。私がしていることは、管理クラス内でこれらを「構成」することです。標準の 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');
それでおしまい。努力とデザインは私にとってかなり公平に思えます。:-)