Skip to content

Commit ccaa770

Browse files
refactor(chore): fix sonar code smells (#117)
fix sonar code smells related to cognitive complexity, empty constructor and method types GH-116
1 parent 260a995 commit ccaa770

File tree

4 files changed

+48
-53
lines changed

4 files changed

+48
-53
lines changed

src/providers/casbin-authorization-action.provider.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {Getter, inject, Provider} from '@loopback/core';
22
import {Request} from '@loopback/express';
33
import {HttpErrors} from '@loopback/rest';
44
import * as casbin from 'casbin';
5-
65
import {AuthorizationBindings} from '../keys';
76
import {
87
AuthorizationMetadata,
@@ -63,15 +62,7 @@ export class CasbinAuthorizationProvider
6362
}
6463
const subject = this.getUserName(`${user.id}`);
6564

66-
let desiredPermissions;
67-
68-
if (metadata.permissions && metadata.permissions.length > 0) {
69-
desiredPermissions = metadata.permissions;
70-
} else {
71-
throw new HttpErrors.Unauthorized(
72-
`Permissions are missing in the decorator.`,
73-
);
74-
}
65+
const desiredPermissions = this.getDesiredPermissions(metadata);
7566

7667
// Fetch casbin config by invoking casbin-config-getter-provider
7768
const casbinConfig = await this.getCasbinEnforcerConfig(
@@ -90,7 +81,7 @@ export class CasbinAuthorizationProvider
9081
);
9182
}
9283
// In case casbin policy is coming via provider, use that to initialise enforcer
93-
else if (!metadata.isCasbinPolicy && casbinConfig.allowedRes) {
84+
else if (casbinConfig.allowedRes) {
9485
const policy = this.createCasbinPolicy(
9586
casbinConfig.allowedRes,
9687
subject,
@@ -124,6 +115,16 @@ export class CasbinAuthorizationProvider
124115
return `u${id}`;
125116
}
126117

118+
getDesiredPermissions(metadata: AuthorizationMetadata): Array<string> {
119+
if (metadata.permissions && metadata.permissions.length > 0) {
120+
return metadata.permissions;
121+
} else {
122+
throw new HttpErrors.Unauthorized(
123+
`Permissions are missing in the decorator.`,
124+
);
125+
}
126+
}
127+
127128
// Create casbin policy for user based on ResourcePermission data provided by extension client
128129
createCasbinPolicy(
129130
resPermObj: ResourcePermissionObject[],

src/providers/casbin-enforcer-config.provider.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import {Provider} from '@loopback/context';
22

3-
import {CasbinEnforcerConfigGetterFn, IAuthUserWithPermissions} from '../types';
43
import {HttpErrors} from '@loopback/rest';
4+
import {CasbinEnforcerConfigGetterFn, IAuthUserWithPermissions} from '../types';
55

66
export class CasbinEnforcerProvider
77
implements Provider<CasbinEnforcerConfigGetterFn>
88
{
9-
constructor() {
10-
//This is intentional
11-
}
12-
139
value(): CasbinEnforcerConfigGetterFn {
1410
return async (
1511
authUser: IAuthUserWithPermissions,

src/providers/user-permissions.provider.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import {UserPermission, UserPermissionsFn} from '../types';
55
export class UserPermissionsProvider
66
implements Provider<UserPermissionsFn<string>>
77
{
8-
constructor() {
9-
//This is intentional
10-
}
11-
128
value(): UserPermissionsFn<string> {
139
return (userPermissions, rolePermissions) =>
1410
this.action(userPermissions, rolePermissions);

src/types.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@ import {FileAdapter, Model} from 'casbin';
33
import PostgresAdapter from 'casbin-pg-adapter';
44

55
/**
6-
* Authorize action method interface
6+
* Authorize action method type
7+
*
8+
* @param userPermissions - Array of permission keys granted to the user
9+
* This is actually a union of permissions picked up based on role
10+
* attached to the user and allowed permissions at specific user level
711
*/
8-
export interface AuthorizeFn {
9-
// userPermissions - Array of permission keys granted to the user
10-
// This is actually a union of permissions picked up based on role
11-
// attached to the user and allowed permissions at specific user level
12-
(userPermissions: string[], request?: Request): Promise<boolean>;
13-
}
12+
export type AuthorizeFn = (
13+
userPermissions: string[],
14+
request?: Request,
15+
) => Promise<boolean>;
1416

1517
/**
16-
* Casbin authorize action method interface
18+
* Casbin authorize action method type
19+
* @param user - User object corresponding to the logged in user
20+
* @param resVal - value of the resource for which authorisation is being sought
21+
*
1722
*/
18-
export interface CasbinAuthorizeFn {
19-
// user - User object corresponding to the logged in user
20-
// resVal - value of the resource for which authorisation is being sought
21-
(
22-
user: IAuthUserWithPermissions,
23-
resVal: string,
24-
request: Request,
25-
): Promise<boolean>;
26-
}
23+
export type CasbinAuthorizeFn = (
24+
user: IAuthUserWithPermissions,
25+
resVal: string,
26+
request: Request,
27+
) => Promise<boolean>;
28+
2729
export type PermissionObject = {
2830
[controller: string]: {
2931
[method: string]: string[];
@@ -82,40 +84,40 @@ export interface UserPermission<T> {
8284
}
8385

8486
/**
85-
* User permissions manipulation method interface.
87+
* User permissions manipulation method type.
8688
*
8789
* This is where we can add our business logic to read and
8890
* union permissions associated to user via role with
8991
* those associated directly to the user.
9092
*
9193
*/
92-
export interface UserPermissionsFn<T> {
93-
(userPermissions: UserPermission<T>[], rolePermissions: T[]): T[];
94-
}
94+
export type UserPermissionsFn<T> = (
95+
userPermissions: UserPermission<T>[],
96+
rolePermissions: T[],
97+
) => T[];
9598

9699
/**
97-
* Casbin enforcer getter method interface
100+
* Casbin enforcer getter method type
98101
*
99102
* This method provides the Casbin config
100103
* required to initialise a Casbin enforcer
101104
*/
102-
export interface CasbinEnforcerConfigGetterFn {
103-
(
104-
authUser: IAuthUserWithPermissions,
105-
resource: string,
106-
isCasbinPolicy?: boolean,
107-
): Promise<CasbinConfig>;
108-
}
105+
export type CasbinEnforcerConfigGetterFn = (
106+
authUser: IAuthUserWithPermissions,
107+
resource: string,
108+
isCasbinPolicy?: boolean,
109+
) => Promise<CasbinConfig>;
109110

110111
/**
111-
* Casbin resource value modifier method interface
112+
* Casbin resource value modifier method type
112113
*
113114
* This method can help modify the resource value
114115
* for integration with casbin, as per business logic
115116
*/
116-
export interface CasbinResourceModifierFn {
117-
(pathParams: string[], req: Request): Promise<string>;
118-
}
117+
export type CasbinResourceModifierFn = (
118+
pathParams: string[],
119+
req: Request,
120+
) => Promise<string>;
119121

120122
/**
121123
* Casbin config object

0 commit comments

Comments
 (0)