简体   繁体   English

在Angular 2中应用客户端权限

[英]Applying client side permissions in Angular 2

I'm creating a simple Angular 2 app in which I'm required to apply client-side permissions (needless to say permissions have also been applied on the server side, but hiding components which the user shouldn't see is necessary.) 我正在创建一个简单的Angular 2应用程序,其中我需要应用客户端权限(不用说服务器端也应用了权限,但隐藏用户不应该看到的组件是必要的。)

I'm not yet familiar with everything Angular has to offer, so I'd love to know what way would be acceptable in order to achieve such functionality. 我还不熟悉Angular提供的所有东西,所以我很想知道为了实现这样的功能,可接受的方式。

Or if possible, I'd love to receive some comments about the approach I've taken below: 或者如果可能的话,我希望收到一些关于我在下面采取的方法的评论:

Once the user logs in, he receives a list of permissions that he has, for example: 用户登录后,他会收到他拥有的权限列表,例如:

  • READ_POSTS READ_POSTS
  • EDIT_POSTS EDIT_POSTS
  • READ_USERS READ_USERS
  • EDIT_USERS EDIT_USERS

These for example, will determine if the user should see edit buttons around the app. 例如,这些将确定用户是否应该看到应用程序周围的编辑按钮。

Then I created a directive (basically a replica of *ngIf ) that checks if the user has permissions against the UserService which holds the list of permissions the user has. 然后我创建了一个指令(基本上是*ngIf的副本),它检查用户是否具有针对UserService权限,该UserService包含用户拥有的权限列表。

This is basically what the directive does: 这基本上是指令的作用:

if (hasPermissions) {
  this.viewContainer.createEmbeddedView(this.templateRef);
}
else {
  this.viewContainer.clear();
}

And it's used as follows: 它使用如下:

<div id="someContainer">
  <a *myPermissionDirective="'EDIT_POSTS'">Edit Post</a>
</div>

My main problem with this approach is that it seems to get ugly with generic components that contain some elements that should be displayed and some that shouldn't . 我对这种方法的主要问题是它似乎变得丑陋的通用组件包含一些应该显示的元素和一些 应该显示的元素。

For example, imagine a component called listComponent which we use to display lists, and we'd use it to display a list of users with the ability to edit them, according to the permissions you have: (currently, you can't edit other admin users) 例如,假设我们用来显示列表的名为listComponent的组件,我们将根据您拥有的权限使用它来显示能够编辑它们的用户列表:(目前,您无法编辑其他管理员用户)

  • User 1 edit-> 用户1编辑 - >
  • Admin 1 [shouldn't display edit] 管理员1 [不应显示编辑]
  • User 2 edit-> 用户2编辑 - >
  • User 3 edit-> 用户3编辑 - >

In a non generic list component, for example UserListComponent maybe we could refer to user specific permissions, but since we're using a generic ones, how would we know which of these is relevant?: EDIT_POSTS, EDIT_USERS, EDIT_ADMIN_USERS, EDIT_ARTICLE, EDIT_SYSTEM_SETTINGS etc' 在非通用列表组件中,例如UserListComponent可能我们可以引用用户特定权限,但由于我们使用的是通用权限,我们如何知道哪些是相关的?:EDIT_POSTS,EDIT_USERS,EDIT_ADMIN_USERS,EDIT_ARTICLE,EDIT_SYSTEM_SETTINGS等“

Thanks in advance. 提前致谢。

There is concept called guard in angular 2. May be you can use "can activate Guard". 在角度2有概念称为守卫。可以使用“可以激活卫兵”。

Angular 2 roles and permissions Angular 2角色和权限

For manage permissions and access control for your angular2 applications, you can use ng2-permission module. 对于angular2应用程序的管理权限和访问控制,您可以使用ng2-permission模块。

app.module.ts app.module.ts

import { Ng2Permission } from 'angular2-permission';

@NgModule({
  imports: [
    Ng2Permission
  ]
})

login.component.ts login.component.ts

import { PermissionService } from 'angular2-permission';

@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
    //...

    constructor(private _permissionService: PermissionService) { }

    login() {
        this._permissionService.clearStore();
        this._permissionService.define(['READ_POSTS', 'EDIT_POSTS', 'READ_USERS', 'EDIT_USERS']);
    }
}

*.html *。html的

<div id="someContainer">
  <a [hasPermission]="['EDIT_POSTS']">Edit Post</a>
</div>

Edit Post link will displayed, if EDIT_POSTS already defined or add in permission store. 如果EDIT_POSTS已定义或添加到权限存储中,则将显示“ Edit Post链接。

You can also use PermissionGuard from Ng2Permission module, protecting routes. 您还可以使用Ng2Permission模块中的PermissionGuard来保护路由。

您还可以使用使用相同方法的ngx-permissions库,如果您来自角度1,它看起来有点不寻常,但它是从DOM中删除对象而不是用css隐藏它的唯一方法。

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

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