我已经在表单中创建和编辑Sonata Admin的这些角色从FOSUserBundle扩展。但我想要的是:
- “ROLE_SONATA_ADMIN_PRODUCT_LIST”
- “ROLE_SONATA_ADMIN_PRODUCT_EDIT”
- “ROLE_ADMIN”
- “ROLE_SUPER_ADMIN”
- “SONATA”
- “角色产品列表”
- “角色产品编辑”
- “角色管理员”
- “角色奏鸣曲”
我一个月前试过但是没用,我没有好的解决方案。我只是在学习Symfony2,所以任何有这方面想法或经验的人都可以帮助我。
答案 0 :(得分:1)
1)我认为这可以在app/config/config.yml
中配置:
# app/config/config.yml sonata_admin: security: handler: sonata.admin.security.handler.acl # acl security information information: GUEST: [VIEW, LIST] STAFF: [EDIT, LIST, CREATE] EDITOR: [OPERATOR, EXPORT] ADMIN: [MASTER] # permissions not related to an object instance and also to be available when objects do not exist # the DELETE admin permission means the user is allowed to batch delete objects admin_permissions: [CREATE, LIST, DELETE, UNDELETE, EXPORT, OPERATOR, MASTER] # permission related to the objects object_permissions: [VIEW, EDIT, DELETE, UNDELETE, OPERATOR, MASTER, OWNER]
有关详情:http://sonata-project.org/bundles/admin/master/doc/reference/security.html#configuration
2)您只需将ROLE_XXX
字符串的相应翻译添加到翻译文件中即可。
Symfony2表单组件支持翻译错误消息,字段标签和其他字符串。
答案 1 :(得分:1)
尝试使用自己的角色。我的意思是你必须在用户实体中创建另一个属性(比如profile)并使用它而不是sonata admin的角色属性:
0-在您的security.yml
中 role_hierarchy:
ROLE_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN]
ROLE_SUPER_ADMIN: [ROLE_SONATA_ADMIN, ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
SONATA:
- ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT
- ROLE_SONATA_ADMIN
ROLE_0: [ROLE_SUPER_ADMIN]
ROLE_1: [ROLE_SUPER_ADMIN]
ROLE_2: [ROLE_SUPER_ADMIN]
ROLE_3: [ROLE_SUPER_ADMIN]
ROLE_4: [ROLE_SUPER_ADMIN]
ROLE_5: [ROLE_SUPER_ADMIN]
ROLE_6: [ROLE_SUPER_ADMIN]
ROLE_7: [ROLE_SUPER_ADMIN]
1-在您的用户实体中:
<field name="profile" type="smallint" column="profile" nullable="true" />
2-在formMapper中添加它并删除sonata角色属性
->add('profile', 'choice', array(
'required' => true,
'label' => 'profile_label',
'translation_domain' => 'SonataUserBundle',
'mapped' => false,
'multiple' => true,
'choices' => array(
'0' => 'Role 0',
'1' => 'Role 1',
'2' => 'Role 2',
'3' => 'Role 3',
'4' => 'Role 4',
'5' => 'Role 5',
'6' => 'Role 6',
'7' => 'Role 7')
))
3-在您的UserAdmin控制器中,定义security.yml中提到的自定义角色数组,并在formMapper中使用相同的角色顺序
protected $roleMapArary = array('ROLE_Role0','ROLE_Role1', 'ROLE_Role2', 'ROLE_Role3', 'ROLE_Role4', 'ROLE_Role5', 'ROLE_Role6', 'ROLE_Role6');
4 - 然后在userAdminController的创建动作中
if ($this->getRestMethod() == 'POST')
{
$roleArray = $this->get('request')->request->get($this->admin->getUniqid())['profile'];
$roles = array();
foreach ($roleArray as $key => $role)
{
$roles[] = $this->roleMapArary[$role];
}
$object->setRealRoles($roles);
$form->submit($this->get('request'));
// ....
}
5-然后在您的编辑操作中:
$requestRoles = array();
$roles = array();
$id = $this->get('request')->get($this->admin->getIdParameter());
$object = $this->admin->getObject($id);
if (!$object)
{
throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
}
if (false === $this->admin->isGranted('EDIT', $object))
{
throw new AccessDeniedException();
}
$this->admin->setSubject($object);
$realRoles = $object->getRealRoles();
if (sizeof($realRoles) > 0)
{
foreach ($realRoles as $key => $value)
{
$trouve = array_search($value, $this->roleMapArary);
if (is_int($trouve))
{
$requestRoles[] = $trouve;
}
}
}
/** @var $form \Symfony\Component\Form\Form */
$form = $this->admin->getForm();
$form->setData($object);
$form->get('profile')->setData($requestRoles);
if ($this->getRestMethod() == 'POST')
{
$roleArray = $this->get('request')->request->get($this->admin->getUniqid())['profile'];
foreach ($roleArray as $key => $role)
{
$roles[] = $this->roleMapArary[$role];
}
$object->setRealRoles($roles);
$form->submit($this->get('request'));
$isFormValid = $form->isValid();
// ...
6-确保在用户实体中添加getProfile和setProfile:
public function setProfile($profile) {
$this->profile = $profile;
return $this;
}
public function getProfile() {
$profile = '';
$roleMapArary = array('ROLE_Role0','ROLE_Role1', 'ROLE_Role2', 'ROLE_Role3', 'ROLE_Role4', 'ROLE_Role5', 'ROLE_Role6', 'ROLE_Role6');
// your array as string (as you want to show them == the same as formMapper)
$profileMapArary = array(
'Role 0 string',
'Role 1 string ',
'Role 2 string ',
'Role 3 string ',
'Role 4 string ',
'Role 5 string ',
'Role 6 string ',
'Role 7 string ',);
$roleArray = $this->getRealRoles();
if (sizeof($roleArray) > 0)
{
foreach ($roleArray as $key => $value)
{
$trouve = array_search($value, $roleMapArary);
if (is_int($trouve))
{
$profile = $profileMapArary[$trouve]. ' - '.$profile;
}
}
$profile = substr($profile, 0, -2);
}
return $profile;
}