Serenity: how to control navigation items based on user authentication

Created on 6 Jul 2020  路  4Comments  路  Source: serenity-is/Serenity

Hi;

I need help in displaying a set of menu items for public(anyone who enters the site) and the rest of menu items when users are authenticated.
Is it possible?

Thanks

Most helpful comment

@ms4hafiz
You might also want to check out the WIKI
https://github.com/volkanceylan/Serenity/wiki/UI:-Dynamic-navigation-items

All 4 comments

You can set permissions for which users can see various menu items in the navigation.cs files - use the "Permission" parameter within the NavigationLink.

Example:
[assembly: NavigationLink(int.MaxValue, "Menu/Menu Item", typeof(MyPages.xyzController), icon: "fa fa-clipboard", Permission=xyzModule.PermissionKeys.Admin)] // replace xyzModule.PermissionKeys.Admin with your permission string constant or literal such as "Module:Admin"

I haven't tried removing permissions from menu items altogether but would guess that providing an empty string for the permissions parameter would achieve that.

If you want unauthenticated users to be able to access the site, you will also need to make sure that your default page does not have the [PageAuthorize] attribute on it's controller class.

Obviously you will need to be careful what data you expose by doing this and make sure you have appropriate permissions on your xyzRow entities etc.

@sjcdtm

Thank you for the response
I have already set permission to xxRow.cs so that will will handle permission for authenticated users. I am thinking of conditionally allowing users to have to menus:

If (anyuer.authorized==true)
{

  1. Menu Item 1
  2. Menu Item 2
  3. Menu Item 3
  4. Menu Item 4
    }
    else // these will shown for all users (authenticated or not authenticated)
    {

    1. Menu item 5

    2. Menu item 6

    3. Menu item 7

      }

The example that I gave above allows you to override the permission that is inherited from the xyzRow for the menu items (assuming you mean the standard navigation menus).

If the user does not have the permission specified in the NavigationLink, the menu item does not appear. You will find that non-authenticated users will be redirected to the login page if you have [PageAuthorization] attribute on the default page controller though.

If you want to be able to change the menus in code as your example above, this can be achieved using Dynamic Navigation, there are a few examples of this such as in #4346

@ms4hafiz
You might also want to check out the WIKI
https://github.com/volkanceylan/Serenity/wiki/UI:-Dynamic-navigation-items

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stepankurdylo picture stepankurdylo  路  3Comments

JohnRanger picture JohnRanger  路  3Comments

Shraddha996 picture Shraddha996  路  3Comments

StefanTheiner picture StefanTheiner  路  3Comments

Akarsh03 picture Akarsh03  路  3Comments