Skip to content

Commit 7d4c12e

Browse files
committed
🦄 refactor: new version
1 parent 361d74c commit 7d4c12e

File tree

90 files changed

+1912
-3588
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1912
-3588
lines changed

‎README.md

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ npm install --save abacl
3131

3232
### Usage
3333

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:
3535

3636
```ts
37-
import { Ability } from 'abacl';
37+
import { Policy } from 'abacl';
3838

3939
enum Role {
4040
Admin = 'admin',
@@ -43,7 +43,7 @@ enum Role {
4343
Manager = 'manager',
4444
}
4545

46-
const abilities: Ability<Role>[] = [
46+
const policies: Policy<Role>[] = [
4747
{
4848
subject: Role.Admin,
4949
action: 'any',
@@ -54,6 +54,11 @@ const abilities: Ability<Role>[] = [
5454
action: 'read',
5555
object: 'article:published',
5656
},
57+
{
58+
subject: Role.Guest,
59+
action: 'create:own',
60+
object: 'article:published',
61+
},
5762
{
5863
subject: Role.Manager,
5964
action: 'any',
@@ -64,11 +69,11 @@ const abilities: Ability<Role>[] = [
6469
action: 'create:own',
6570
object: 'article',
6671
field: ['*', '!owner'],
67-
location: ['127.0.0.1', '192.168.1.0/24'],
72+
location: ['192.168.2.10', '192.168.1.0/24'],
6873
time: [
6974
{
70-
cron_exp: '* * 8 * * *',
71-
duration: 20 * 60 * 60,
75+
cron_exp: '* * 7 * * *', // from 7 AM
76+
duration: 9 * 60 * 60, // for 9 hours
7277
},
7378
],
7479
},
@@ -81,7 +86,7 @@ const abilities: Ability<Role>[] = [
8186
subject: Role.User,
8287
action: 'read:shared',
8388
object: 'article',
84-
filter: ['*', '!id'],
89+
filter: ['*', '!owner'],
8590
},
8691
{
8792
subject: Role.User,
@@ -92,7 +97,7 @@ const abilities: Ability<Role>[] = [
9297
subject: Role.User,
9398
action: 'update:own',
9499
object: 'article',
95-
field: ['*', '!owner'],
100+
field: ['*', '!id', '!owner'],
96101
},
97102
];
98103
```
@@ -117,20 +122,20 @@ const article = {
117122
Create a new access control object, then get the permission grants:
118123

119124
```ts
120-
import AccessControl from 'abacl';
125+
import AccessControl, { normalize } from 'abacl';
121126

122127
// The `strict` `AccessControlOption` control the scoped functionality
123128
// default strict value is true, you can change it on the `can` method
124129

125-
const ac = new AccessControl(abilities, { strict: false });
130+
const ac = new AccessControl(policies, { strict: false });
126131
const permission = ac.can([user.subject], 'read', 'article');
127132

128133
// change strict mode dynamically, Example:
129134
// const strictPermission = ac.can([user.subject], 'read', 'article', undefined, { strict: true });
130135

131136
/**
132137
* it('should change strict mode dynamically', () => {
133-
* const ac = new AccessControl(abilities, { strict: true });
138+
* const ac = new AccessControl(policies, { strict: true });
134139
*
135140
* expect(ac.can([Role.User], 'read', 'article:published').granted).toBeFalsy();
136141
*
@@ -143,48 +148,40 @@ const permission = ac.can([user.subject], 'read', 'article');
143148
if (permission.granted) {
144149
// default scope for action and object is `any` and `all`
145150

146-
if (permission.has('own')) {
147-
// Or pattern 'own:.*'
151+
if (permission.has({ action: 'read:own' })) {
148152
// user has read owned article objects
149153
}
150154

151-
if (permission.has('shared')) {
152-
// Or pattern 'shared:.*'
155+
if (permission.has({ action: 'read:shared' })) {
153156
// user can access shared article objects
154157
}
155158

156-
if (permission.has('published')) {
157-
// Or pattern '.*:published'
159+
if (permission.has({ object: 'article:published' })) {
158160
// user can access shared article objects
159161
}
160162

161163
// do something ...
162164

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);
169166
}
170167
```
171168

172169
Time and location access check example:
173170

174171
```ts
175-
import { Permission } from 'abacl';
172+
import { AccessControl, Permission } from 'abacl';
176173

177174
// default `strict` value is true
178-
const ac = new AccessControl(abilities, { strict: true });
175+
const ac = new AccessControl(policies, { strict: true });
179176

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+
},
183181
});
184182

185183
if (permission.granted) {
186-
const inputData = permission.field(article); // OR
187-
const inputData = permission.grant('.*').field(article);
184+
const inputData = permission.field(article);
188185

189186
// the `inputData` has not `owner` property
190187
// do something and then return results to user

‎coverage-badge.svg

Lines changed: 1 addition & 1 deletion
Loading

‎docs/assets/highlight.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
--dark-hl-6: #4EC9B0;
1616
--light-hl-7: #0070C1;
1717
--dark-hl-7: #4FC1FF;
18-
--light-hl-8: #098658;
19-
--dark-hl-8: #B5CEA8;
20-
--light-hl-9: #008000;
21-
--dark-hl-9: #6A9955;
18+
--light-hl-8: #008000;
19+
--dark-hl-8: #6A9955;
20+
--light-hl-9: #098658;
21+
--dark-hl-9: #B5CEA8;
2222
--light-code-background: #FFFFFF;
2323
--dark-code-background: #1E1E1E;
2424
}

‎docs/assets/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)