Skip to content

Commit

Permalink
fix(rbac): handle code review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Andriienko <[email protected]>
  • Loading branch information
AndrienkoAleksandr committed Jul 4, 2024
1 parent b25543c commit 77edf3a
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions plugins/rbac-backend/src/service/enforcer-delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export class EnforcerDelegate {
): Promise<void> {
const trx = externalTrx || (await this.knex.transaction());

if (policies.length === 0) {
return;
}

try {
const ok = await this.enforcer.addPolicies(policies);
if (!ok) {
Expand Down Expand Up @@ -156,18 +160,10 @@ export class EnforcerDelegate {
): Promise<void> {
const trx = externalTrx ?? (await this.knex.transaction());

const policiesToAdd: string[][] = await policies.reduce<
Promise<string[][]>
>(async (accP: Promise<string[][]>, gp: string[]) => {
const acc = await accP;
const hasPolicy = await this.enforcer.hasGroupingPolicy(...gp);
if (!hasPolicy) acc.push(gp);
return acc;
}, Promise.resolve([]));

if (policiesToAdd.length === 0) {
if (policies.length === 0) {
return;
}

try {
const currentRoleMetadata =
await this.roleMetadataStorage.findRoleMetadata(
Expand All @@ -187,10 +183,10 @@ export class EnforcerDelegate {
await this.roleMetadataStorage.createRoleMetadata(roleMetadata, trx);
}

const ok = await this.enforcer.addGroupingPolicies(policiesToAdd);
const ok = await this.enforcer.addGroupingPolicies(policies);
if (!ok) {
throw new Error(
`Failed to store policies ${policiesToString(policiesToAdd)}`,
`Failed to store policies ${policiesToString(policies)}`,
);
}

Expand Down

0 comments on commit 77edf3a

Please sign in to comment.