-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.d.ts
194 lines (174 loc) · 5.57 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import * as sequelize from 'sequelize/types/lib/sequelize';
import { Model, ModelType, Sequelize } from 'sequelize';
import EventEmitter = NodeJS.EventEmitter;
import NodeCache from 'node-cache';
declare module 'sequelize/types/lib/sequelize' {
interface Sequelize {
guard: SequelizeGuard;
models: {
GuardModels
}
}
}
type Action = 'view' | 'create' | 'update' | 'delete' | '*' | 'approve';
interface SequelizeGuardOptions<TModel extends ModelType = typeof _GuardUser> {
prefix?: string;
primaryKey?: string;
timestamps?: boolean;
paranoid?: boolean;
sync?: boolean;
debug?: boolean;
UserModel?: TModel;
userPk?: string;
safeGuardDeletes?: boolean;
userCache?: boolean;
userCacheTtl?: number;
}
interface GuardUser {
assignRole(role: string): Promise<this>;
assignRoles(roles: string[]): Promise<RoleUser>;
rmAssignedRoles(roles: string[]): Promise<void>;
can(permission: string): Promise<boolean>;
cant(permission: string): Promise<boolean>;
isAllOf(roles: string[]): Promise<boolean>;
isAnyOf(roles: string[]): Promise<boolean>;
isA(role: string): Promise<boolean>;
isAn(role: string): Promise<boolean>;
roles(): Promise<GuardRole>;
}
/* ---------- Models ---------- */
declare class GuardResource extends Model<GuardResource> {
id: number;
name: string;
description: string;
}
declare class GuardPermission extends Model<GuardPermission> {
id: number;
name: string;
description: string;
resource: string;
action: string;
allow: number;
}
declare class GuardRole extends Model<GuardRole> {
id: number;
name: string;
description: string;
parent_id: number;
Permissions?: GuardPermission[];
RoleUser?: RoleUser;
}
declare class RolePermission extends Model<RolePermission> {
id: number;
role_id: number;
permission_id: number;
}
/**
* Default guard user that is used when no UserModel is provided in SequelizeGuard constructor's options.
*/
declare class _GuardUser extends Model<_GuardUser> {
id: number;
name: string;
email: string;
}
declare class RoleUser extends Model<RoleUser> {
id: number;
role_id: number;
user_id: any;
}
interface GuardModels {
GuardResource,
GuardRole,
GuardPermission,
RolePermission,
GuardUser,
RoleUser
}
declare class SequelizeGuard<TModel extends ModelType = ModelType> {
constructor(seql: Sequelize, options: SequelizeGuardOptions<TModel>);
models(): GuardModels;
init(): GuardControl;
allow(role: string, actions: Action[] | Action, resources: string[] | string): Promise<{
role: GuardRole,
permissions: GuardPermission[]
}>;
userIsA(user: TModel, role: string): Promise<boolean>;
createPerms(resources: string | string[], actions: Action | Action[], options?: {
names?: string[],
all?: boolean,
json?: false
}): Promise<GuardPermission[]>;
createPerms(resources: string | string[], actions: Action | Action[], options?: {
names?: string[],
all?: boolean,
json?: true
}): Promise<string[]>;
createPerms(resources: string | string[], actions: Action | Action[], options?: {
names?: string[],
all?: boolean,
json?: boolean
}): Promise<GuardPermission[]> | Promise<string[]>;
createPermsBulk(permissions: GuardPermission[], options: {
all?: boolean,
json?: false
}): Promise<GuardPermission[]>;
createPermsBulk(permissions: GuardPermission[], options: {
all?: boolean,
json?: true
}): Promise<string[]>;
createPermsBulk(permissions: GuardPermission[], options: {
all?: boolean,
json?: boolean
}): Promise<GuardPermission[]> | Promise<string[]>;
findPerms(args: Partial<GuardPermission & {search: boolean}>): Promise<GuardPermission>;
makeRole(role: string): Promise<{role: GuardRole, created: boolean}>;
makeRoles(roles: string | string[]): Promise<GuardRole[]>;
deleteRoles(roles: string | string[]): Promise<number>;
allRoles(): Promise<GuardRole[]>;
getRole(role: string): Promise<GuardRole>;
findRoles(args: {
name?: string,
names?: string[],
search?: boolean
}): Promise<GuardRole[]>;
addPermsToRole(role: string, actions: Action | Action[], resources: string | string[]): Promise<{
role: GuardRole,
permissions: GuardPermission[]
}>;
rmPermsFromRole(role: string, actions: Action | Action[], resources: string | string[]): Promise<{
role: GuardRole,
permissions: GuardPermission[],
permsRemoved: GuardPermission[]
}>
assignRole(user: TModel, role: string): Promise<TModel>;
assignRoles(user: TModel, roles: string[]): Promise<RoleUser>;
rmAssignedRoles(user: TModel, roles: string | string[]): Promise<void>;
onRolesCreated(cb: (...args: any[]) => void): () => EventEmitter;
onRolesDeleted(cb: (...args: any[]) => void): () => EventEmitter;
onPermsCreated(cb: (...args: any[]) => void): () => EventEmitter;
onPermsAddedToRole(cb: (...args: any[]) => void): () => EventEmitter;
onPermsRemovedFromRole(cb: (...args: any[]) => void): () => EventEmitter;
makeUser(user: Partial<TModel>): Promise<TModel>;
getUserRoles(user: TModel): Promise<GuardRole>;
resetCache(): NodeCache;
getCache(): Promise<NodeCache>;
resetUserCache(): NodeCache;
getUserCache(): NodeCache;
userHasRoles(user: T, roles: string): Promise<boolean>;
userHasAllRoles(user: T, roles: string): Promise<boolean>;
}
declare class GuardControl {
constructor(guard: SequelizeGuard);
allow(roles: string): this;
to(actions: Action[] | Action): this;
on(resources: string[] | string): this;
commit(): Promise<{
role: GuardRole,
permissions: GuardPermission[]
}>;
}
export default SequelizeGuard;
export type {
SequelizeGuardOptions,
GuardUser
}