Skip to content

Commit e9f4d38

Browse files
committed
improvement: convert all management_api to async function
BREAKING CHANGE: see #
1 parent 516841f commit e9f4d38

File tree

4 files changed

+94
-89
lines changed

4 files changed

+94
-89
lines changed

src/enforcer.ts

+28-26
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ export class Enforcer extends ManagementEnforcer {
8484
*/
8585
public async getRolesForUser(name: string, domain?: string): Promise<string[]> {
8686
if (domain == null) {
87-
return await this.rm.getRoles(name);
87+
return this.rm.getRoles(name);
8888
} else {
89-
return await this.rm.getRoles(name, domain);
89+
return this.rm.getRoles(name, domain);
9090
}
9191
}
9292

@@ -99,9 +99,9 @@ export class Enforcer extends ManagementEnforcer {
9999
*/
100100
public async getUsersForRole(name: string, domain?: string): Promise<string[]> {
101101
if (domain == null) {
102-
return await this.rm.getUsers(name);
102+
return this.rm.getUsers(name);
103103
} else {
104-
return await this.rm.getUsers(name, domain);
104+
return this.rm.getUsers(name, domain);
105105
}
106106
}
107107

@@ -137,9 +137,9 @@ export class Enforcer extends ManagementEnforcer {
137137
*/
138138
public async addRoleForUser(user: string, role: string, domain?: string): Promise<boolean> {
139139
if (domain == null) {
140-
return await this.addGroupingPolicy(user, role);
140+
return this.addGroupingPolicy(user, role);
141141
} else {
142-
return await this.addGroupingPolicy(user, role, domain);
142+
return this.addGroupingPolicy(user, role, domain);
143143
}
144144
}
145145

@@ -154,9 +154,9 @@ export class Enforcer extends ManagementEnforcer {
154154
*/
155155
public async deleteRoleForUser(user: string, role: string, domain?: string): Promise<boolean> {
156156
if (domain == null) {
157-
return await this.removeGroupingPolicy(user, role);
157+
return this.removeGroupingPolicy(user, role);
158158
} else {
159-
return await this.removeGroupingPolicy(user, role, domain);
159+
return this.removeGroupingPolicy(user, role, domain);
160160
}
161161
}
162162

@@ -170,9 +170,9 @@ export class Enforcer extends ManagementEnforcer {
170170
*/
171171
public async deleteRolesForUser(user: string, domain?: string): Promise<boolean> {
172172
if (domain == null) {
173-
return await this.removeFilteredGroupingPolicy(0, user);
173+
return this.removeFilteredGroupingPolicy(0, user);
174174
} else {
175-
return await this.removeFilteredGroupingPolicy(0, user, '', domain);
175+
return this.removeFilteredGroupingPolicy(0, user, '', domain);
176176
}
177177
}
178178

@@ -184,7 +184,7 @@ export class Enforcer extends ManagementEnforcer {
184184
* @return succeeds or not.
185185
*/
186186
public async deleteUser(user: string): Promise<boolean> {
187-
return await this.removeFilteredGroupingPolicy(0, user);
187+
return this.removeFilteredGroupingPolicy(0, user);
188188
}
189189

190190
/**
@@ -207,7 +207,7 @@ export class Enforcer extends ManagementEnforcer {
207207
* @return succeeds or not.
208208
*/
209209
public async deletePermission(...permission: string[]): Promise<boolean> {
210-
return await this.removeFilteredPolicy(1, ...permission);
210+
return this.removeFilteredPolicy(1, ...permission);
211211
}
212212

213213
/**
@@ -220,7 +220,7 @@ export class Enforcer extends ManagementEnforcer {
220220
*/
221221
public async addPermissionForUser(user: string, ...permission: string[]): Promise<boolean> {
222222
permission.unshift(user);
223-
return await this.addPolicy(...permission);
223+
return this.addPolicy(...permission);
224224
}
225225

226226
/**
@@ -233,7 +233,7 @@ export class Enforcer extends ManagementEnforcer {
233233
*/
234234
public async deletePermissionForUser(user: string, ...permission: string[]): Promise<boolean> {
235235
permission.unshift(user);
236-
return await this.removePolicy(...permission);
236+
return this.removePolicy(...permission);
237237
}
238238

239239
/**
@@ -244,7 +244,7 @@ export class Enforcer extends ManagementEnforcer {
244244
* @return succeeds or not.
245245
*/
246246
public async deletePermissionsForUser(user: string): Promise<boolean> {
247-
return await this.removeFilteredPolicy(0, user);
247+
return this.removeFilteredPolicy(0, user);
248248
}
249249

250250
/**
@@ -253,7 +253,7 @@ export class Enforcer extends ManagementEnforcer {
253253
* @param user the user.
254254
* @return the permissions, a permission is usually like (obj, act). It is actually the rule without the subject.
255255
*/
256-
public getPermissionsForUser(user: string): string[][] {
256+
public async getPermissionsForUser(user: string): Promise<string[][]> {
257257
return this.getFilteredPolicy(0, user);
258258
}
259259

@@ -264,7 +264,7 @@ export class Enforcer extends ManagementEnforcer {
264264
* @param permission the permission, usually be (obj, act). It is actually the rule without the subject.
265265
* @return whether the user has the permission.
266266
*/
267-
public hasPermissionForUser(user: string, ...permission: string[]): boolean {
267+
public async hasPermissionForUser(user: string, ...permission: string[]): Promise<boolean> {
268268
permission.unshift(user);
269269
return this.hasPolicy(...permission);
270270
}
@@ -283,11 +283,11 @@ export class Enforcer extends ManagementEnforcer {
283283
const res: string[] = [];
284284
const roles = await this.rm.getRoles(name, ...domain);
285285
res.push(...roles);
286-
await Promise.all(
287-
roles.map(async n => {
288-
res.push(...(await this.getImplicitRolesForUser(n, ...domain)));
289-
})
290-
);
286+
287+
for (const n of roles) {
288+
res.push(...(await this.getImplicitRolesForUser(n, ...domain)));
289+
}
290+
291291
return res;
292292
}
293293

@@ -306,13 +306,15 @@ export class Enforcer extends ManagementEnforcer {
306306
const roles = [user, ...(await this.getImplicitRolesForUser(user, ...domain))];
307307
const res: string[][] = [];
308308
const withDomain = domain && domain.length !== 0;
309-
roles.forEach(n => {
309+
310+
for (const n of roles) {
310311
if (withDomain) {
311-
res.push(...this.getFilteredPolicy(0, n, ...domain));
312+
res.push(...(await this.getFilteredPolicy(0, n, ...domain)));
312313
} else {
313-
res.push(...this.getPermissionsForUser(n));
314+
res.push(...(await this.getPermissionsForUser(n)));
314315
}
315-
});
316+
}
317+
316318
return res;
317319
}
318320
}

0 commit comments

Comments
 (0)