@tmlmobilidade/interfaces 20251029.1408.3 → 20251031.1051.3

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.
@@ -1,4 +1,4 @@
1
- import { CreateUserDto, LoginDto, Permission, Session, User } from '@tmlmobilidade/types';
1
+ import { CreateUserDto, LoginDto, OneOrTheOther, Permission, Session, User } from '@tmlmobilidade/types';
2
2
  declare class AuthProvider {
3
3
  private static _instance;
4
4
  /**
@@ -6,13 +6,16 @@ declare class AuthProvider {
6
6
  */
7
7
  static getInstance(): Promise<AuthProvider>;
8
8
  /**
9
- * Get Permissions for a user based on their session token.
10
- * @param sessionToken - The session token
11
- * @param scope - The scope to check
12
- * @param action - The action to check
9
+ * Get Permissions for a user based on their session token or user_id.
10
+ * @param sessionToken - The session token (optional if user_id is provided)
11
+ * @param user_id - The user ID (optional if sessionToken is provided)
13
12
  * @returns The permissions that the user has
14
13
  */
15
- getPermissions<T>(sessionToken: string): Promise<Permission<T>[]>;
14
+ getPermissions<T>(params: OneOrTheOther<{
15
+ sessionToken: string;
16
+ }, {
17
+ user_id: string;
18
+ }>): Promise<Permission<T>[]>;
16
19
  /**
17
20
  * Gets a user by their session token.
18
21
  * @param sessionToken The session token to look up.
@@ -17,17 +17,29 @@ class AuthProvider {
17
17
  return AuthProvider._instance;
18
18
  }
19
19
  /**
20
- * Get Permissions for a user based on their session token.
21
- * @param sessionToken - The session token
22
- * @param scope - The scope to check
23
- * @param action - The action to check
20
+ * Get Permissions for a user based on their session token or user_id.
21
+ * @param sessionToken - The session token (optional if user_id is provided)
22
+ * @param user_id - The user ID (optional if sessionToken is provided)
24
23
  * @returns The permissions that the user has
25
24
  */
26
- async getPermissions(sessionToken) {
25
+ async getPermissions(params) {
27
26
  //
28
27
  //
29
28
  // Get the user and their roles
30
- const userData = await this.getUser(sessionToken);
29
+ let userData;
30
+ if ('user_id' in params) {
31
+ const foundUser = await users.findOne({ _id: { $eq: params.user_id } });
32
+ if (!foundUser) {
33
+ throw new HttpException(HttpStatus.UNAUTHORIZED, 'User not found');
34
+ }
35
+ userData = foundUser;
36
+ }
37
+ else if ('sessionToken' in params) {
38
+ userData = await this.getUser(params.sessionToken);
39
+ }
40
+ else {
41
+ throw new HttpException(HttpStatus.BAD_REQUEST, 'Either sessionToken or user_id must be provided');
42
+ }
31
43
  const rolesData = await roles.findMany({ _id: { $in: userData.role_ids } });
32
44
  const allPermissions = [...rolesData.flatMap(role => role.permissions), ...userData.permissions];
33
45
  const permissionsMap = new Map();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/interfaces",
3
- "version": "20251029.1408.3",
3
+ "version": "20251031.1051.3",
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",