@nsshunt/stsdatamanagement 1.8.0 → 1.10.0

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.
package/blcauth.js CHANGED
@@ -5,6 +5,7 @@ class BLCAuth
5
5
  {
6
6
  static SYSTEM_USER_ID = "STS_SYSTEM";
7
7
  static USER_ID_PREFIX = "USR_";
8
+ static ROLE_ID_PREFIX = "ROLE_";
8
9
 
9
10
  #accessLayer = null;
10
11
 
@@ -14,17 +15,15 @@ class BLCAuth
14
15
  }
15
16
 
16
17
  // Normally, register would be provided by a hardened dedicated authentication server.
17
- async registeruser(user)
18
+ async AddUser(user)
18
19
  {
19
- let { name, password, email } = user;
20
+ let { name, password, email, roles } = user;
20
21
  const saltRounds = 10;
21
22
  try
22
23
  {
23
24
  let userid = BLCAuth.USER_ID_PREFIX + email;
24
25
  let existingUser = await this.#accessLayer.getLatestResource(userid);
25
- if (existingUser.status === 200)
26
- {
27
- //@@ should throw exception here
26
+ if (existingUser.status === 200) {
28
27
  return { status: status.conflict, error: 'User already exists.', detail: { message: 'User already exists.' }};
29
28
  }
30
29
 
@@ -35,14 +34,17 @@ class BLCAuth
35
34
  ,name: name
36
35
  ,email: email
37
36
  ,hash: hashedPassword
37
+ ,roles: roles
38
38
  };
39
39
 
40
40
  await this.#accessLayer.saveResource(BLCAuth.SYSTEM_USER_ID, user.id, user);
41
41
 
42
42
  let payload =
43
43
  {
44
- name: user.name
45
- ,email: user.email
44
+ id: userid
45
+ ,name: name
46
+ ,email: email
47
+ ,roles: roles
46
48
  }
47
49
 
48
50
  return { status: status.success, detail: payload };
@@ -52,6 +54,71 @@ class BLCAuth
52
54
  throw new Error({ status: status.error, error: 'Operation was not successful', detail: error });
53
55
  }
54
56
  }
57
+
58
+ async AddRolePermissions(rolePermissions)
59
+ {
60
+ try
61
+ {
62
+ const { name, permissions } = rolePermissions;
63
+ let roleId = BLCAuth.ROLE_ID_PREFIX + name;
64
+ let existingRole = await this.#accessLayer.getLatestResource(roleId);
65
+ if (existingRole.status === 200) {
66
+ return { status: status.conflict, error: 'Role already exists.', detail: { message: 'Role already exists.' }};
67
+ }
68
+
69
+ let roleResource = {
70
+ id: roleId,
71
+ name: name,
72
+ permissions: permissions
73
+ }
74
+
75
+ await this.#accessLayer.saveResource(BLCAuth.SYSTEM_USER_ID, roleId, roleResource);
76
+
77
+ return { status: status.success, detail: roleResource };
78
+ } catch (error)
79
+ {
80
+ console.error(error);
81
+ throw new Error({ status: status.error, error: 'Operation was not successful', detail: error });
82
+ }
83
+ }
84
+
85
+ async GetUserPermissions(email)
86
+ {
87
+ try
88
+ {
89
+ let userid = BLCAuth.USER_ID_PREFIX + email;
90
+ let existingUser = await this.#accessLayer.getLatestResource(userid);
91
+ if (existingUser.status !== 200) {
92
+ return { status: status.notfound, error: 'User not found.', detail: { message: 'User not found.' }};
93
+ }
94
+
95
+ let userResource = JSON.parse(existingUser.detail.resdesc);
96
+
97
+ let permissions = [ ];
98
+
99
+ for (let i=0; i < userResource.roles.length; i++) {
100
+ let role = userResource.roles[i];
101
+ let roleId = BLCAuth.ROLE_ID_PREFIX + role;
102
+ let existingRole = await this.#accessLayer.getLatestResource(roleId);
103
+ if (existingRole.status !== 200) {
104
+ return { status: status.notfound, error: 'Role not found.', detail: { message: 'Role not found.' }};
105
+ }
106
+ let roleResource = JSON.parse(existingRole.detail.resdesc);
107
+ for (let j=0; j < roleResource.permissions.length; j++) {
108
+ let permission = roleResource.permissions[j];
109
+ if (!permissions.includes(permission)) {
110
+ permissions.push(permission);
111
+ }
112
+ }
113
+ }
114
+
115
+ return { status: status.success, detail: permissions };
116
+ } catch (error)
117
+ {
118
+ console.error(error);
119
+ throw new Error({ status: status.error, error: 'Operation was not successful', detail: error });
120
+ }
121
+ }
55
122
  }
56
123
 
57
124
  module.exports = { BLCAuth };
package/databaseutils.js CHANGED
@@ -1,7 +1,9 @@
1
1
  const prompts = require('prompts');
2
- const goptions = require('@nsshunt/stsconfig').$options;
2
+ const fs = require('fs');
3
3
  require('colors');
4
4
 
5
+ const goptions = require('@nsshunt/stsconfig').$options;
6
+
5
7
  const { PGPoolManager } = require('./pgpoolmanager');
