the dreamer
the dreamer

Reputation: 23

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

How can I remove a button action in backoffice according to the user? I was able to disable the button by adding a condition in the canPerform method like this

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

But I want to hide the button instead of making it disable.

Upvotes: 2

Views: 2554

Answers (1)

HybrisHelp
HybrisHelp

Reputation: 5810

I hope you already have a custom backoffice extension, if not then follow this tutorial to create one.

Now, in yourcustombackoffice-backoffice-config.xml you can declare listviewactions component for your itemtype with those actions which you want to allow to the user/group. Then you need to assign that user's role/group to the principal attribute.

For example, there are two backoffice roles "mainRole" and "otherRole". "mainRole" role has assigned to X user and "otherRole" role to Y user. Now using below backoffice config, X user can only see the Create button and Y user can only see Delete button.

<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>

Find more detailed steps here

Upvotes: 2

Related Questions