@hmcts/opal-frontend-common 0.0.23 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,13 +18,37 @@ class PermissionsService {
18
18
  }
19
19
  return this.storedUniquePermissionIds;
20
20
  }
21
- hasPermissionAccess(permissionId, businessUnitId, roles) {
21
+ /**
22
+ * Determines whether the user has access to a specific permission based on their roles.
23
+ *
24
+ * This method short-circuits and returns as soon as a matching permission is found for efficiency.
25
+ *
26
+ * @param permissionId - The ID of the permission to check for access.
27
+ * @param roles - An array of user roles, each containing a list of permissions.
28
+ * @returns `true` if the user has the specified permission in any of their roles,
29
+ * or if no roles are provided; otherwise, returns `false`.
30
+ */
31
+ hasPermissionAccess(permissionId, roles) {
32
+ if (roles?.length) {
33
+ return roles.some((role) => role.permissions.some((permission) => permission.permission_id === permissionId));
34
+ }
35
+ // if we don't have any roles, we can't have any permissions
36
+ return true;
37
+ }
38
+ /**
39
+ * Checks if the user has access to a specific permission within a given business unit.
40
+ *
41
+ * This method short-circuits and returns as soon as a matching permission is found for efficiency.
42
+ *
43
+ * @param permissionId - The ID of the permission to check.
44
+ * @param businessUnitId - The ID of the business unit to check against.
45
+ * @param roles - An array of user roles, each containing permissions and associated business unit IDs.
46
+ * @returns `true` if the user has the specified permission for the business unit, or if no roles are provided; otherwise, `false`.
47
+ */
48
+ hasBusinessUnitPermissionAccess(permissionId, businessUnitId, roles) {
22
49
  if (roles?.length) {
23
- // First we need to find the matching role
24
- const role = roles?.find((role) => role.business_unit_id === businessUnitId);
25
- // Then we need to find the matching permission
26
- const hasPermission = !!role?.permissions.find((permission) => permission.permission_id === permissionId);
27
- return hasPermission;
50
+ return roles.some((role) => role.business_unit_id === businessUnitId &&
51
+ role.permissions.some((permission) => permission.permission_id === permissionId));
28
52
  }
29
53
  // if we don't have any roles, we can't have any permissions
30
54
  return true;
@@ -1 +1 @@
1
- {"version":3,"file":"hmcts-opal-frontend-common-services-permissions-service.mjs","sources":["../../../projects/opal-frontend-common/services/permissions-service/permissions.service.ts","../../../projects/opal-frontend-common/services/permissions-service/hmcts-opal-frontend-common-services-permissions-service.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport {\n ISessionUserState,\n ISessionUserStatePermission,\n ISessionUserStateRole,\n} from '@hmcts/opal-frontend-common/services/session-service/interfaces';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class PermissionsService {\n private storedUniquePermissionIds: number[] = [];\n\n /**\n * Retrieves the unique permission IDs associated with the user.\n * If the unique permission IDs have not been stored yet, it calculates them based on the user's roles and permissions.\n * @returns An array of unique permission IDs.\n */\n public getUniquePermissions(userState: ISessionUserState | null): number[] {\n const roles = userState ? userState['business_unit_user'] : null;\n\n if (!this.storedUniquePermissionIds.length && roles) {\n const permissionIds = roles.flatMap((role) => {\n return role.permissions.map(({ permission_id: permissionId }) => permissionId);\n });\n\n this.storedUniquePermissionIds = [...new Set(permissionIds)];\n }\n\n return this.storedUniquePermissionIds;\n }\n\n public hasPermissionAccess(permissionId: number, businessUnitId: number, roles: ISessionUserStateRole[]): boolean {\n if (roles?.length) {\n // First we need to find the matching role\n const role = roles?.find((role) => role.business_unit_id === businessUnitId);\n\n // Then we need to find the matching permission\n const hasPermission = !!role?.permissions.find(\n (permission: ISessionUserStatePermission) => permission.permission_id === permissionId,\n );\n\n return hasPermission;\n }\n // if we don't have any roles, we can't have any permissions\n return true;\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;MAUa,kBAAkB,CAAA;IACrB,yBAAyB,GAAa,EAAE;AAEhD;;;;AAIG;AACI,IAAA,oBAAoB,CAAC,SAAmC,EAAA;AAC7D,QAAA,MAAM,KAAK,GAAG,SAAS,GAAG,SAAS,CAAC,oBAAoB,CAAC,GAAG,IAAI;QAEhE,IAAI,CAAC,IAAI,CAAC,yBAAyB,CAAC,MAAM,IAAI,KAAK,EAAE;YACnD,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,KAAI;AAC3C,gBAAA,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,EAAE,YAAY,EAAE,KAAK,YAAY,CAAC;AAChF,YAAA,CAAC,CAAC;YAEF,IAAI,CAAC,yBAAyB,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,CAAC;QAC9D;QAEA,OAAO,IAAI,CAAC,yBAAyB;IACvC;AAEO,IAAA,mBAAmB,CAAC,YAAoB,EAAE,cAAsB,EAAE,KAA8B,EAAA;AACrG,QAAA,IAAI,KAAK,EAAE,MAAM,EAAE;;AAEjB,YAAA,MAAM,IAAI,GAAG,KAAK,EAAE,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,gBAAgB,KAAK,cAAc,CAAC;;YAG5E,MAAM,aAAa,GAAG,CAAC,CAAC,IAAI,EAAE,WAAW,CAAC,IAAI,CAC5C,CAAC,UAAuC,KAAK,UAAU,CAAC,aAAa,KAAK,YAAY,CACvF;AAED,YAAA,OAAO,aAAa;QACtB;;AAEA,QAAA,OAAO,IAAI;IACb;wGApCW,kBAAkB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAAlB,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,kBAAkB,cAFjB,MAAM,EAAA,CAAA;;4FAEP,kBAAkB,EAAA,UAAA,EAAA,CAAA;kBAH9B,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;ACTD;;AAEG;;;;"}
1
+ {"version":3,"file":"hmcts-opal-frontend-common-services-permissions-service.mjs","sources":["../../../projects/opal-frontend-common/services/permissions-service/permissions.service.ts","../../../projects/opal-frontend-common/services/permissions-service/hmcts-opal-frontend-common-services-permissions-service.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport {\n ISessionUserState,\n ISessionUserStateRole,\n} from '@hmcts/opal-frontend-common/services/session-service/interfaces';\n\n@Injectable({\n providedIn: 'root',\n})\nexport class PermissionsService {\n private storedUniquePermissionIds: number[] = [];\n\n /**\n * Retrieves the unique permission IDs associated with the user.\n * If the unique permission IDs have not been stored yet, it calculates them based on the user's roles and permissions.\n * @returns An array of unique permission IDs.\n */\n public getUniquePermissions(userState: ISessionUserState | null): number[] {\n const roles = userState ? userState['business_unit_user'] : null;\n\n if (!this.storedUniquePermissionIds.length && roles) {\n const permissionIds = roles.flatMap((role) => {\n return role.permissions.map(({ permission_id: permissionId }) => permissionId);\n });\n\n this.storedUniquePermissionIds = [...new Set(permissionIds)];\n }\n\n return this.storedUniquePermissionIds;\n }\n\n /**\n * Determines whether the user has access to a specific permission based on their roles.\n *\n * This method short-circuits and returns as soon as a matching permission is found for efficiency.\n *\n * @param permissionId - The ID of the permission to check for access.\n * @param roles - An array of user roles, each containing a list of permissions.\n * @returns `true` if the user has the specified permission in any of their roles,\n * or if no roles are provided; otherwise, returns `false`.\n */\n public hasPermissionAccess(permissionId: number, roles: ISessionUserStateRole[]): boolean {\n if (roles?.length) {\n return roles.some((role) => role.permissions.some((permission) => permission.permission_id === permissionId));\n }\n // if we don't have any roles, we can't have any permissions\n return true;\n }\n\n /**\n * Checks if the user has access to a specific permission within a given business unit.\n *\n * This method short-circuits and returns as soon as a matching permission is found for efficiency.\n *\n * @param permissionId - The ID of the permission to check.\n * @param businessUnitId - The ID of the business unit to check against.\n * @param roles - An array of user roles, each containing permissions and associated business unit IDs.\n * @returns `true` if the user has the specified permission for the business unit, or if no roles are provided; otherwise, `false`.\n */\n public hasBusinessUnitPermissionAccess(\n permissionId: number,\n businessUnitId: number,\n roles: ISessionUserStateRole[],\n ): boolean {\n if (roles?.length) {\n return roles.some(\n (role) =>\n role.business_unit_id === businessUnitId &&\n role.permissions.some((permission) => permission.permission_id === permissionId),\n );\n }\n // if we don't have any roles, we can't have any permissions\n return true;\n }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;MASa,kBAAkB,CAAA;IACrB,yBAAyB,GAAa,EAAE;AAEhD;;;;AAIG;AACI,IAAA,oBAAoB,CAAC,SAAmC,EAAA;AAC7D,QAAA,MAAM,KAAK,GAAG,SAAS,GAAG,SAAS,CAAC,oBAAoB,CAAC,GAAG,IAAI;QAEhE,IAAI,CAAC,IAAI,CAAC,yBAAyB,CAAC,MAAM,IAAI,KAAK,EAAE;YACnD,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,KAAI;AAC3C,gBAAA,OAAO,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,EAAE,YAAY,EAAE,KAAK,YAAY,CAAC;AAChF,YAAA,CAAC,CAAC;YAEF,IAAI,CAAC,yBAAyB,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,CAAC;QAC9D;QAEA,OAAO,IAAI,CAAC,yBAAyB;IACvC;AAEA;;;;;;;;;AASG;IACI,mBAAmB,CAAC,YAAoB,EAAE,KAA8B,EAAA;AAC7E,QAAA,IAAI,KAAK,EAAE,MAAM,EAAE;YACjB,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,UAAU,KAAK,UAAU,CAAC,aAAa,KAAK,YAAY,CAAC,CAAC;QAC/G;;AAEA,QAAA,OAAO,IAAI;IACb;AAEA;;;;;;;;;AASG;AACI,IAAA,+BAA+B,CACpC,YAAoB,EACpB,cAAsB,EACtB,KAA8B,EAAA;AAE9B,QAAA,IAAI,KAAK,EAAE,MAAM,EAAE;AACjB,YAAA,OAAO,KAAK,CAAC,IAAI,CACf,CAAC,IAAI,KACH,IAAI,CAAC,gBAAgB,KAAK,cAAc;AACxC,gBAAA,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,UAAU,KAAK,UAAU,CAAC,aAAa,KAAK,YAAY,CAAC,CACnF;QACH;;AAEA,QAAA,OAAO,IAAI;IACb;wGAhEW,kBAAkB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;AAAlB,IAAA,OAAA,KAAA,GAAA,EAAA,CAAA,qBAAA,CAAA,EAAA,UAAA,EAAA,QAAA,EAAA,OAAA,EAAA,SAAA,EAAA,QAAA,EAAA,EAAA,EAAA,IAAA,EAAA,kBAAkB,cAFjB,MAAM,EAAA,CAAA;;4FAEP,kBAAkB,EAAA,UAAA,EAAA,CAAA;kBAH9B,UAAU;AAAC,YAAA,IAAA,EAAA,CAAA;AACV,oBAAA,UAAU,EAAE,MAAM;AACnB,iBAAA;;;ACRD;;AAEG;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmcts/opal-frontend-common",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "license": "MIT",
5
5
  "peerDependencies": {
6
6
  "@angular/common": "^18.2.0 || ^19.0.0",
@@ -9,7 +9,28 @@ declare class PermissionsService {
9
9
  * @returns An array of unique permission IDs.
10
10
  */
11
11
  getUniquePermissions(userState: ISessionUserState | null): number[];
12
- hasPermissionAccess(permissionId: number, businessUnitId: number, roles: ISessionUserStateRole[]): boolean;
12
+ /**
13
+ * Determines whether the user has access to a specific permission based on their roles.
14
+ *
15
+ * This method short-circuits and returns as soon as a matching permission is found for efficiency.
16
+ *
17
+ * @param permissionId - The ID of the permission to check for access.
18
+ * @param roles - An array of user roles, each containing a list of permissions.
19
+ * @returns `true` if the user has the specified permission in any of their roles,
20
+ * or if no roles are provided; otherwise, returns `false`.
21
+ */
22
+ hasPermissionAccess(permissionId: number, roles: ISessionUserStateRole[]): boolean;
23
+ /**
24
+ * Checks if the user has access to a specific permission within a given business unit.
25
+ *
26
+ * This method short-circuits and returns as soon as a matching permission is found for efficiency.
27
+ *
28
+ * @param permissionId - The ID of the permission to check.
29
+ * @param businessUnitId - The ID of the business unit to check against.
30
+ * @param roles - An array of user roles, each containing permissions and associated business unit IDs.
31
+ * @returns `true` if the user has the specified permission for the business unit, or if no roles are provided; otherwise, `false`.
32
+ */
33
+ hasBusinessUnitPermissionAccess(permissionId: number, businessUnitId: number, roles: ISessionUserStateRole[]): boolean;
13
34
  static ɵfac: i0.ɵɵFactoryDeclaration<PermissionsService, never>;
14
35
  static ɵprov: i0.ɵɵInjectableDeclaration<PermissionsService>;
15
36
  }