6
8
  const { PGAccessLayer } = require('./pgaccesslayer');
7
9
  const { PGUtils } = require('./pgutils');
@@ -27,7 +29,7 @@ class DatabaseUtils
27
29
  let ns = `proc:${process.pid}:DatabaseUtils`; // namespace for debug
28
30
  let debug = require('debug')(ns);
29
31
  let fname = 'createfreshdatabase';
30
- const { start, entries, minextradata, maxextradata, user } = options;
32
+ const { start, entries, minextradata, maxextradata } = options;
31
33
  let builddbscript = goptions.databasescriptfolder + '/builddb.sql'
32
34
  debug(`Database Build Script: [${builddbscript}]`.yellow);
33
35
 
@@ -62,10 +64,41 @@ class DatabaseUtils
62
64
 
63
65
  const blcauth = new BLCAuth(localAccesslayer);
64
66
 
65
- // Now register a new K6 test user
66
- debug(`Registering test users.`.yellow);
67
- let retVal = await blcauth.registeruser(user);
68
- console.log(`User registered: ${JSON.stringify(retVal)}`);
67
+ /*
68
+ debug(`Registering Users.`.yellow);
69
+ console.log(`Registering Users.`.yellow);
70
+ const userFile = goptions.databasescriptfolder + '/users.json'
71
+ let rawdata = fs.readFileSync(userFile);
72
+ let users = JSON.parse(rawdata);
73
+ for (const [, user] of Object.entries(users)) {
74
+ let retVal = await blcauth.registeruser(user);
75
+ console.log(`User registered: ${JSON.stringify(retVal)}`);
76
+ }
77
+ */
78
+
79
+ debug(`Registering Users and Roles.`.yellow);
80
+ console.log(`Registering Users and Roles.`.yellow);
81
+ const roleFile = goptions.databasescriptfolder + '/user-role.json'
82
+ let rawdata = fs.readFileSync(roleFile);
83
+ let userroles = JSON.parse(rawdata);
84
+ for (const [, user] of Object.entries(userroles)) {
85
+ let retVal = await blcauth.AddUser(user);
86
+ console.log(`Role registered: ${JSON.stringify(retVal)}`);
87
+ }
88
+
89
+ debug(`Registering Roles and Role Permissions.`.yellow);
90
+ console.log(`Registering Roles and Role Permissions.`.yellow);
91
+ const rolePermissionFile = goptions.databasescriptfolder + '/role-permission.json'
92
+ rawdata = fs.readFileSync(rolePermissionFile);
93
+ let rolePermissions = JSON.parse(rawdata);
94
+ for (const [, rolePermission] of Object.entries(rolePermissions)) {
95
+ let retVal = await blcauth.AddRolePermissions(rolePermission);
96
+ console.log(`Role Permission registered: ${JSON.stringify(retVal)}`);
97
+ }
98
+
99
+ let retVal = await blcauth.GetUserPermissions('STSREST01ServiceUser@stsmda.com');
100
+ console.log(`User Permissions: ${JSON.stringify(retVal)}`);
101
+
69
102
 
70
103
  localAccesslayer.enddatabase();
71
104
  debug(`Database successfully initiailized.`.green);
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nsshunt/stsdatamanagement",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "description": "STS Data Management Modules, Utilities and Services",
5
5
  "main": "dbaccess.js",
6
6
  "dependencies": {
7
- "@nsshunt/stsconfig": "^1.9.0",
8
- "@nsshunt/stsinstrumentation": "^6.4.0",
9
- "@nsshunt/stsutils": "^1.7.0",
10
- "axios": "^0.25.0",
7
+ "@nsshunt/stsconfig": "^1.14.0",
8
+ "@nsshunt/stsinstrumentation": "^6.4.2",
9
+ "@nsshunt/stsutils": "^1.7.3",
10
+ "axios": "^0.26.0",
11
11
  "bcryptjs": "^2.4.3",
12
12
  "cli-progress": "^3.10.0",
13
13
  "colors": "^1.4.0",
@@ -27,11 +27,11 @@
27
27
  "parser": "@babel/eslint-parser"
28
28
  },
29
29
  "devDependencies": {
30
- "@babel/core": "^7.17.2",
30
+ "@babel/core": "^7.17.5",
31
31
  "@babel/eslint-parser": "^7.17.0",
32
32
  "@babel/plugin-proposal-class-properties": "^7.16.7",
33
33
  "@babel/plugin-proposal-private-methods": "^7.16.11",
34
- "eslint": "^8.8.0",
34
+ "eslint": "^8.10.0",
35
35
  "jest": "^27.5.1"
36
36
  },
37
37
  "scripts": {
package/pgaccesslayer.js CHANGED
@@ -154,7 +154,7 @@ class PGAccessLayer
154
154
  try {
155
155
  const { rows } = await client.query(createQuery);
156
156
  const dbResponse = rows[0];
157
- return { status: status.created, detail: dbResponse };
157
+ return { status: status.success, detail: dbResponse };
158
158
  } catch (error) {
159
159
  return { status: status.error, error: `[${fname}]: Operation was not successful`, detail: error }; // Set default
160
160
  } finally {