[英]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.