You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -21,9 +21,8 @@ It provides three ways of integration
21
21
2.**Role based permissions** - Permissions are associated to roles and users have a specific role attached. This actually reduces redundancy in DB a lot, as most of the time, users will have many common permissions. If that is not the case for you, then, use method #1 above.
22
22
3.**Role based permissions with user level override** - This is the most flexible architecture. In this case, method #2 is implemented as is. On top of it, we also add user-level permissions override, allow/deny permissions over role permissions. So, say there is user who can perform all admin role actions except he cannot remove users from the system. So, DeleteUser permission can be denied at user level and role can be set as Admin for the user.
23
23
24
-
As a further enhancement to these methods, we are using [casbin library!](https://casbin.org/docs/en/overview) to define permissions at level of entity or resource associated with an API call. Casbin authorisation implementation can be performed in two ways:
25
-
1.**Using default casbin policy document** - Define policy document in default casbin format in the app, and configure authorise decorator to use those policies.
26
-
2.**Defining custom logic to form dynamic policies** - Implement dynamic permissions based on app logic in casbin-enforcer-config provider. Authorisation extension will dynamically create casbin policy using this business logic to give the authorisation decisions.
24
+
[Extension enhancement using CASBIN authorisation](#Extension-enhancement-using-CASBIN-authorisation)
25
+
27
26
28
27
Refer to the usage section below for details on integration
29
28
@@ -41,21 +40,13 @@ For a quick starter guide, you can refer to our [loopback 4 starter](https://git
41
40
42
41
In order to use this component into your LoopBack application, please follow below steps.
43
42
44
-
- Add component to application. Also add providers to implement casbin authorisation.
- If using method #1 from above, implement Permissions interface in User model and add permissions array.
@@ -128,7 +119,182 @@ export class User extends Entity implements UserPermissionsOverride<string> {
128
119
}
129
120
}
130
121
```
122
+
- For method #3, we also provide a simple provider function [_AuthorizationBindings.USER_PERMISSIONS_](<[./src/providers/user-permissions.provider.ts](https://github.com/sourcefuse/loopback4-authorization/blob/master/src/providers/user-permissions.provider.ts)>) to evaluate the user permissions based on its role permissions and user-level overrides. Just inject it
The above sequence also contains user authentication using [loopback4-authentication](https://github.com/sourcefuse/loopback4-authentication) package. You can refer to the documentation for the same for more details.
216
+
217
+
- Now we can add access permission keys to the controller methods using authorize
This endpoint will only be accessible if logged in user has permission
238
+
'CreateRole'.
239
+
240
+
A good practice is to keep all permission strings in a separate enum file like this.
241
+
242
+
```ts
243
+
exportconstenumPermissionKey {
244
+
ViewOwnUser='ViewOwnUser',
245
+
ViewAnyUser='ViewAnyUser',
246
+
ViewTenantUser='ViewTenantUser',
247
+
CreateAnyUser='CreateAnyUser',
248
+
CreateTenantUser='CreateTenantUser',
249
+
UpdateOwnUser='UpdateOwnUser',
250
+
UpdateTenantUser='UpdateTenantUser',
251
+
UpdateAnyUser='UpdateAnyUser',
252
+
DeleteTenantUser='DeleteTenantUser',
253
+
DeleteAnyUser='DeleteAnyUser',
254
+
255
+
ViewTenant='ViewTenant',
256
+
CreateTenant='CreateTenant',
257
+
UpdateTenant='UpdateTenant',
258
+
DeleteTenant='DeleteTenant',
259
+
260
+
ViewRole='ViewRole',
261
+
CreateRole='CreateRole',
262
+
UpdateRole='UpdateRole',
263
+
DeleteRole='DeleteRole',
264
+
265
+
ViewAudit='ViewAudit',
266
+
CreateAudit='CreateAudit',
267
+
UpdateAudit='UpdateAudit',
268
+
DeleteAudit='DeleteAudit',
269
+
}
270
+
```
271
+
272
+
# Extension enhancement using CASBIN authorisation
273
+
274
+
As a further enhancement to these methods, we are using [casbin library!](https://casbin.org/docs/en/overview) to define permissions at level of entity or resource associated with an API call. Casbin authorisation implementation can be performed in two ways:
275
+
1.**Using default casbin policy document** - Define policy document in default casbin format in the app, and configure authorise decorator to use those policies.
276
+
2.**Defining custom logic to form dynamic policies** - Implement dynamic permissions based on app logic in casbin-enforcer-config provider. Authorisation extension will dynamically create casbin policy using this business logic to give the authorisation decisions.
277
+
278
+
## Usage
279
+
280
+
In order to use this enhacement into your LoopBack application, please follow below steps.
281
+
282
+
- Add providers to implement casbin authorisation along with authorisation component.
- Implement the **Casbin Resource value modifier provider**. Customise the resource value based on business logic using route arguments parameter in the provider.
133
299
134
300
```ts
@@ -203,29 +369,14 @@ export class CasbinEnforcerConfigProvider
203
369
}
204
370
```
205
371
206
-
207
-
- For method #3, we also provide a simple provider function [_AuthorizationBindings.USER_PERMISSIONS_](<[./src/providers/user-permissions.provider.ts](https://github.com/sourcefuse/loopback4-authorization/blob/master/src/providers/user-permissions.provider.ts)>) to evaluate the user permissions based on its role permissions and user-level overrides. Just inject it
- Add a step in custom sequence to check for authorization whenever any end
231
382
point is hit.
@@ -306,8 +457,6 @@ export class MySequence implements SequenceHandler {
306
457
}
307
458
```
308
459
309
-
The above sequence also contains user authentication using [loopback4-authentication](https://github.com/sourcefuse/loopback4-authentication) package. You can refer to the documentation for the same for more details.
310
-
311
460
- Now we can add access permission keys to the controller methods using authorize
312
461
decorator as below. Set isCasbinPolicy parameter to use casbin default policy format. Default is false.
This endpoint will only be accessible if logged in user has permission
332
-
'CreateRole'.
333
-
334
-
A good practice is to keep all permission strings in a separate enum file like this.
335
-
336
-
```ts
337
-
exportconstenumPermissionKey {
338
-
ViewOwnUser='ViewOwnUser',
339
-
ViewAnyUser='ViewAnyUser',
340
-
ViewTenantUser='ViewTenantUser',
341
-
CreateAnyUser='CreateAnyUser',
342
-
CreateTenantUser='CreateTenantUser',
343
-
UpdateOwnUser='UpdateOwnUser',
344
-
UpdateTenantUser='UpdateTenantUser',
345
-
UpdateAnyUser='UpdateAnyUser',
346
-
DeleteTenantUser='DeleteTenantUser',
347
-
DeleteAnyUser='DeleteAnyUser',
348
-
349
-
ViewTenant='ViewTenant',
350
-
CreateTenant='CreateTenant',
351
-
UpdateTenant='UpdateTenant',
352
-
DeleteTenant='DeleteTenant',
353
-
354
-
ViewRole='ViewRole',
355
-
CreateRole='CreateRole',
356
-
UpdateRole='UpdateRole',
357
-
DeleteRole='DeleteRole',
358
-
359
-
ViewAudit='ViewAudit',
360
-
CreateAudit='CreateAudit',
361
-
UpdateAudit='UpdateAudit',
362
-
DeleteAudit='DeleteAudit',
363
-
}
364
-
```
365
-
366
480
## Feedback
367
481
368
482
If you've noticed a bug or have a question or have a feature request, [search the issue tracker](https://github.com/sourcefuse/loopback4-authorization/issues) to see if someone else in the community has already created a ticket.
0 commit comments