【问题标题】:Hiding the required menu items for the particular user in angular 2以角度 2 隐藏特定用户所需的菜单项
【发布时间】:2018-08-04 10:14:01
【问题描述】:

我创建了一个 Angular 应用程序,其中我有一个登录表单,我在其中使用 service 和 guard 实现了身份验证。

我已经定义了一个用户名和密码的界面用户。在authentication.service.ts中使用这个界面我已经定义了两个用户adminuser强>。

我还实现了一个名为 authentication.guard.ts 的守卫,我使用 CanActivate RouteGaurd 将用户路由到他可以访问的页面。

在我的应用程序中有一个只能由 admin 用户访问的管理页面,因此如果管理员仅登录,它就会导航到 管理页面。如果 用户登录,然后他将被重定向到主页跳过管理页面

adminuser 登录应用程序时,会出现一个菜单栏,其中包含三个菜单项 Home, AdminActivity ,RegisterUser

如果 admin 登录应用程序,我试图隐藏 AdminActivity 菜单项,如果 user 则隐藏 RegisterUser 菜单项 登录应用程序。

我尝试在 app.component.ts 中导入身份验证服务,但无法获取记录的用户名和密码。

任何人都可以帮助我实现所需的功能,因为我需要在我的应用程序中实现

请访问我的示例应用程序here

【问题讨论】:

  • 用户名是否保存在本地存储中?
  • @AbdulRafay.....是的.....如authentication.service.ts中所示.......let authenticatedUser = users.find(u => u .username === this.user.username); if(authenticatedUser && authenticatedUser.password==this.user.password){ localStorage.setItem("user", this.user.username); this._router.navigate(['/Admin']); }
  • 更新您的代码以检查具有本地存储的管理员用户:isSuperAdmin() { if (localStorage.getItem('user') === "admin") { return true; } 否则 { 返回假; } }
  • @Heena 请考虑在您的问题中发布您的代码,方法是点击您问题下方的编辑链接。

标签: angular authentication angular-material angular-flex-layout


【解决方案1】:

更新了您的示例代码HERE

使用BehaviorSubject 跟踪用户类型并使用它来显示/隐藏菜单项。您可以使用它在整个应用程序的不同组件之间进行通信。我删除了您的一些导致 Stackblitz 错误的代码,但不要专注于此。

AuthenticationService 的变化:

声明userTypeBehaviorSubject为:

  userType: BehaviorSubject<string> = new BehaviorSubject<string>(this.getUserType());

添加了从本地存储中获取用户类型的方法:

  getUserType() {
    return localStorage.getItem('user')
  }

在登录时更新userTypeBehaviorSubject

login(username, password) {
...
localStorage.setItem("user", this.user.username);
this.userType.next(this.user.username);

app.componet.ts 的变化:

订阅userTypeBehaviorSubject

  userType: string = '';

  ngOnInit(){
    ...
    this.authService.userType.subscribe(value => this.userType = value);
  }

app.componet.html 的变化:

使用*ngIf显示/隐藏菜单项:

  <button *ngIf="userType === 'admin'" mat-raised-button routerLink='application/AdminAccess/admin-activity' routerLinkActive="active">Admin Activity </button>

  <button *ngIf="userType === 'user'" mat-raised-button routerLink='application/UserManagement/add-users' routerLinkActive="active">Register User</button>

【讨论】:

  • 非常感谢您的反馈......它的工作正是我所需要的......我修改了我早期应用程序中的更改并且工作正常......再次非常感谢......
【解决方案2】:

由于您的 stackbliz 无法正常工作,但下面我想提供一些有用的代码:-

在您的 routing.module.ts 中,修改您的“管理员”路由,

{path:'Admin',component: AdminComponent,
data: {
    authorities: ['ROLE_ADMIN'],
},
canActivate: [AuthenticationGuard]}

在你的 authenticationguard.guard.ts 中,修改如下:-

 constructor( private auth: AuthenticationService,  private _router: Router) {} 
    
 

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Promise<boolean> {

        const authorities = route.data['authorities'];
        // here you get authorities user role name, now you can apply your service logic
         if (!this.auth.isSuperAdmin(authorities)) { // add arg in isSuperAdmin to validate username
      this._router.navigate(['/application/home']);
      return false; 
    }
    
    return true;
    }

希望这会有所帮助。

【讨论】:

  • 感谢您的回复...当我修改我的 authenticationguard.guard.ts 时,它导致我的控制台出现以下错误.....无法调用类型缺少呼叫签名的表达式.类型“布尔”没有兼容的调用签名。
猜你喜欢
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多