@holoyan/adonisjs-permissions 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,6 +12,11 @@ Checkout other AdonisJS packages
12
12
  - It's simple, just star this repository, that is enough to keep me motivated to maintain this package.
13
13
 
14
14
 
15
+ ## Release Notes
16
+
17
+ Version: >= v1.2.0
18
+ * Added [Morph Map](https://github.com/holoyan/morph-map-js) as a dependency to handle polymorphic relations - fixed [#20](https://github.com/holoyan/adonisjs-permissions/issues/20)
19
+
15
20
  ## Table of Contents
16
21
 
17
22
  <details><summary>Click to expand</summary><p>
@@ -49,7 +54,7 @@ Checkout other AdonisJS packages
49
54
  - [The Scope middleware](#the-scope-middleware)
50
55
  - [Default Scope](#default-scope-tenant)
51
56
  - [Transactions](#transactions)
52
- - [Events] (#events)
57
+ - [Events](#events)
53
58
  - [Cheat sheet](#cheat-sheet)
54
59
  - [Todo](#todo)
55
60
  - [Test](#test)
@@ -105,7 +110,7 @@ Example.
105
110
  ```typescript
106
111
 
107
112
  import { BaseModel, column } from '@adonisjs/lucid/orm'
108
- import { MorphMap } from '@holoyan/adonisjs-permissions'
113
+ import { MorphMap } from '@holoyan/morph-map-js' // (NOTE upper case `MorphMap`)
109
114
  import { AclModelInterface } from '@holoyan/adonisjs-permissions/types'
110
115
 
111
116
  @MorphMap('users')
@@ -136,11 +141,6 @@ export default class Post extends BaseModel implements AclModelInterface {
136
141
 
137
142
  ```
138
143
 
139
- ## Release Notes
140
-
141
- Version: >= v1.1.0
142
- * Added support for [Events](#events)
143
-
144
144
  ## Mixins
145
145
 
146
146
  If you want to be able to call `Acl` methods on a `User` model then consider using `hasPermissions` mixin
@@ -148,7 +148,7 @@ If you want to be able to call `Acl` methods on a `User` model then consider usi
148
148
  ```typescript
149
149
 
150
150
  import { BaseModel, column } from '@adonisjs/lucid/orm'
151
- import { MorphMap } from '@holoyan/adonisjs-permissions'
151
+ import { MorphMap } from '@holoyan/morph-map-js' // (NOTE upper case `MorphMap`)
152
152
  import { AclModelInterface } from '@holoyan/adonisjs-permissions/types'
153
153
 
154
154
  // import mixin
@@ -1011,6 +1011,42 @@ await Acl.model(myUser).allow('create')
1011
1011
 
1012
1012
  ```
1013
1013
 
1014
+
1015
+ You can also disable events per query by calling withoutEvents() method
1016
+
1017
+ ```typescript
1018
+
1019
+ emitter.on(RoleCreatedEvent, () => {
1020
+ console.log('Role created') // this will not be called
1021
+ })
1022
+
1023
+ //
1024
+ await Acl.role().withoutEvents().create({ // this one will not trigger RoleCreatedEvent
1025
+ slug: 'admin',
1026
+ })
1027
+
1028
+ await Acl.role().create({ // this will trigger
1029
+ slug: 'admin',
1030
+ })
1031
+
1032
+ ```
1033
+
1034
+ If you want to completely disable events, you can do that by calling `withoutEvents()` method on the `Acl` class
1035
+
1036
+ ```typescript
1037
+
1038
+ Acl.withoutEvents()
1039
+
1040
+ // any method called on the Acl will not trigger any events
1041
+ emitter.on(RoleCreatedEvent, () => {
1042
+ console.log('Role created') // this will not be called
1043
+ })
1044
+
1045
+ await Acl.role().create({ // this will not trigger because globaly withoutEvents() is set
1046
+ slug: 'admin',
1047
+ })
1048
+ ```
1049
+
1014
1050
  List of events you can listen to:
1015
1051
 
1016
1052
  ```
@@ -1251,7 +1287,7 @@ await Acl.permission(myPermission).detachFromRole(role_slug)
1251
1287
 
1252
1288
  | AdonisJS Lucid version | Package version |
1253
1289
  |------------------------|-----------------|
1254
- | v20.x | 0.9.x |
1290
+ | v20.x | 0.10.x |
1255
1291
  | v21.x | 1.x |
1256
1292
 
1257
1293
 
package/build/index.d.ts CHANGED
@@ -10,6 +10,5 @@ export { configure } from './configure.js';
10
10
  export { stubsRoot } from './stubs/main.js';
11
11
  export { AclManager, Acl } from './src/acl.js';
12
12
  export { MorphMap, getClassPath } from './src/decorators.js';
13
- export * as morphMapModel from './src/morph_map.js';
14
13
  export { hasPermissions } from './src/mixins/has_permissions.js';
15
14
  export { Scope } from './src/scope.js';
package/build/index.js CHANGED
@@ -18,6 +18,5 @@ export { configure } from './configure.js';
18
18
  export { stubsRoot } from './stubs/main.js';
19
19
  export { AclManager, Acl } from './src/acl.js';
20
20
  export { MorphMap, getClassPath } from './src/decorators.js';
21
- export * as morphMapModel from './src/morph_map.js';
22
21
  export { hasPermissions } from './src/mixins/has_permissions.js';
23
22
  export { Scope } from './src/scope.js';
@@ -1,9 +1,9 @@
1
1
  import { ApplicationService } from '@adonisjs/core/types';
2
2
  import ModelManager from '../src/model_manager.js';
3
- import MorphMap from '../src/morph_map.js';
3
+ import { MorphMapManager } from '@holoyan/morph-map-js';
4
4
  declare module '@adonisjs/core/types' {
5
5
  interface ContainerBindings {
6
- morphMap: MorphMap;
6
+ morphMap: MorphMapManager;
7
7
  modelManager: ModelManager;
8
8
  }
9
9
  }
@@ -4,8 +4,8 @@ import ModelPermission from '../src/models/model_permission.js';
4
4
  import ModelRole from '../src/models/model_role.js';
5
5
  import ModelManager from '../src/model_manager.js';
6
6
  import { AclManager } from '../src/acl.js';
7
- import MorphMap from '../src/morph_map.js';
8
7
  import { Scope } from '../src/scope.js';
8
+ import { morphMap } from '@holoyan/morph-map-js';
9
9
  export default class RolePermissionProvider {
10
10
  app;
11
11
  constructor(app) {
@@ -13,7 +13,7 @@ export default class RolePermissionProvider {
13
13
  }
14
14
  register() {
15
15
  this.app.container.singleton('morphMap', async () => {
16
- return new MorphMap();
16
+ return morphMap;
17
17
  });
18
18
  this.app.container.singleton('modelManager', async () => {
19
19
  return new ModelManager();
@@ -39,8 +39,8 @@ export default class RolePermissionProvider {
39
39
  AclManager.setModelManager(modelManager);
40
40
  AclManager.setEmitter(emitter);
41
41
  const map = await this.app.container.make('morphMap');
42
- map.set('permissions', Permission);
43
- map.set('roles', Role);
42
+ // map.set('permissions', Permission)
43
+ // map.set('roles', Role)
44
44
  AclManager.setMorphMap(map);
45
45
  }
46
46
  }
@@ -1,19 +1,11 @@
1
- import app from '@adonisjs/core/services/app';
1
+ import { morphMap, getClassPath as baseGetClassPath } from '@holoyan/morph-map-js';
2
+ // keep for backward compatibility
2
3
  export function MorphMap(param) {
3
4
  return function (target) {
4
- const service = async function () {
5
- var result = await app.container.make('morphMap');
6
- result.set(param, target);
7
- return param;
8
- };
9
- target.prototype.__morphMapName = service();
5
+ morphMap.set(param, target);
10
6
  target.prototype.__morphMapName = param;
11
7
  };
12
8
  }
13
9
  export function getClassPath(clazz) {
14
- const morphMapName = clazz.prototype.__morphMapName;
15
- if (!morphMapName) {
16
- throw new Error('morph map name not specified');
17
- }
18
- return morphMapName;
10
+ return baseGetClassPath(clazz);
19
11
  }
@@ -246,8 +246,10 @@ export declare function hasPermissions(): <Model extends NormalizeConstructor<ty
246
246
  isDirty(fields?: undefined[] | undefined): boolean;
247
247
  enableForceUpdate(): /*elided*/ any;
248
248
  save(): Promise</*elided*/ any>;
249
+ saveQuietly(): Promise</*elided*/ any>;
249
250
  lockForUpdate<T>(callback: (user: /*elided*/ any) => T | Promise<T>): Promise<T>;
250
251
  delete(): Promise<void>;
252
+ deleteQuietly(): Promise<void>;
251
253
  refresh(): Promise</*elided*/ any>;
252
254
  load: import("@adonisjs/lucid/types/model").LucidRowPreload</*elided*/ any>;
253
255
  loadOnce: import("@adonisjs/lucid/types/model").LucidRowPreloadOnce</*elided*/ any>;
@@ -315,7 +317,9 @@ export declare function hasPermissions(): <Model extends NormalizeConstructor<ty
315
317
  <Model_1 extends import("@adonisjs/lucid/types/model").LucidModel, Event_2 extends import("@adonisjs/lucid/types/model").EventsList>(this: Model_1, event: Event_2, handler: import("@adonisjs/lucid/types/model").HooksHandler<InstanceType<Model_1>, Event_2>): void;
316
318
  };
317
319
  create: <T extends import("@adonisjs/lucid/types/model").LucidModel>(this: T, values: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T>>>, options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T>>;
320
+ createQuietly: <T extends import("@adonisjs/lucid/types/model").LucidModel>(this: T, values: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T>>>, options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T>>;
318
321
  createMany: <T extends import("@adonisjs/lucid/types/model").LucidModel>(this: T, values: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T>>>[], options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T>[]>;
322
+ createManyQuietly: <T extends import("@adonisjs/lucid/types/model").LucidModel>(this: T, values: Partial<import("@adonisjs/lucid/types/model").ModelAttributes<InstanceType<T>>>[], options?: import("@adonisjs/lucid/types/model").ModelAssignOptions) => Promise<InstanceType<T>[]>;
319
323
  findOrFail: <T extends import("@adonisjs/lucid/types/model").LucidModel>(this: T, value: any, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions) => Promise<InstanceType<T>>;
320
324
  findBy: {
321
325
  <T extends import("@adonisjs/lucid/types/model").LucidModel>(this: T, clause: Record<string, unknown>, options?: import("@adonisjs/lucid/types/model").ModelAdapterOptions): Promise<null | InstanceType<T>>;
@@ -6,7 +6,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
6
6
  };
7
7
  import { v4 as uuidv4 } from 'uuid';
8
8
  import { BaseModel, beforeCreate, column } from '@adonisjs/lucid/orm';
9
- export default class Permission extends BaseModel {
9
+ import { MorphMap } from '@holoyan/morph-map-js';
10
+ let Permission = class Permission extends BaseModel {
10
11
  static uuidSupport = false;
11
12
  static assignUuid(permission) {
12
13
  if (this.uuidSupport) {
@@ -16,7 +17,7 @@ export default class Permission extends BaseModel {
16
17
  getModelId() {
17
18
  return this.id;
18
19
  }
19
- }
20
+ };
20
21
  __decorate([
21
22
  column({ isPrimary: true })
22
23
  ], Permission.prototype, "id", void 0);
@@ -47,3 +48,7 @@ __decorate([
47
48
  __decorate([
48
49
  beforeCreate()
49
50
  ], Permission, "assignUuid", null);
51
+ Permission = __decorate([
52
+ MorphMap('permissions')
53
+ ], Permission);
54
+ export default Permission;
@@ -6,7 +6,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
6
6
  };
7
7
  import { BaseModel, beforeCreate, column } from '@adonisjs/lucid/orm';
8
8
  import { v4 as uuidv4 } from 'uuid';
9
- export default class Role extends BaseModel {
9
+ import { MorphMap } from '@holoyan/morph-map-js';
10
+ let Role = class Role extends BaseModel {
10
11
  static uuidSupport = false;
11
12
  static assignUuid(role) {
12
13
  if (this.uuidSupport) {
@@ -16,7 +17,7 @@ export default class Role extends BaseModel {
16
17
  getModelId() {
17
18
  return this.id;
18
19
  }
19
- }
20
+ };
20
21
  __decorate([
21
22
  column({ isPrimary: true })
22
23
  ], Role.prototype, "id", void 0);
@@ -47,3 +48,7 @@ __decorate([
47
48
  __decorate([
48
49
  beforeCreate()
49
50
  ], Role, "assignUuid", null);
51
+ Role = __decorate([
52
+ MorphMap('roles')
53
+ ], Role);
54
+ export default Role;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@holoyan/adonisjs-permissions",
3
- "description": "Adonisjs roles and permissions system",
4
- "version": "1.1.0",
3
+ "description": "AdonisJs roles and permissions system",
4
+ "version": "1.2.0",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },
@@ -72,9 +72,9 @@
72
72
  "peerDependencies": {
73
73
  "@adonisjs/core": "^6.2.0",
74
74
  "@adonisjs/lucid": "^21.0.0",
75
+ "@types/uuid": "^10.0.0",
75
76
  "luxon": "^3.4.4",
76
- "uuid": "^10.0.0",
77
- "@types/uuid": "^10.0.0"
77
+ "uuid": "^10.0.0"
78
78
  },
79
79
  "publishConfig": {
80
80
  "access": "public",
@@ -98,5 +98,8 @@
98
98
  "eslintConfig": {
99
99
  "extends": "@adonisjs/eslint-config/package"
100
100
  },
101
- "prettier": "@adonisjs/prettier-config"
101
+ "prettier": "@adonisjs/prettier-config",
102
+ "dependencies": {
103
+ "@holoyan/morph-map-js": "^0.1.1"
104
+ }
102
105
  }
@@ -1,11 +0,0 @@
1
- import { MorphInterface } from './types.js';
2
- export default class MorphMap implements MorphInterface {
3
- private _map;
4
- private static _instance?;
5
- static create(): MorphMap;
6
- set(alias: string, target: any): void;
7
- get(alias: string): any;
8
- has(alias: string): boolean;
9
- hasTarget(target: any): boolean;
10
- getAlias(target: any): string;
11
- }
@@ -1,40 +0,0 @@
1
- export default class MorphMap {
2
- _map = {};
3
- static _instance;
4
- static create() {
5
- if (this._instance) {
6
- return this._instance;
7
- }
8
- return new MorphMap();
9
- }
10
- set(alias, target) {
11
- this._map[alias] = target;
12
- }
13
- get(alias) {
14
- if (!(alias in this._map)) {
15
- throw new Error('morph map not found for ' + alias);
16
- }
17
- return this._map[alias] || null;
18
- }
19
- has(alias) {
20
- return alias in this._map;
21
- }
22
- hasTarget(target) {
23
- const keys = Object.keys(this._map);
24
- for (const key of keys) {
25
- if (this._map[key] === target) {
26
- return true;
27
- }
28
- }
29
- return false;
30
- }
31
- getAlias(target) {
32
- const keys = Object.keys(this._map);
33
- for (const key of keys) {
34
- if (target instanceof this._map[key] || target === this._map[key]) {
35
- return key;
36
- }
37
- }
38
- throw new Error('Target not found');
39
- }
40
- }