@felloh-org/lambda-wrapper 1.1.46 → 1.1.49

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.
@@ -15,6 +15,8 @@ var _logger = _interopRequireDefault(require("../service/logger"));
15
15
 
16
16
  var _request = _interopRequireDefault(require("../service/request"));
17
17
 
18
+ var _user = _interopRequireDefault(require("../service/user"));
19
+
18
20
  var _warehouse = _interopRequireDefault(require("../service/warehouse"));
19
21
 
20
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -25,6 +27,7 @@ const DEFINITIONS = {
25
27
  HTTP: 'HTTP',
26
28
  LOGGER: 'LOGGER',
27
29
  REQUEST: 'REQUEST',
30
+ USER: 'USER',
28
31
  WAREHOUSE: 'WAREHOUSE'
29
32
  };
30
33
  exports.DEFINITIONS = DEFINITIONS;
@@ -34,7 +37,8 @@ const Dependencies = {
34
37
  [DEFINITIONS.LOGGER]: _logger.default,
35
38
  [DEFINITIONS.REQUEST]: _request.default,
36
39
  [DEFINITIONS.WAREHOUSE]: _warehouse.default,
37
- [DEFINITIONS.AUTHENTICATION]: _authentication.default
40
+ [DEFINITIONS.AUTHENTICATION]: _authentication.default,
41
+ [DEFINITIONS.USER]: _user.default
38
42
  };
39
43
  exports.Dependencies = Dependencies;
40
44
  var _default = {
@@ -36,7 +36,7 @@ class AuthenticationService extends _dependencyAware.default {
36
36
  this.initialised = true;
37
37
  this.userRoleRepository = await that.connection.getRepository(_userRole.default);
38
38
  this.organisationRepository = await that.connection.getRepository(_organisation.default);
39
- this.user = JSON.parse(di.getEvent().requestContext.authorizer.user);
39
+ this.user = await di.get(_dependencies.DEFINITIONS.USER).getUser();
40
40
  }
41
41
  };
42
42
  }
@@ -51,7 +51,7 @@ class AuthenticationService extends _dependencyAware.default {
51
51
  await this.initialise();
52
52
  const response = [];
53
53
 
54
- for await (const organisation of user.organisations) {
54
+ for (const organisation of user.organisations) {
55
55
  const orgs = await this.organisationRepository.findDescendants(organisation, {
56
56
  relations: ['parent']
57
57
  });
@@ -64,7 +64,33 @@ class AuthenticationService extends _dependencyAware.default {
64
64
  return response;
65
65
  }
66
66
  /**
67
- * Check user role and organisation access
67
+ * Fetch roles for a user
68
+ * @param userID
69
+ * @returns {Promise<any[]>}
70
+ */
71
+
72
+
73
+ async fetchUserRoles(userID = null) {
74
+ await this.initialise();
75
+ const roles = await this.userRoleRepository.createQueryBuilder('user_roles').leftJoinAndSelect('user_roles.role', 'role').where('user_id = :userID', {
76
+ userID: userID !== null ? userID : this.user.id
77
+ }).getMany();
78
+ return roles.map(role => role.role.machine_name);
79
+ }
80
+ /**
81
+ * Check to see if user has a role
82
+ * @param role
83
+ * @param userID
84
+ * @returns {Promise<boolean>}
85
+ */
86
+
87
+
88
+ async hasRole(role, userID = null) {
89
+ const roles = await this.fetchUserRoles(userID);
90
+ return roles.indexOf(role) !== -1;
91
+ }
92
+ /**
93
+ * Check a users role and organisation access
68
94
  * @param organisationID
69
95
  * @param roles
70
96
  * @returns {Promise<boolean>}
@@ -103,14 +129,12 @@ class AuthenticationService extends _dependencyAware.default {
103
129
 
104
130
  if (roles.length >= 1) {
105
131
  // Fetch the users assigned roles
106
- const userRoles = await this.userRoleRepository.createQueryBuilder('user_roles').leftJoinAndSelect('user_roles.role', 'role').where('user_id = :userID', {
107
- userID: this.user.id
108
- }).getMany(); // Loop through the roles and determine match count
132
+ const userRoles = await this.fetchUserRoles(this.user.id); // Loop through the roles and determine match count
109
133
 
110
134
  let roleCount = 0;
111
135
 
112
136
  for (const userRole of userRoles) {
113
- if (roles.indexOf(userRole.role.machine_name) !== -1) {
137
+ if (roles.indexOf(userRole) !== -1) {
114
138
  roleCount += 1;
115
139
  }
116
140
  } // Throw an error if the user does not have all the roles
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _dependencies = require("../config/dependencies");
9
+
10
+ var _dependencyAware = _interopRequireDefault(require("../dependency-injection/dependency-aware"));
11
+
12
+ var _user = _interopRequireDefault(require("../entity/user/user"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ // eslint-disable-next-line import/no-named-as-default,import/no-named-as-default-member
17
+
18
+ /**
19
+ * User Service class
20
+ */
21
+ class User extends _dependencyAware.default {
22
+ constructor(di) {
23
+ super(di);
24
+ this.initialised = false;
25
+ const that = this;
26
+
27
+ this.initialise = async () => {
28
+ if (that.initialised === false) {
29
+ const warehouse = di.get(_dependencies.DEFINITIONS.WAREHOUSE);
30
+ that.connection = await warehouse.connect();
31
+ this.initialised = true;
32
+ this.userRepository = await that.connection.getRepository(_user.default);
33
+ }
34
+ };
35
+ }
36
+ /**
37
+ * Fetch
38
+ * @returns {Promise<*>}
39
+ */
40
+
41
+
42
+ async getUser() {
43
+ await this.initialise();
44
+
45
+ if (typeof process.env.IS_OFFLINE !== 'undefined') {
46
+ const sub = this.di.getEvent().requestContext.authorizer.claims.sub;
47
+ return this.userRepository.findOne({
48
+ cognito_sub: sub
49
+ }, {
50
+ relations: ['organisations']
51
+ });
52
+ }
53
+
54
+ return this.di.getEvent().requestContext.authorizer.user;
55
+ }
56
+
57
+ }
58
+
59
+ exports.default = User;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@felloh-org/lambda-wrapper",
3
- "version": "1.1.46",
3
+ "version": "1.1.49",
4
4
  "description": "Lambda wrapper for all Felloh Serverless Projects",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {