You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
abp/docs/zh-Hans/UI/Angular/Permission-Management.md

81 lines
2.0 KiB

5 years ago
# 权限管理
5 years ago
5 years ago
权限是为特定用户,角色或客户端授予或禁止的简单策略. 你可以在[ABP授权文档](../../Authorization.md)中阅读更多信息.
5 years ago
你可以使用 `ConfigState``getGrantedPolicy` 选择器获取经过身份验证的用户的权限.
5 years ago
你可以获取权限的布尔值:
5 years ago
```js
import { Store } from '@ngxs/store';
5 years ago
import { ConfigState } from '@abp/ng.core';
5 years ago
export class YourComponent {
constructor(private store: Store) {}
ngOnInit(): void {
const canCreate = this.store.selectSnapshot(ConfigState.getGrantedPolicy('AbpIdentity.Roles.Create'));
}
// ...
}
```
或者你可以通过 `ConfigStateService` 获取它:
```js
5 years ago
import { ConfigStateService } from '@abp/ng.core';
5 years ago
export class YourComponent {
constructor(private configStateService: ConfigStateService) {}
ngOnInit(): void {
const canCreate = this.configStateService.getGrantedPolicy('AbpIdentity.Roles.Create');
}
// ...
}
```
## 权限指令
你可以使用 `PermissionDirective` 来根据用户的权限控制DOM元素是否可见.
```html
5 years ago
<div *abpPermission="'AbpIdentity.Roles'">
5 years ago
仅当用户具有`AbpIdentity.Roles`权限时,此内容才可见.
</div>
```
如上所示,你可以使用 `abpPermission` 结构指令从DOM中删除元素.
该指令也可以用作属性指令,但是我们建议你将其用作结构指令.
## 权限守卫
1 year ago
如果你想要在导航过程中控制经过身份验证的用户对路由的访问权限,可以使用 `permissionGuard`.
5 years ago
1 year ago
* 从@abp/ng.core导入permissionGuard.
* 添加 `canActivate: [permissionGuard]` 到你的路由对象.
* 添加 `requiredPolicy` 到路由模块路由的 `data` 属性.
5 years ago
```js
1 year ago
import { permissionGuard } from '@abp/ng.core';
5 years ago
// ...
5 years ago
const routes: Routes = [
{
path: 'path',
component: YourComponent,
1 year ago
canActivate: [permissionGuard],
5 years ago
data: {
requiredPolicy: 'YourProjectName.YourComponent', // policy key for your component
5 years ago
},
},
];
```
授予的策略存储在 `ConfigState``auth` 属性中.
## 下一步是什么?
* [多租户](./Multi-Tenancy.md)