loopback-acl-extension
v6.4.0
Published
Loopback ACL generic implementation based on RBAC
Downloads
3
Readme
loopback-acl-extension
Creating User
, Role
, Permission
models and repositories and crud controllers in any application is a repetitive and futile task.
Using this extension you can bind them to your application using a simple and optional configurations.
Installation
npm i --save loopback-history-extension
npm i --save loopback-authorization-extension
npm i --save loopback-acl-extension
Usage
Follow these steps to add acl
extension to your loopback4 application
- Define your Relational and Cache
dataSources
- Add
ACLMixin
to your application - Bind
ACLRestServer
Now, let's try:
Step 1 (Define DataSource)
Bind your dataSources you want to use for acl tables using bindRelationalDataSource
and bindCacheDataSource
We need two dataSource, one for relational models, and one for cache models
- Relational Models:
CRUD
User
Role
Permission
UserRole
RolePermission
- Cache Models:
Key-Value
Session
Code
See this example of binding relational dataSource:
import { bindRelationalDataSource } from "loopback-authorization-extension";
@bindRelationalDataSource()
export class MySqlDataSource extends juggler.DataSource {
static dataSourceName = "MySQL";
constructor(
@inject("datasources.config.MySQL", { optional: true })
dsConfig: object = config
) {
super(dsConfig);
}
}
See this example of binding cache dataSource:
import { bindCacheDataSource } from "loopback-acl-extension";
@bindCacheDataSource()
export class RedisDataSource extends juggler.DataSource {
static dataSourceName = "Redis";
constructor(
@inject("datasources.config.Redis", { optional: true })
dsConfig: object = config
) {
super(dsConfig);
}
}
Step 2,3 (Application Mixin)
Edit your application.ts
file:
import { AuthorizationMixin } from "loopback-authorization-extension";
import { ACLMixin, ACLRestServer, ACLGQLServer } from "loopback-acl-extension";
export class TestApplication extends AuthorizationMixin(
ACLMixin(BootMixin(ServiceMixin(RepositoryMixin(Application))))
) {
constructor(options: ApplicationConfig = {}) {
super(options);
// ...
// Bind servers
this.server(ACLRestServer);
this.server(ACLGQLServer);
}
}
Contributions
License
This project is licensed under the MIT license.
Copyright (c) KoLiBer ([email protected])