如何根据用户角色隐藏后台的操作按钮?

How to hide the action button in backoffice based on user roles?

如何根据用户删除后台中的按钮操作?我可以通过在 canPerform 方法中添加一个条件来禁用按钮,就像这样

public boolean canPerform(final ActionContext<String> ctx)
    {
      final UserModel currentUser = userService.getCurrentUser();
      final boolean isUserMemberOfGroup = this.userService.isMemberOfGroup(currentUser,"group_name");
      return isUserMemberOfGroup;
    }

但我想隐藏按钮而不是禁用它。

我希望您已经有一个自定义后台扩展,如果没有,请按照 this tutorial 创建一个。

现在,在 yourcustombackoffice-backoffice-config.xml 中,您可以为您的项目类型声明 listviewactions 组件,其中包含您想要允许 user/group。然后您需要将该用户的 role/group 分配给主体属性。

例如,有两个后台角色 "mainRole" 和 "otherRole"。 "mainRole" 角色已分配给 X 用户,"otherRole" 角色已分配给 Y 用户。现在使用下面的后台配置,X 用户只能看到创建按钮,Y 用户只能看到删除按钮。

<contexttype="Media" component="listviewactions" principal="mainRole" module="hideActionB">

    <y:actionsxmlns:y="http://www.hybris.com/cockpit/config/hybris"xmlns:advanced-search="http://www.hybris.com/cockpitng/config/advancedsearch"xmlns:df="http://www.hybris.com/cockpitng/component/dynamicForms"xmlns:editorArea="http://www.hybris.com/cockpitng/component/editorArea"xmlns:explorer-tree="http://www.hybris.com/cockpitng/config/explorertree"xmlns:list-view="http://www.hybris.com/cockpitng/component/listView"xmlns:simple-search="http://www.hybris.com/cockpitng/config/simplesearch"xmlns:wz="http://www.hybris.com/cockpitng/config/wizard-config"xmlns:ysl="http://www.hybris.com/cockpitng/config/simplelist">

           <y:group qualifier="common">

               <y:label>actiongroup.common</y:label>

               <y:action action-id="com.hybris.cockpitng.action.create" property="pageable.typeCode"/>

           </y:group>

       </y:actions>

</context>


<contexttype="Media" component="listviewactions" principal="otherRole" module="hideActionB">

    <y:actionsxmlns:y="http://www.hybris.com/cockpit/config/hybris"xmlns:advanced-search="http://www.hybris.com/cockpitng/config/advancedsearch"xmlns:df="http://www.hybris.com/cockpitng/component/dynamicForms"xmlns:editorArea="http://www.hybris.com/cockpitng/component/editorArea"xmlns:explorer-tree="http://www.hybris.com/cockpitng/config/explorertree"xmlns:list-view="http://www.hybris.com/cockpitng/component/listView"xmlns:simple-search="http://www.hybris.com/cockpitng/config/simplesearch"xmlns:wz="http://www.hybris.com/cockpitng/config/wizard-config"xmlns:ysl="http://www.hybris.com/cockpitng/config/simplelist">

           <y:group qualifier="common">

               <y:label>actiongroup.common</y:label>

               <y:action action-id="com.hybris.cockpitng.action.delete" property="currentObject"/>

           </y:group>

       </y:actions>

    </context>

</config>

查找更详细的步骤here