@tmlmobilidade/interfaces 20250925.1818.18 → 20251001.1147.43

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.
@@ -2,7 +2,7 @@
2
2
  import { roles, sessions, users, verificationTokens } from '../../interfaces/index.js';
3
3
  import { sendWelcomeEmail } from '@tmlmobilidade/emails';
4
4
  import { getAppConfig, HttpException, HttpStatus } from '@tmlmobilidade/lib';
5
- import { AsyncSingletonProxy, Dates, generateRandomString, generateRandomToken } from '@tmlmobilidade/utils';
5
+ import { AsyncSingletonProxy, Dates, generateRandomString, generateRandomToken, mergeObjects } from '@tmlmobilidade/utils';
6
6
  import bcrypt from 'bcryptjs';
7
7
  /* * */
8
8
  class AuthProvider {
@@ -29,28 +29,21 @@ class AuthProvider {
29
29
  // Get the user and their roles
30
30
  const userData = await this.getUser(sessionToken);
31
31
  const rolesData = await roles.findMany({ _id: { $in: userData.role_ids } });
32
- //
33
- // Build the permissions list
34
- let combinedPermissions = [];
35
- try {
36
- // Combine permissions from associated roles
37
- // and user-specific permissions
38
- combinedPermissions = [
39
- ...rolesData.flatMap(role => role.permissions),
40
- ...userData.permissions,
41
- ];
42
- }
43
- catch (e) {
44
- throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Error getting permissions', { cause: e });
32
+ const allPermissions = [...rolesData.flatMap(role => role.permissions), ...userData.permissions];
33
+ const permissionsMap = new Map();
34
+ for (const permission of allPermissions) {
35
+ const key = `${permission.scope}:${permission.action}`;
36
+ if (permissionsMap.has(key)) {
37
+ const existingPermission = permissionsMap.get(key);
38
+ if (!existingPermission) {
39
+ throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR, 'Error getting permissions');
40
+ }
41
+ permissionsMap.set(key, mergeObjects(existingPermission, permission));
42
+ continue;
43
+ }
44
+ permissionsMap.set(key, permission);
45
45
  }
46
- //
47
- // If no permission found, throw an error
48
- if (!combinedPermissions?.length) {
49
- throw new HttpException(HttpStatus.FORBIDDEN, 'User does not have permissions');
50
- }
51
- //
52
- // Else, return the permission
53
- return combinedPermissions;
46
+ return Array.from(permissionsMap.values());
54
47
  }
55
48
  /**
56
49
  * Gets a user by their session token.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/interfaces",
3
- "version": "20250925.1818.18",
3
+ "version": "20251001.1147.43",
4
4
  "author": "João de Vasconcelos & Jusi Monteiro",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "homepage": "https://github.com/tmlmobilidade/services#readme",