@@ -12,41 +12,40 @@ import type { UserRoleType } from "./User";
1212import type { Base } from "../entities/Base" ;
1313
1414// setup action and subject types for ability
15- export type ActionType =
16- | "create"
17- | "delete"
18- | "read"
19- | "update"
15+ export type ActionType = "create" | "read" | "update" | "delete" ;
16+ export type ExtendedActionType =
17+ | ActionType
2018 | "manage" // all CRUD ops
2119 | "edit" // all CRUD ops except delete
22- | "manipulate" ; // all CRUD ops except read
20+ | "manipulate" ; // all CRUD ops except read;
2321
2422export interface IModelKind < K extends string = string > {
2523 kind : K ;
2624}
2725
2826// define what an ability is using the action and subject types
29- export type Ability < Model extends IModelKind | Base > = MongoAbility <
30- [ ActionType , InferSubjects < Model > ] ,
31- MongoQuery < Omit < Model , "kind" > >
27+ export type Ability < M extends IModelKind , C extends typeof Base > = MongoAbility <
28+ [ ExtendedActionType , InferSubjects < M | C > ] ,
29+ MongoQuery < Omit < M , "kind" > >
3230> ;
3331
3432// define permissions; used to create an ability
35- export type DefinePermissions < Model extends IModelKind | Base > = (
33+ export type DefinePermissions < M extends IModelKind , C extends typeof Base > = (
3634 user : JwtPayload ,
37- builder : AbilityBuilder < Ability < Model > > ,
35+ builder : AbilityBuilder < Ability < M , C > > ,
3836) => void ;
3937
40- export type Permissions < Model extends IModelKind | Base > = Record <
38+ export type Permissions < M extends IModelKind , C extends typeof Base > = Record <
4139 UserRoleType ,
42- DefinePermissions < Model >
40+ DefinePermissions < M , C >
4341> ;
4442
4543// export a function for defining an ability using generics.
4644export const DefineAbility = function DefineAbility <
47- Model extends IModelKind | Base ,
48- > ( user : JwtPayload , permissions : Permissions < Model > ) : Ability < Model > {
49- const builder = new AbilityBuilder < Ability < Model > > ( createMongoAbility ) ;
45+ M extends IModelKind ,
46+ C extends typeof Base ,
47+ > ( user : JwtPayload , permissions : Permissions < M , C > ) : Ability < M , C > {
48+ const builder = new AbilityBuilder < Ability < M , C > > ( createMongoAbility ) ;
5049
5150 if ( typeof permissions [ user . role ] === "function" ) {
5251 permissions [ user . role ] ( user , builder ) ;
@@ -56,18 +55,12 @@ export const DefineAbility = function DefineAbility<
5655 ) ;
5756 }
5857
59- return builder . build ( {
60- resolveAction,
61- detectSubjectType : ( obj : Model ) => {
62- if ( isModel ( obj ) ) {
63- return obj . kind as ExtractSubjectType < Model > ;
64- } else {
65- return obj . constructor . name as ExtractSubjectType < Model > ;
66- }
67- } ,
68- } ) ;
58+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
59+ // @ts -ignore
60+ return builder . build ( { resolveAction, detectSubjectType } ) ;
6961} ;
7062
63+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7164function isModel ( obj : any ) : obj is IModelKind {
7265 return typeof obj ?. kind === "string" ;
7366}
@@ -77,3 +70,14 @@ const resolveAction = createAliasResolver({
7770 edit : [ "create" , "read" , "update" ] ,
7871 manipulate : [ "create" , "update" , "delete" ] ,
7972} ) ;
73+
74+ const detectSubjectType = function detectSubjectType <
75+ M extends IModelKind ,
76+ C extends typeof Base ,
77+ > ( subject : M | C ) {
78+ if ( isModel ( subject ) ) {
79+ return subject . kind as ExtractSubjectType < M | C > ;
80+ } else {
81+ return subject . constructor . name as ExtractSubjectType < M | C > ;
82+ }
83+ } ;
0 commit comments