简体   繁体   English

我们如何才能对 angular 应用程序的某些功能进行基于角色的身份验证?

[英]How can we approach to have role based authentication to some of the functionalities of angular app?

I am trying to provide role based access to certain functionalities in the app.我正在尝试提供对应用程序中某些功能的基于角色的访问。 For example, we have Delete functionality option, so that delete option should only be visible to certain level of roles (users) and should only have read-only access, While for some can delete it as well.例如,我们有删除功能选项,因此删除选项应该只对某些级别的角色(用户)可见,并且应该只有只读访问权限,而对于某些人也可以删除它。 Is such approach possible?这种方法可行吗? The application already provides access using identity management.该应用程序已经使用身份管理提供访问权限。 Just wanted to know can this be possible role based, or may be we can access the database of the idm.只是想知道这可能是基于角色的,还是我们可以访问 idm 的数据库。 Any idea of how this can be implemented are welcome.欢迎任何关于如何实施的想法。

Yes it is possible.对的,这是可能的。 What you can do is define an auth service.您可以做的是定义身份验证服务。 If you use NgRx, the below solution could what you want.如果你使用 NgRx,下面的解决方案可能是你想要的。 In this service keep for example currentRole$ as below:在此服务中,例如currentRole$如下所示:

auth.service.ts auth.service.ts

import { Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { selectUser } from './store/selectors';
import { selectIsLoggedIn } from './store/selectors';

export enum ROLES 
  customer = 'client',
  manager = 'manager',
  admin = 'admin'
}

@Injectable({
  providedIn: 'root'
})

export class AuthService {
  private currentRole$: Observable<ROLES>;
  private isLoggedIn$: Observable<boolean>;

constructor(private store: Store) {
    this.currentRole$ = this.store.pipe(
      select(selectUser),
      filter((user) => !!user),
      map((user) => user.role)
    );


    this.isLoggedIn$ = this.store.pipe(select(selectIsLoggedIn));

   }

 getCurrentRole(): Observable<ROLES> {
    return this.currentRole$;
  }

  isLoggedIn(): Observable<boolean> {
    return this.isLoggedIn$;
  }
   
} 

You can get subscribe the current role in your component.您可以订阅组件中的当前角色。 Based on the currentRole you can decide whether you can show the UI to the user.根据currentRole ,您可以决定是否可以向用户显示 UI。

app.component.html app.component.html

<button *ngIf="(currentRole$ | async) === ROLES.client (click)="onMenuClick()"> Client Button </button>

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

相关问题 如何在一个普通的应用程序中应用基于角色的身份验证? - How can apply role based authentication in a mean app? 基于Angular 2角色的身份验证 - Angular 2 role based authentication Angular 6基于角色的身份验证 - Angular 6 Role based Authentication 我们如何为角度应用程序拥有多个tsconfig文件 - How can we have multiple tsconfig file for angular app 当管理员角色应该能够创建子帐户时,如何在 Angular/Ionic 中使用 firebase 编写基于角色的身份验证? - How is it possbible to program a role based authentication with firebase in Angular/Ionic, when the admin role should be able to create subaccounts? 我有一个有角度的应用程序,有些元素在 cypress 中找不到 - I have an angular app and some elements can not find with cypress 我们如何在Angular 6中检查数组中的某些复选框 - How we can checked some checkboxes from the array In Angular 6 ASP.NET Core 3.1 + Angular 9 基于角色的身份验证 - ASP.NET Core 3.1 + Angular 9 role based authentication 基于JWT身份验证和角色路由到Express中的多个Angular 7项目 - Routing to multiple Angular 7 projects in Express based on JWT authentication and Role 如何将angular 2应用程序与Angular JS应用程序集成? - How can we integrate angular 2 application with Angular JS app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM