Apply User Permission on both Side Menu And Top Menu

Hi Team ,

Currently we are migratting From Cuba 6.10 to Cuba 7.1

And we are following the Sampler code to create our new Interface

Now we have two Menus Side Menu And Top Menu

Side Menu Is Controlled by code using xsd and menu xml files

While Top Menu is handled by default using cuba web-menu.xml

image

Currently We need to add permissions over side Menu so we can control show - hide according to each user permissions

Is their any straight forward way to do this , or it is requested to implement my own isPermitted function to call it while drawing side Menu items

Allam,
Thanks

Hi @abd.ibrahim.allam,
The standard way to show/hide menu items for users is to use roles. There is an example in the manual as well.

When the user logs in, cuba platform will load the user permissions based on the roles that he has. Then, if any role grants access to the menu, the menu will be shown, otherwise not.

Regards.

Hi @peterson.machado ,

Thanks for your replay , but seems you didn’t get me question right , I’m not asking about default Cuba default role permissions ,

My Question is about custom Menu implementation and how can i handle access to them.

Thanks

Hi @abd.ibrahim.allam,
Now I think I understood.

If your menu is not static, I would suggest you to use “Specific Permission” (search for “The Specific tab configures named functionality permissions” in the manual.

Then, when adding menu entries programatically, you can check if the user has permission before adding the menu entry:

@Inject
protected Security security;

protected boolean userCanUseFeature() {
return security.isSpecificPermitted(“my.custom.feature”);
}

Take a look at FilterDelegateImpl, method userCanEditFilers() to inspect how this functionality is used by the platform itself. Later you can update user permission changing the Role permissions (Specific tab).

Hope it helps.
Regards.

1 Like

Hi @peterson.machado ,

I do really appreciate your support regarding my question .

I just want to clarify point regarding My custom Menu , it is menu defined on another xml file other than one defined inside web-menu.xml generated by Cuba ,

Hope you pass through Sampler application code , that will make you got over my point

While parsing my-custom-menu.xml I want to make same behavior as Cuba are doing for handling isPermitted over each menu item

That one can be handled by custom function , My Main Question is their any way to make if from Cuba Administration ,

Or shall I write custom function for fetching user roles and each menu item has role - with permission each role will be mapping at the end to screen which will be reflecting menu item

Thanks

Hi @abd.ibrahim.allam,
I took a look at the Sampler app. I must admit I don’t understand why would you like to rewrite the menu from scratch, but I’ll share my thoughts on how I think you can do it.

I used cuba-petclinic project as an example for the instructions below.

  1. For every screen on my-custom-menu.xml, I would create an entry in web-permissions.xml file (just like the sample in the documentation):
    image

This step is required to enable the admin to setup permissions using cuba standard Role Edit screens.

  1. Implement isPermitted method for your menu entries (as you already mentioned). In this method you should evaluate user specific permissions:

@Inject
protected Security security;

protected boolean isScreenPermitted(String screenId) {
return security.isSpecificPermitted(screenId); //“app.myScreen” for example
}

Hope I understood correctly now.
Regards.

Hi @peterson.br

Thanks for sharing information about how i can handle specific permissions

I will share with you , why I’m going to implement custom-menu - > If you want to create two menus inside your application which on my case , one menu containing Administration and Help on TOP and all other elements will be on the right side

If you know a way to put all menu items inside web-menu.xml and then display some on the Top menu using

<menu id="mainMenu" align="MIDDLE_LEFT"/>

and put the rest of the items on the side menu using the below line

<sideMenu id="sideMenu" width="100%"/>

as my situation their is no need to override even the side menu implementation as i’m opening the editor and browser screen not fragments as done on the sampler application

other wise from my thinking the only option to do so is to implement custom-menu

correct me if i’m wrong :slight_smile:

Thanks

Hi @peterson.br ,

To make it clear for you also if you can share idea , i got solution and i want to share it with you ,

Consider blow is My main screen

`




        <menu id="mainMenu" align="MIDDLE_LEFT"/>

        <userIndicator id="userIndicator" align="MIDDLE_LEFT"/>

        <hbox id="mainButtonsBox" stylename="c-main-buttons" align="MIDDLE_LEFT">
            <newWindowButton id="newWindowButton"
                             icon="app/images/new-window.png"
                             description="msg://newWindowBtnDescription"/>

            <logoutButton id="logoutButton"
                          icon="app/images/exit.png"
                          description="mainMsg://logoutBtnDescription"/>
        </hbox>
    </hbox>

    <split id="mainSplit" width="100%"
           maxSplitPosition="50%"
           orientation="horizontal"
           pos="300px"
           settingsEnabled="false">
        <vbox id="sideMenuBox"
              expand="sideMenu"
              height="100%"
              spacing="true">
            <hbox id="appTitleBox"
                  spacing="true"
                  stylename="c-sidemenu-title"
                  width="100%">
                <label id="appTitleLabel"
                       align="MIDDLE_CENTER"
                       value="msg://application.caption"/>
            </hbox>
            <cssLayout id="searchBox"
                       stylename="v-component-group search-box"
                       width="100%">
                <textField id="searchField"
                           inputPrompt="msg://sideMenu.search"
                           width="100%"/>
                <button id="searchButton"
                        caption="msg://sideMenu.search"
                        icon="font-icon:SEARCH"/>
            </cssLayout>
            <hbox id="menuHeaderBox"
                  expand="menuCaptionLabel"
                  margin="false,true,false,true"
                  spacing="true"
                  width="100%">
                <label id="menuCaptionLabel"
                       stylename="cuba-folders-pane-caption"
                       value="msg://sideMenu.menuCaption"/>
                <linkButton id="collapseAllBtn"
                            align="MIDDLE_RIGHT"
                            description="collapse All"
                            icon="font-icon:COMPRESS"
                            stylename="small-link dark"/>
                <linkButton id="expandAllBtn"
                            align="MIDDLE_RIGHT"
                            icon="font-icon:EXPAND"
                            description="Expand All"
                            stylename="small-link dark"/>
            </hbox>
            <sideMenu id="sideMenu"
                      width="100%"/>
            <hbox id="localesBox"
                  expand="spacer"
                  margin="false,false,false,true"
                  width="100%">

                <label id="spacer"/>

                <label id="localesLabel"
                       align="MIDDLE_RIGHT"
                       stylename="bold"
                       value="msg://locales.caption"/>
                <lookupField id="localesSelect"
                             align="MIDDLE_RIGHT"
                             nullOptionVisible="false"
                             stylename="borderless"
                             textInputAllowed="false"
                             width="100px"/>
            </hbox>
        </vbox>

        <workArea id="workArea" width="100%" height="100%">
            <initialLayout>
                <fragment id="dashboardFrame"
                          screen="sampler-main-dashboard-fragment1"
                          height="100%"
                          width="100%"/>
            </initialLayout>
        </workArea>
    </split>
</layout>

`

Now what i’m thinking is to put all menu items inside web-menu.xml normal cuba ,

And Inside init of the main screen , I will get all menu items user has already has access ,

Which i don’t know if their is proper way to fetch them and then put draw them inside the side menu ,

I was looking also this links

https://doc.cuba-platform.com/manual-7.1/gui_SideMenu.html

https://doc.cuba-platform.com/manual-7.1/gui_AppMenu.html

https://doc.cuba-platform.com/manual-7.1/folders_pane.html

If their is way to do so , it will fix actually all my issue

Thanks

Hi @peterson.br ,

I make it work ,

What I did is added both mainmenu item and sidemenu item on my main-window

Cuba by default will add all men items appearing on the web-menu.xml to both menus

Inside main-window-controller onInit function just removing what ever i want not to appear on the top-menu and remove what ever i want to hide on the side-menu

Hi @peterson.br ,

Now this is how my application looks like now ,

image

Once you press the upper menu item which is only parent menu item ,
display all items under that men on the side-menu

2 Likes