2022-11-13 23:00:59 +08:00
# ACLRole
2022-10-31 11:52:17 +08:00
2023-02-18 23:23:14 +08:00
ACLRole is the user role class in ACL system. In ACL systems, roles are usually defined by `acl.define` .
2022-10-31 11:52:17 +08:00
2023-02-18 23:23:14 +08:00
## Class Methods
2022-10-31 11:52:17 +08:00
### `constructor()`
2023-02-18 23:23:14 +08:00
Constructor.
**Signature**
2022-10-31 11:52:17 +08:00
* `constructor(public acl: ACL, public name: string)`
2023-02-18 23:23:14 +08:00
**Detailed Information**
* acl - ACL instance
* name - Name of the role
2022-10-31 11:52:17 +08:00
### `grantAction()`
2023-02-18 23:23:14 +08:00
Grant the action permission to the role.
2022-10-31 11:52:17 +08:00
2023-02-18 23:23:14 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `grantAction(path: string, options?: RoleActionParams)`
2023-02-18 23:23:14 +08:00
**Type**
2022-10-31 11:52:17 +08:00
```typescript
interface RoleActionParams {
fields?: string[];
filter?: any;
own?: boolean;
whitelist?: string[];
blacklist?: string[];
[key: string]: any;
}
```
2023-02-18 23:23:14 +08:00
**Detailed Information**
2022-10-31 11:52:17 +08:00
2023-02-18 23:23:14 +08:00
* path - Action path of the resource, such as `posts:edit` , which means the `edit` action of the `posts` resource. Use colon `:` to separate the name of resource and action.
2022-10-31 11:52:17 +08:00
2023-02-18 23:23:14 +08:00
When RoleActionParams is to grant permission, the corresponding action can be configured with parameters to achieve finer-grained permission control.
2022-10-31 11:52:17 +08:00
2023-02-18 23:23:14 +08:00
* fields - Accessible fields
2022-10-31 11:52:17 +08:00
```typescript
acl.define({
role: 'admin',
actions: {
'posts:view': {
2023-02-18 23:23:14 +08:00
// admin user can request posts:view action, but limited to the configured fields
2022-10-31 11:52:17 +08:00
fields: ["id", "title", "content"],
},
},
});
```
2023-02-18 23:23:14 +08:00
* filter - Permission resource filtering configuration
2022-10-31 11:52:17 +08:00
```typescript
acl.define({
role: 'admin',
actions: {
'posts:view': {
2023-02-18 23:23:14 +08:00
// admin user can request posts:view action, but the listed results is filtered by conditions in the filter
2022-10-31 11:52:17 +08:00
filter: {
2023-02-18 23:23:14 +08:00
createdById: '{{ ctx.state.currentUser.id }}', // Template syntax is supported to take the value in ctx, and will be replaced when checking permissions
2022-10-31 11:52:17 +08:00
},
},
},
});
```
2023-02-18 23:23:14 +08:00
* own - Whether to access only your own data
2022-10-31 11:52:17 +08:00
```typescript
const actionsWithOwn = {
'posts:view': {
"own": true //
}
}
2023-02-18 23:23:14 +08:00
// Equivalent to
2022-10-31 11:52:17 +08:00
const actionsWithFilter = {
'posts:view': {
"filter": {
"createdById": "{{ ctx.state.currentUser.id }}"
}
}
}
```
2023-02-18 23:23:14 +08:00
* whitelist - Whitelist, only the fields in whitelist can be accessed
* blacklist - Blacklist, fields in blacklist cannot be accessed