简体   繁体   English

Angular 在路由基于角色的授权中分配 2 个角色

[英]Angular assign 2 roles in a routing Role Based Authorization

I am creating a mean stack application and i want to add a Role Based Authorization in some routing modules, i managed to do that but i couldn't assign multiple roles in one authorization this is m hasRole guard:我正在创建一个平均堆栈应用程序,并且我想在某些路由模块中添加基于角色的授权,我设法做到了,但我无法在一个授权中分配多个角色,这是 m hasRole 保护:

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

const isAuthorized= this.user.role.includes(route.data.role);
if (!isAuthorized){
  window.alert('ALERT : you are not authorized !!')
}
 return isAuthorized;


}

and this is the code in the component code:这是组件代码中的代码:

{
path: 'listuser',
component: ListUsersComponent,
canActivate:[HasRoleGuard],
data:{
  role:"Admin"
}

} }

i want to add two roles Admin and Manager i tried我想添加两个角色 Admin 和 Manager 我试过

data:{
  role:["Admin"||"Manager"]
}

but it didn't work但它没有用

Well first of all you should do it like this:那么首先你应该这样做:

data:{
  roles:["Admin", "Manager"]
}

and then you have to check it like this:然后你必须像这样检查它:

const intersectedRoles = this.user.role.filter(value => route.data.roles.includes(value));
const isAuthorized= intersectedRoles.length? true : false;

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

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