简体   繁体   English

Angular 根据用户角色显示和隐藏侧面菜单栏项目

[英]Angular show and hide the side menu bar items based on the user roles

I am loading angular side menu bar based the response getting from server.我正在根据从服务器获得的响应加载 angular 侧边菜单栏。 And want to conditionally show and hide the menu items based on the user roles.并希望根据用户角色有条件地显示和隐藏菜单项。

userRoles = [1,2];

Below is the JSON getting from server,以下是从服务器获取的 JSON,

{
  "menu": [
    {
      "title": "Merchant",
      "order": 1,
      "subMenu": [
        {
          "order": 1,
          "subMenu1": "Initiate"
        }
      ]
    },
    {
      "title": "Prepaid",
      "order": 2,
      "subMenu": [
        {
          "order": 1,
          "subMenu1": "Merch"
        }
      ]
    }

And the sample angular menu item,以及示例 angular 菜单项,

 <ul class="sidebar-menu collapsible collapsible-accordion" data-collapsible="accordion">
                <ng-container>
                    <li *ngFor="let parentMenu of sideMenuResponse" class="no-padding">
                        <a class="collapsible-header">
                            <i class="material-icons"></i> {{parentMenu.title}}
                            <i class="nav-drop-icon material-icons">keyboard_arrow_right</i>
                        </a>
                            <div class="collapsible-body">
                                <ul *ngFor="let submenu of parentMenu.subMenu">
                                       <li>
                                        <a target="_self" [routerLink]="">
                                            {{submenu.subMenu1}}</a>
                                       </li> 
                                </ul>
                            </div>
                    </li>
                </ng-container>
            </ul> 

And want to hide the Initiate2 submenu if the user role is 1如果用户角色为1 ,则希望隐藏Initiate2子菜单

And hide the Prepaid menu if the user role is 2如果用户角色为2 ,则隐藏Prepaid菜单

Can any one help me to achieve this?任何人都可以帮助我实现这一目标吗?

First question is why is the server sending down unavailable menu options?第一个问题是为什么服务器发送不可用的菜单选项? Negates any reason for the server doing that in the first place;首先否定服务器这样做的任何理由; save the effort, data, and cpu cycles to just keep the whole thing UI-side only.节省精力、数据和 CPU 周期,只保留整个 UI 端。

So, first answer is to not touch the UI and filter it appropriately server-side.所以,第一个答案是不要触摸 UI 并在服务器端适当地过滤它。

Second answer is to apply filtering based on what you want.第二个答案是根据您的需要应用过滤。

I'm assuming that order has something to do with the user roles?我假设order与用户角色有关? The 1/2 kind of match up to what you're saying. 1/2 与您所说的匹配。

First: change the naming.第一:更改命名。 Order.= access control. Order.=访问控制。 It's confusing.这很混乱。 Order suggests display order on-screen for this menu and not at all that it's relevant to showing or hiding anything. Order建议此菜单在屏幕上的显示顺序,与显示或隐藏任何内容无关。

Second: include only what the user has access to - get the base menu items, and then pull out only what the user can see:第二:仅包含用户有权访问的内容——获取基本菜单项,然后仅拉出用户可以看到的内容:

Note: I'm going to change the shape of the objects for this purpose - I don't care about order but I do care about requiresRole for the user to be able to see it...注意:为此,我将更改对象的形状——我不关心order ,但我关心requiresRole以便用户能够看到它……

public userMenu: SideMenu[];

private onMenuReceived(menu: SideMenu[]): void {
    const userRoles = this.userService.getUserRoles(); // Assuming this exists to provide me with [1], [2], or even [1, 2] if that's a possibility

    const result = menu.filter(x => userRoles.includes(x.requiresRole));
    result.forEach((item: SideMenu) => {
        item.subMenu = item.subMenu.filter(x => userRoles.includes(x.requiresRole));
    });

    this.userMenu = result;
}

This gives you only the top-level menu items wherein the user has the required role, and then for each of those, trims down the submenus to only those wherein the user has the required role.这只为您提供用户具有所需角色的顶级菜单项,然后对于每个菜单项,将子菜单缩减为仅用户具有所需角色的子菜单。

Out of this, this.userMenu should contain all the menu and sub-menu items that conform to the user having the relevant required role(s).其中, this.userMenu应该包含符合具有相关所需角色的用户的所有菜单和子菜单项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM