@@ -31,10 +31,10 @@ npm install --save abacl
31
31
32
32
### Usage
33
33
34
- Define your user abilities as a json array, so you can store it in your database:
34
+ Define your user policies as a json array, so you can store it in your database:
35
35
36
36
``` ts
37
- import { Ability } from ' abacl' ;
37
+ import { Policy } from ' abacl' ;
38
38
39
39
enum Role {
40
40
Admin = ' admin' ,
@@ -43,7 +43,7 @@ enum Role {
43
43
Manager = ' manager' ,
44
44
}
45
45
46
- const abilities : Ability <Role >[] = [
46
+ const policies : Policy <Role >[] = [
47
47
{
48
48
subject: Role .Admin ,
49
49
action: ' any' ,
@@ -54,6 +54,11 @@ const abilities: Ability<Role>[] = [
54
54
action: ' read' ,
55
55
object: ' article:published' ,
56
56
},
57
+ {
58
+ subject: Role .Guest ,
59
+ action: ' create:own' ,
60
+ object: ' article:published' ,
61
+ },
57
62
{
58
63
subject: Role .Manager ,
59
64
action: ' any' ,
@@ -64,11 +69,11 @@ const abilities: Ability<Role>[] = [
64
69
action: ' create:own' ,
65
70
object: ' article' ,
66
71
field: [' *' , ' !owner' ],
67
- location: [' 127.0.0.1 ' , ' 192.168.1.0/24' ],
72
+ location: [' 192.168.2.10 ' , ' 192.168.1.0/24' ],
68
73
time: [
69
74
{
70
- cron_exp: ' * * 8 * * *' ,
71
- duration: 20 * 60 * 60 ,
75
+ cron_exp: ' * * 7 * * *' , // from 7 AM
76
+ duration: 9 * 60 * 60 , // for 9 hours
72
77
},
73
78
],
74
79
},
@@ -81,7 +86,7 @@ const abilities: Ability<Role>[] = [
81
86
subject: Role .User ,
82
87
action: ' read:shared' ,
83
88
object: ' article' ,
84
- filter: [' *' , ' !id ' ],
89
+ filter: [' *' , ' !owner ' ],
85
90
},
86
91
{
87
92
subject: Role .User ,
@@ -92,7 +97,7 @@ const abilities: Ability<Role>[] = [
92
97
subject: Role .User ,
93
98
action: ' update:own' ,
94
99
object: ' article' ,
95
- field: [' *' , ' !owner' ],
100
+ field: [' *' , ' !id ' , ' ! owner' ],
96
101
},
97
102
];
98
103
```
@@ -117,20 +122,20 @@ const article = {
117
122
Create a new access control object, then get the permission grants:
118
123
119
124
``` ts
120
- import AccessControl from ' abacl' ;
125
+ import AccessControl , { normalize } from ' abacl' ;
121
126
122
127
// The `strict` `AccessControlOption` control the scoped functionality
123
128
// default strict value is true, you can change it on the `can` method
124
129
125
- const ac = new AccessControl (abilities , { strict: false });
130
+ const ac = new AccessControl (policies , { strict: false });
126
131
const permission = ac .can ([user .subject ], ' read' , ' article' );
127
132
128
133
// change strict mode dynamically, Example:
129
134
// const strictPermission = ac.can([user.subject], 'read', 'article', undefined, { strict: true });
130
135
131
136
/**
132
137
* it('should change strict mode dynamically', () => {
133
- * const ac = new AccessControl(abilities , { strict: true });
138
+ * const ac = new AccessControl(policies , { strict: true });
134
139
*
135
140
* expect(ac.can([Role.User], 'read', 'article:published').granted).toBeFalsy();
136
141
*
@@ -143,48 +148,40 @@ const permission = ac.can([user.subject], 'read', 'article');
143
148
if (permission .granted ) {
144
149
// default scope for action and object is `any` and `all`
145
150
146
- if (permission .has (' own' )) {
147
- // Or pattern 'own:.*'
151
+ if (permission .has ({ action: ' read:own' })) {
148
152
// user has read owned article objects
149
153
}
150
154
151
- if (permission .has (' shared' )) {
152
- // Or pattern 'shared:.*'
155
+ if (permission .has ({ action: ' read:shared' })) {
153
156
// user can access shared article objects
154
157
}
155
158
156
- if (permission .has (' published' )) {
157
- // Or pattern '.*:published'
159
+ if (permission .has ({ object: ' article:published' })) {
158
160
// user can access shared article objects
159
161
}
160
162
161
163
// do something ...
162
164
163
- // get grants by pattern 'shared' or 'shared:.*'
164
- // pattern: [action_scoped_regex]:[object_scoped_regex]
165
- const response = permission .filter (article ); // OR
166
- const response = permission .grant (' shared' ).filter (article );
167
-
168
- // Now response has no `id` property so sent it to user
165
+ const response = permission .filter (article );
169
166
}
170
167
```
171
168
172
169
Time and location access check example:
173
170
174
171
``` ts
175
- import { Permission } from ' abacl' ;
172
+ import { AccessControl , Permission } from ' abacl' ;
176
173
177
174
// default `strict` value is true
178
- const ac = new AccessControl (abilities , { strict: true });
175
+ const ac = new AccessControl (policies , { strict: true });
179
176
180
- const permission = ac .can ([user .subject ], ' create' , ' article' , (perm : Permission ) => {
181
- return perm .location (user .ip ) && perm .time (); // OR Alternative Method
182
- return perm .grant (' own' ).location (user .ip ) && perm .grant (' own' ).time ();
177
+ const permission = ac .can ([user .subject ], ' create' , ' article' , {
178
+ callable : (perm : Permission ) => {
179
+ return perm .location (user .ip ) && perm .time ();
180
+ },
183
181
});
184
182
185
183
if (permission .granted ) {
186
- const inputData = permission .field (article ); // OR
187
- const inputData = permission .grant (' .*' ).field (article );
184
+ const inputData = permission .field (article );
188
185
189
186
// the `inputData` has not `owner` property
190
187
// do something and then return results to user
0 commit comments