简体   繁体   English

Angular - 如何在侧边菜单应用用户角色权限

[英]Angular - How to apply user roles permission on the side menu

In my Angular-14 application, I have these roles from the API:在我的 Angular-14 应用程序中,我有来自 API 的这些角色:

  1. Admin行政
  2. Teacher老师
  3. Student学生

I use userRole = localStorage.getItem('role') to display this.我使用 userRole = localStorage.getItem('role') 来显示它。 Each user can only have a role.每个用户只能有一个角色。

I want to apply the roles to the sidebar, so that only the specified roles will see the allowed menu.我想将角色应用到侧边栏,这样只有指定的角色才能看到允许的菜单。

sidebar.ts:侧边栏.ts:

export class SidebarComponent implements OnInit {
  @HostBinding('class') classes: string = BASE_CLASSES;
  public menu = MENU;
  userRole = localStorage.getItem('role');

  constructor(
  ) {}

  ngOnInit() {

  }

}
export const MENU = [
  {
      name: 'Dashboard',
      iconClasses: 'fas fa-tachometer-alt',
      path: ['/admin-dashboard']
  },
  {
      name: 'Student List',
      iconClasses: 'fas fa-users',
      path: ['/admin-dashboard/students-list']
  },
  {
    name: 'Payments',
    iconClasses: 'fas fa-calendar',
    children: [
        {
            name: 'Fee List',
            iconClasses: 'far fa-circle nav-icon',
            path: ['/admin-dashboard/fee-list']
        },
        {
            name: 'My Fees',
            iconClasses: 'far fa-circle nav-icon',
            path: ['/admin-dashboard/myfees']
        }
    ]
  }
];

sidebar.html:侧边栏.html:

  <nav class="mt-2" style="overflow-y: hidden">
      <ul
          class="nav nav-pills nav-sidebar flex-column"
          data-widget="treeview"
          role="menu"
          data-accordion="false"
      >
          <app-menu-item
              *ngFor="let item of menu"
              [menuItem]="item"
          ></app-menu-item>
      </ul>
  </nav>

I want to to achieve these:我想实现这些:

  1. All users to view Dashboard所有用户查看仪表板
  2. Only Admin and Teacher to view Student List只有管理员和老师才能查看学生列表
  3. Only Admin to view Fee List只有管理员才能查看费用清单
  4. Only Student to view My Fees只有学生可以查看我的费用

How do I apply userRole() to achieve this?如何应用 userRole() 来实现这一点?

Thanks谢谢

You would still want to look into route guards to lock down the actual route but you can conditionally add menu items您仍然希望查看路线守卫以锁定实际路线,但您可以有条件地添加菜单项

export class SidebarComponent implements OnInit {
  @HostBinding('class') classes: string = BASE_CLASSES;
  userRole = localStorage.getItem('role') as "admin" | "teacher" | "student";
  public menu = [
    {
        name: 'Dashboard',
        iconClasses: 'fas fa-tachometer-alt',
        path: ['/admin-dashboard']
    },
    ...(this.userRole === "admin" || this.userRole === "teacher" ? [{
        name: 'Student List',
        iconClasses: 'fas fa-users',
        path: ['/admin-dashboard/students-list']
    }] : []), 
    ...(this.userRole === "admin" || this.userRole === "student" ? [{
      name: 'Payments',
      iconClasses: 'fas fa-calendar',
      children: [
          ...(this.userRole === "admin" ? [{
              name: 'Fee List',
              iconClasses: 'far fa-circle nav-icon',
              path: ['/admin-dashboard/fee-list']
          }] : []),
          ...(this.userRole === "student" ? [{
              name: 'My Fees',
              iconClasses: 'far fa-circle nav-icon',
              path: ['/admin-dashboard/myfees']
          }] : [])
      ]
    }] : [])
  ];

  constructor(
  ) {}

  ngOnInit() {
  }
}

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

相关问题 Angular 根据用户角色显示和隐藏侧面菜单栏项目 - Angular show and hide the side menu bar items based on the user roles 如何在角度2中管理角色和权限 - How to manage the roles and permission in angular 2 如何基于角度的用户角色/权限管理菜单 - How to manage menu based on user roles/permissions in angular 如何在没有角色7的用户权限的情况下阻止材料菜单关闭 - How to prevent material menu from closing without user permission in angular 7 如何在由 angular 和 .net 核心 web api 组成的全栈应用程序中管理用户角色和权限 - how to manage user roles and permission in a full stack application made up of angular and .net core web api 如何在 Angular7 中将过滤器应用于 *ngFor 角色 - How to apply filters to *ngFor Roles in Angular7 Angular 应用程序的角色和权限工具 - Roles and Permission Tools for Angular Applications Angular - 如何选中和取消选中所有用户角色 - Angular - How to Check and Uncheck All User Roles 如何在angular2中显示和隐藏侧面菜单 - How to show and hide side menu in angular2 如何在 ionic / angular 中刷新侧边菜单 - how to refresh side menu in ionic / angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM