@magda/acs-cmd 2.3.2 → 3.0.0-alpha.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.
Files changed (43) hide show
  1. package/bin/acs-cmd-admin-set.js +129 -0
  2. package/bin/acs-cmd-admin-unset.js +129 -0
  3. package/bin/acs-cmd-admin.js +63 -0
  4. package/bin/acs-cmd-assign-permission.js +132 -0
  5. package/bin/acs-cmd-assign-role.js +130 -0
  6. package/bin/acs-cmd-assign.js +65 -0
  7. package/bin/acs-cmd-create-operation.js +112 -0
  8. package/bin/acs-cmd-create-permission.js +93 -0
  9. package/bin/acs-cmd-create.js +62 -0
  10. package/bin/acs-cmd-jwt.js +80 -0
  11. package/bin/acs-cmd-list-permissions.js +139 -0
  12. package/bin/acs-cmd-list-resources.js +111 -0
  13. package/bin/acs-cmd-list-roles.js +136 -0
  14. package/bin/acs-cmd-list-users.js +147 -0
  15. package/bin/acs-cmd-list.js +62 -0
  16. package/bin/acs-cmd-remove-permission.js +132 -0
  17. package/bin/acs-cmd-remove-role.js +130 -0
  18. package/bin/acs-cmd-remove.js +65 -0
  19. package/bin/acs-cmd.js +77 -0
  20. package/bin/utils.js +24 -0
  21. package/package.json +17 -13
  22. package/bin/acs-cmd/acs-cmd-admin-set.js +0 -251
  23. package/bin/acs-cmd/acs-cmd-admin-unset.js +0 -252
  24. package/bin/acs-cmd/acs-cmd-admin.js +0 -158
  25. package/bin/acs-cmd/acs-cmd-assign-permission.js +0 -260
  26. package/bin/acs-cmd/acs-cmd-assign-role.js +0 -257
  27. package/bin/acs-cmd/acs-cmd-assign.js +0 -157
  28. package/bin/acs-cmd/acs-cmd-create-operation.js +0 -234
  29. package/bin/acs-cmd/acs-cmd-create-permission.js +0 -241
  30. package/bin/acs-cmd/acs-cmd-create.js +0 -154
  31. package/bin/acs-cmd/acs-cmd-jwt.js +0 -192
  32. package/bin/acs-cmd/acs-cmd-list-permissions.js +0 -267
  33. package/bin/acs-cmd/acs-cmd-list-resources.js +0 -241
  34. package/bin/acs-cmd/acs-cmd-list-roles.js +0 -267
  35. package/bin/acs-cmd/acs-cmd-list-users.js +0 -271
  36. package/bin/acs-cmd/acs-cmd-list.js +0 -155
  37. package/bin/acs-cmd/acs-cmd-remove-permission.js +0 -264
  38. package/bin/acs-cmd/acs-cmd-remove-role.js +0 -260
  39. package/bin/acs-cmd/acs-cmd-remove.js +0 -157
  40. package/bin/acs-cmd/acs-cmd.js +0 -174
  41. package/bin/acs-cmd/utils.js +0 -133
  42. package/bin/db/getDBConfig.js +0 -19
  43. package/bin/db/getDBPool.js +0 -13
@@ -0,0 +1,129 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../../node_modules/@magda/esm-utils/dist/esmUtils.js
4
+ import { createRequire } from "module";
5
+ function callsites() {
6
+ const _prepareStackTrace = Error.prepareStackTrace;
7
+ try {
8
+ let result = [];
9
+ Error.prepareStackTrace = (_, callSites) => {
10
+ const callSitesWithoutCurrent = callSites.slice(1);
11
+ result = callSitesWithoutCurrent;
12
+ return callSitesWithoutCurrent;
13
+ };
14
+ new Error().stack;
15
+ return result;
16
+ } finally {
17
+ Error.prepareStackTrace = _prepareStackTrace;
18
+ }
19
+ }
20
+ function callerCallsite({ depth = 0 } = {}) {
21
+ const callers = [];
22
+ const callerFileSet = /* @__PURE__ */ new Set();
23
+ for (const callsite of callsites()) {
24
+ const fileName = callsite.getFileName();
25
+ const hasReceiver = callsite.getTypeName() !== null && fileName !== null;
26
+ if (!callerFileSet.has(fileName)) {
27
+ callerFileSet.add(fileName);
28
+ callers.unshift(callsite);
29
+ }
30
+ if (hasReceiver) {
31
+ return callers[depth];
32
+ }
33
+ }
34
+ }
35
+ function callerpath({ depth = 0 } = {}) {
36
+ const callsite = callerCallsite({ depth });
37
+ return callsite && callsite.getFileName();
38
+ }
39
+ function require2(id) {
40
+ const requireFrom = createRequire(callerpath({ depth: 1 }));
41
+ return requireFrom(id);
42
+ }
43
+
44
+ // ../../scripts/acs-cmd/acs-cmd-admin-set.js
45
+ import { program } from "commander";
46
+ import chalk from "chalk";
47
+
48
+ // ../../scripts/db/getDBPool.js
49
+ import pg from "pg";
50
+
51
+ // ../../scripts/db/getDBConfig.js
52
+ function getDBConfig() {
53
+ const {
54
+ POSTGRES_HOST: host,
55
+ POSTGRES_DB: database,
56
+ POSTGRES_USER: user,
57
+ POSTGRES_PASSWORD: password,
58
+ POSTGRES_PORT: port
59
+ } = process.env;
60
+ return {
61
+ host: host ? host : "localhost",
62
+ database: database ? database : "auth",
63
+ port: port ? port : 5432,
64
+ user: user ? user : "postgres",
65
+ password: password ? password : ""
66
+ };
67
+ }
68
+
69
+ // ../../scripts/db/getDBPool.js
70
+ var pool = new pg.Pool(getDBConfig());
71
+ pool.on("error", function(err, client) {
72
+ console.error("DB Pool Error: ", err.message, err.stack);
73
+ });
74
+ function getDBPool() {
75
+ return pool;
76
+ }
77
+ var getDBPool_default = getDBPool;
78
+
79
+ // ../../scripts/acs-cmd/utils.js
80
+ async function recordExist(pool3, table, record) {
81
+ if (!Object.keys(record).length) {
82
+ throw new Error("record cannot be an empty object!");
83
+ }
84
+ const sqlValues = [];
85
+ const where = Object.keys(record).map((key) => {
86
+ sqlValues.push(record[key]);
87
+ return `"${key}" = $${sqlValues.length}`;
88
+ }).join(" AND ");
89
+ const result = await pool3.query(
90
+ `SELECT id FROM "${table}" WHERE ${where}`,
91
+ sqlValues
92
+ );
93
+ if (!result || !result.rows || !result.rows.length) {
94
+ return false;
95
+ }
96
+ return true;
97
+ }
98
+ var ADMIN_ROLE_ID = "00000000-0000-0003-0000-000000000000";
99
+
100
+ // ../../scripts/acs-cmd/acs-cmd-admin-set.js
101
+ var pkg = require2("../package.json");
102
+ var pool2 = getDBPool_default();
103
+ program.description("Make a user an Admin user").option("<userId>", "User ID").version(pkg.version).action(async (userId) => {
104
+ try {
105
+ if (process.argv.slice(2).length < 1) {
106
+ program.help();
107
+ }
108
+ if (!await recordExist(pool2, "users", { id: userId })) {
109
+ throw new Error(`Supplied userId: ${userId} doesn't exist`);
110
+ }
111
+ if (!await recordExist(pool2, "user_roles", {
112
+ role_id: ADMIN_ROLE_ID,
113
+ user_id: userId
114
+ })) {
115
+ await pool2.query(
116
+ `INSERT INTO "user_roles" ("role_id", "user_id") VALUES ($1, $2)`,
117
+ [ADMIN_ROLE_ID, userId]
118
+ );
119
+ }
120
+ await pool2.query(
121
+ `UPDATE "users" SET "isAdmin" = $1 WHERE "id" = $2`,
122
+ [true, userId]
123
+ );
124
+ console.log(chalk.green("Operation Completed!"));
125
+ } catch (e) {
126
+ console.error(chalk.red(`Error: ${e}`));
127
+ }
128
+ process.exit(0);
129
+ }).parse(process.argv);
@@ -0,0 +1,129 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../../node_modules/@magda/esm-utils/dist/esmUtils.js
4
+ import { createRequire } from "module";
5
+ function callsites() {
6
+ const _prepareStackTrace = Error.prepareStackTrace;
7
+ try {
8
+ let result = [];
9
+ Error.prepareStackTrace = (_, callSites) => {
10
+ const callSitesWithoutCurrent = callSites.slice(1);
11
+ result = callSitesWithoutCurrent;
12
+ return callSitesWithoutCurrent;
13
+ };
14
+ new Error().stack;
15
+ return result;
16
+ } finally {
17
+ Error.prepareStackTrace = _prepareStackTrace;
18
+ }
19
+ }
20
+ function callerCallsite({ depth = 0 } = {}) {
21
+ const callers = [];
22
+ const callerFileSet = /* @__PURE__ */ new Set();
23
+ for (const callsite of callsites()) {
24
+ const fileName = callsite.getFileName();
25
+ const hasReceiver = callsite.getTypeName() !== null && fileName !== null;
26
+ if (!callerFileSet.has(fileName)) {
27
+ callerFileSet.add(fileName);
28
+ callers.unshift(callsite);
29
+ }
30
+ if (hasReceiver) {
31
+ return callers[depth];
32
+ }
33
+ }
34
+ }
35
+ function callerpath({ depth = 0 } = {}) {
36
+ const callsite = callerCallsite({ depth });
37
+ return callsite && callsite.getFileName();
38
+ }
39
+ function require2(id) {
40
+ const requireFrom = createRequire(callerpath({ depth: 1 }));
41
+ return requireFrom(id);
42
+ }
43
+
44
+ // ../../scripts/acs-cmd/acs-cmd-admin-unset.js
45
+ import { program } from "commander";
46
+ import chalk from "chalk";
47
+
48
+ // ../../scripts/db/getDBPool.js
49
+ import pg from "pg";
50
+
51
+ // ../../scripts/db/getDBConfig.js
52
+ function getDBConfig() {
53
+ const {
54
+ POSTGRES_HOST: host,
55
+ POSTGRES_DB: database,
56
+ POSTGRES_USER: user,
57
+ POSTGRES_PASSWORD: password,
58
+ POSTGRES_PORT: port
59
+ } = process.env;
60
+ return {
61
+ host: host ? host : "localhost",
62
+ database: database ? database : "auth",
63
+ port: port ? port : 5432,
64
+ user: user ? user : "postgres",
65
+ password: password ? password : ""
66
+ };
67
+ }
68
+
69
+ // ../../scripts/db/getDBPool.js
70
+ var pool = new pg.Pool(getDBConfig());
71
+ pool.on("error", function(err, client) {
72
+ console.error("DB Pool Error: ", err.message, err.stack);
73
+ });
74
+ function getDBPool() {
75
+ return pool;
76
+ }
77
+ var getDBPool_default = getDBPool;
78
+
79
+ // ../../scripts/acs-cmd/utils.js
80
+ async function recordExist(pool3, table, record) {
81
+ if (!Object.keys(record).length) {
82
+ throw new Error("record cannot be an empty object!");
83
+ }
84
+ const sqlValues = [];
85
+ const where = Object.keys(record).map((key) => {
86
+ sqlValues.push(record[key]);
87
+ return `"${key}" = $${sqlValues.length}`;
88
+ }).join(" AND ");
89
+ const result = await pool3.query(
90
+ `SELECT id FROM "${table}" WHERE ${where}`,
91
+ sqlValues
92
+ );
93
+ if (!result || !result.rows || !result.rows.length) {
94
+ return false;
95
+ }
96
+ return true;
97
+ }
98
+ var ADMIN_ROLE_ID = "00000000-0000-0003-0000-000000000000";
99
+
100
+ // ../../scripts/acs-cmd/acs-cmd-admin-unset.js
101
+ var pkg = require2("../package.json");
102
+ var pool2 = getDBPool_default();
103
+ program.description("Remove Admin role / status from a user").option("<userId>", "User ID").version(pkg.version).action(async (userId) => {
104
+ try {
105
+ if (process.argv.slice(2).length < 1) {
106
+ program.help();
107
+ }
108
+ if (!await recordExist(pool2, "users", { id: userId })) {
109
+ throw new Error(`Supplied userId: ${userId} doesn't exist`);
110
+ }
111
+ if (await recordExist(pool2, "user_roles", {
112
+ role_id: ADMIN_ROLE_ID,
113
+ user_id: userId
114
+ })) {
115
+ await pool2.query(
116
+ `DELETE FROM "user_roles" WHERE "role_id" = $1 AND "user_id" = $2`,
117
+ [ADMIN_ROLE_ID, userId]
118
+ );
119
+ }
120
+ await pool2.query(
121
+ `UPDATE "users" SET "isAdmin"=$1 WHERE "id" = $2`,
122
+ [false, userId]
123
+ );
124
+ console.log(chalk.green("Operation Completed!"));
125
+ } catch (e) {
126
+ console.error(chalk.red(`Error: ${e}`));
127
+ }
128
+ process.exit(0);
129
+ }).parse(process.argv);
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../../node_modules/@magda/esm-utils/dist/esmUtils.js
4
+ import { createRequire } from "module";
5
+ function callsites() {
6
+ const _prepareStackTrace = Error.prepareStackTrace;
7
+ try {
8
+ let result = [];
9
+ Error.prepareStackTrace = (_, callSites) => {
10
+ const callSitesWithoutCurrent = callSites.slice(1);
11
+ result = callSitesWithoutCurrent;
12
+ return callSitesWithoutCurrent;
13
+ };
14
+ new Error().stack;
15
+ return result;
16
+ } finally {
17
+ Error.prepareStackTrace = _prepareStackTrace;
18
+ }
19
+ }
20
+ function callerCallsite({ depth = 0 } = {}) {
21
+ const callers = [];
22
+ const callerFileSet = /* @__PURE__ */ new Set();
23
+ for (const callsite of callsites()) {
24
+ const fileName = callsite.getFileName();
25
+ const hasReceiver = callsite.getTypeName() !== null && fileName !== null;
26
+ if (!callerFileSet.has(fileName)) {
27
+ callerFileSet.add(fileName);
28
+ callers.unshift(callsite);
29
+ }
30
+ if (hasReceiver) {
31
+ return callers[depth];
32
+ }
33
+ }
34
+ }
35
+ function callerpath({ depth = 0 } = {}) {
36
+ const callsite = callerCallsite({ depth });
37
+ return callsite && callsite.getFileName();
38
+ }
39
+ function require2(id) {
40
+ const requireFrom = createRequire(callerpath({ depth: 1 }));
41
+ return requireFrom(id);
42
+ }
43
+
44
+ // ../../scripts/acs-cmd/acs-cmd-admin.js
45
+ import { program } from "commander";
46
+ import chalk from "chalk";
47
+ var pkg = require2("../package.json");
48
+ program.version(pkg.version).description(
49
+ `A tool for setting / unsetting Admin role / status to a user. Version: ${pkg.version}
50
+ Hint: Use 'acs-cmd list users' command to list all users in systemp`
51
+ ).command("set <userId>", "Make a user an admin user").command("unset <userId>", "Remove admin role / status from a user").on("command:*", function(cmds) {
52
+ if (["set", "unset"].indexOf(cmds[0]) === -1) {
53
+ console.error(
54
+ chalk.red(
55
+ `Invalid command: ${program.args.join(
56
+ " "
57
+ )}
58
+ See --help for a list of available commands.`
59
+ )
60
+ );
61
+ process.exit(1);
62
+ }
63
+ }).parse(process.argv);
@@ -0,0 +1,132 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../../node_modules/@magda/esm-utils/dist/esmUtils.js
4
+ import { createRequire } from "module";
5
+ function callsites() {
6
+ const _prepareStackTrace = Error.prepareStackTrace;
7
+ try {
8
+ let result = [];
9
+ Error.prepareStackTrace = (_, callSites) => {
10
+ const callSitesWithoutCurrent = callSites.slice(1);
11
+ result = callSitesWithoutCurrent;
12
+ return callSitesWithoutCurrent;
13
+ };
14
+ new Error().stack;
15
+ return result;
16
+ } finally {
17
+ Error.prepareStackTrace = _prepareStackTrace;
18
+ }
19
+ }
20
+ function callerCallsite({ depth = 0 } = {}) {
21
+ const callers = [];
22
+ const callerFileSet = /* @__PURE__ */ new Set();
23
+ for (const callsite of callsites()) {
24
+ const fileName = callsite.getFileName();
25
+ const hasReceiver = callsite.getTypeName() !== null && fileName !== null;
26
+ if (!callerFileSet.has(fileName)) {
27
+ callerFileSet.add(fileName);
28
+ callers.unshift(callsite);
29
+ }
30
+ if (hasReceiver) {
31
+ return callers[depth];
32
+ }
33
+ }
34
+ }
35
+ function callerpath({ depth = 0 } = {}) {
36
+ const callsite = callerCallsite({ depth });
37
+ return callsite && callsite.getFileName();
38
+ }
39
+ function require2(id) {
40
+ const requireFrom = createRequire(callerpath({ depth: 1 }));
41
+ return requireFrom(id);
42
+ }
43
+
44
+ // ../../scripts/acs-cmd/acs-cmd-assign-permission.js
45
+ import { program } from "commander";
46
+ import chalk from "chalk";
47
+
48
+ // ../../scripts/db/getDBPool.js
49
+ import pg from "pg";
50
+
51
+ // ../../scripts/db/getDBConfig.js
52
+ function getDBConfig() {
53
+ const {
54
+ POSTGRES_HOST: host,
55
+ POSTGRES_DB: database,
56
+ POSTGRES_USER: user,
57
+ POSTGRES_PASSWORD: password,
58
+ POSTGRES_PORT: port
59
+ } = process.env;
60
+ return {
61
+ host: host ? host : "localhost",
62
+ database: database ? database : "auth",
63
+ port: port ? port : 5432,
64
+ user: user ? user : "postgres",
65
+ password: password ? password : ""
66
+ };
67
+ }
68
+
69
+ // ../../scripts/db/getDBPool.js
70
+ var pool = new pg.Pool(getDBConfig());
71
+ pool.on("error", function(err, client) {
72
+ console.error("DB Pool Error: ", err.message, err.stack);
73
+ });
74
+ function getDBPool() {
75
+ return pool;
76
+ }
77
+ var getDBPool_default = getDBPool;
78
+
79
+ // ../../scripts/acs-cmd/utils.js
80
+ async function recordExist(pool3, table, record) {
81
+ if (!Object.keys(record).length) {
82
+ throw new Error("record cannot be an empty object!");
83
+ }
84
+ const sqlValues = [];
85
+ const where = Object.keys(record).map((key) => {
86
+ sqlValues.push(record[key]);
87
+ return `"${key}" = $${sqlValues.length}`;
88
+ }).join(" AND ");
89
+ const result = await pool3.query(
90
+ `SELECT id FROM "${table}" WHERE ${where}`,
91
+ sqlValues
92
+ );
93
+ if (!result || !result.rows || !result.rows.length) {
94
+ return false;
95
+ }
96
+ return true;
97
+ }
98
+
99
+ // ../../scripts/acs-cmd/acs-cmd-assign-permission.js
100
+ var pkg = require2("../package.json");
101
+ var pool2 = getDBPool_default();
102
+ program.description("assign the permission to a role").option("<permissionId>", "Permission ID").option("<roleId>", "Role ID").version(pkg.version).action(async (permissionId, roleId) => {
103
+ try {
104
+ if (process.argv.slice(2).length < 2) {
105
+ program.help();
106
+ }
107
+ if (!await recordExist(pool2, "permissions", { id: permissionId })) {
108
+ throw new Error(
109
+ `Supplied permissionId: ${permissionId} doesn't exist`
110
+ );
111
+ }
112
+ if (!await recordExist(pool2, "roles", { id: roleId })) {
113
+ throw new Error(`Supplied roleId: ${roleId} doesn't exist`);
114
+ }
115
+ if (await recordExist(pool2, "role_permissions", {
116
+ role_id: roleId,
117
+ permission_id: permissionId
118
+ })) {
119
+ throw new Error(
120
+ `Cannot re-assign the permission: Role (id: ${roleId}) has the permission (id: ${permissionId}) already!`
121
+ );
122
+ }
123
+ await pool2.query(
124
+ `INSERT INTO role_permissions (role_id, permission_id) VALUES ($1, $2)`,
125
+ [roleId, permissionId]
126
+ );
127
+ console.log(chalk.green("Operation Completed!"));
128
+ } catch (e) {
129
+ console.error(chalk.red(`Error: ${e}`));
130
+ }
131
+ process.exit(0);
132
+ }).parse(process.argv);
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../../node_modules/@magda/esm-utils/dist/esmUtils.js
4
+ import { createRequire } from "module";
5
+ function callsites() {
6
+ const _prepareStackTrace = Error.prepareStackTrace;
7
+ try {
8
+ let result = [];
9
+ Error.prepareStackTrace = (_, callSites) => {
10
+ const callSitesWithoutCurrent = callSites.slice(1);
11
+ result = callSitesWithoutCurrent;
12
+ return callSitesWithoutCurrent;
13
+ };
14
+ new Error().stack;
15
+ return result;
16
+ } finally {
17
+ Error.prepareStackTrace = _prepareStackTrace;
18
+ }
19
+ }
20
+ function callerCallsite({ depth = 0 } = {}) {
21
+ const callers = [];
22
+ const callerFileSet = /* @__PURE__ */ new Set();
23
+ for (const callsite of callsites()) {
24
+ const fileName = callsite.getFileName();
25
+ const hasReceiver = callsite.getTypeName() !== null && fileName !== null;
26
+ if (!callerFileSet.has(fileName)) {
27
+ callerFileSet.add(fileName);
28
+ callers.unshift(callsite);
29
+ }
30
+ if (hasReceiver) {
31
+ return callers[depth];
32
+ }
33
+ }
34
+ }
35
+ function callerpath({ depth = 0 } = {}) {
36
+ const callsite = callerCallsite({ depth });
37
+ return callsite && callsite.getFileName();
38
+ }
39
+ function require2(id) {
40
+ const requireFrom = createRequire(callerpath({ depth: 1 }));
41
+ return requireFrom(id);
42
+ }
43
+
44
+ // ../../scripts/acs-cmd/acs-cmd-assign-role.js
45
+ import { program } from "commander";
46
+ import chalk from "chalk";
47
+
48
+ // ../../scripts/db/getDBPool.js
49
+ import pg from "pg";
50
+
51
+ // ../../scripts/db/getDBConfig.js
52
+ function getDBConfig() {
53
+ const {
54
+ POSTGRES_HOST: host,
55
+ POSTGRES_DB: database,
56
+ POSTGRES_USER: user,
57
+ POSTGRES_PASSWORD: password,
58
+ POSTGRES_PORT: port
59
+ } = process.env;
60
+ return {
61
+ host: host ? host : "localhost",
62
+ database: database ? database : "auth",
63
+ port: port ? port : 5432,
64
+ user: user ? user : "postgres",
65
+ password: password ? password : ""
66
+ };
67
+ }
68
+
69
+ // ../../scripts/db/getDBPool.js
70
+ var pool = new pg.Pool(getDBConfig());
71
+ pool.on("error", function(err, client) {
72
+ console.error("DB Pool Error: ", err.message, err.stack);
73
+ });
74
+ function getDBPool() {
75
+ return pool;
76
+ }
77
+ var getDBPool_default = getDBPool;
78
+
79
+ // ../../scripts/acs-cmd/utils.js
80
+ async function recordExist(pool3, table, record) {
81
+ if (!Object.keys(record).length) {
82
+ throw new Error("record cannot be an empty object!");
83
+ }
84
+ const sqlValues = [];
85
+ const where = Object.keys(record).map((key) => {
86
+ sqlValues.push(record[key]);
87
+ return `"${key}" = $${sqlValues.length}`;
88
+ }).join(" AND ");
89
+ const result = await pool3.query(
90
+ `SELECT id FROM "${table}" WHERE ${where}`,
91
+ sqlValues
92
+ );
93
+ if (!result || !result.rows || !result.rows.length) {
94
+ return false;
95
+ }
96
+ return true;
97
+ }
98
+
99
+ // ../../scripts/acs-cmd/acs-cmd-assign-role.js
100
+ var pkg = require2("../package.json");
101
+ var pool2 = getDBPool_default();
102
+ program.description("assign the role to a user").option("<roleId>", "Role ID").option("<userId>", "User ID").version(pkg.version).action(async (roleId, userId) => {
103
+ try {
104
+ if (process.argv.slice(2).length < 2) {
105
+ program.help();
106
+ }
107
+ if (!await recordExist(pool2, "users", { id: userId })) {
108
+ throw new Error(`Supplied userId: ${userId} doesn't exist`);
109
+ }
110
+ if (!await recordExist(pool2, "roles", { id: roleId })) {
111
+ throw new Error(`Supplied roleId: ${roleId} doesn't exist`);
112
+ }
113
+ if (await recordExist(pool2, "user_roles", {
114
+ role_id: roleId,
115
+ user_id: userId
116
+ })) {
117
+ throw new Error(
118
+ `Cannot re-assign the role: User (id: ${userId}) has the Role (id: ${roleId}) already!`
119
+ );
120
+ }
121
+ await pool2.query(
122
+ `INSERT INTO user_roles (role_id, user_id) VALUES ($1, $2)`,
123
+ [roleId, userId]
124
+ );
125
+ console.log(chalk.green("Operation Completed!"));
126
+ } catch (e) {
127
+ console.error(chalk.red(`Error: ${e}`));
128
+ }
129
+ process.exit(0);
130
+ }).parse(process.argv);
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env node
2
+
3
+ // ../../node_modules/@magda/esm-utils/dist/esmUtils.js
4
+ import { createRequire } from "module";
5
+ function callsites() {
6
+ const _prepareStackTrace = Error.prepareStackTrace;
7
+ try {
8
+ let result = [];
9
+ Error.prepareStackTrace = (_, callSites) => {
10
+ const callSitesWithoutCurrent = callSites.slice(1);
11
+ result = callSitesWithoutCurrent;
12
+ return callSitesWithoutCurrent;
13
+ };
14
+ new Error().stack;
15
+ return result;
16
+ } finally {
17
+ Error.prepareStackTrace = _prepareStackTrace;
18
+ }
19
+ }
20
+ function callerCallsite({ depth = 0 } = {}) {
21
+ const callers = [];
22
+ const callerFileSet = /* @__PURE__ */ new Set();
23
+ for (const callsite of callsites()) {
24
+ const fileName = callsite.getFileName();
25
+ const hasReceiver = callsite.getTypeName() !== null && fileName !== null;
26
+ if (!callerFileSet.has(fileName)) {
27
+ callerFileSet.add(fileName);
28
+ callers.unshift(callsite);
29
+ }
30
+ if (hasReceiver) {
31
+ return callers[depth];
32
+ }
33
+ }
34
+ }
35
+ function callerpath({ depth = 0 } = {}) {
36
+ const callsite = callerCallsite({ depth });
37
+ return callsite && callsite.getFileName();
38
+ }
39
+ function require2(id) {
40
+ const requireFrom = createRequire(callerpath({ depth: 1 }));
41
+ return requireFrom(id);
42
+ }
43
+
44
+ // ../../scripts/acs-cmd/acs-cmd-assign.js
45
+ import { program } from "commander";
46
+ import chalk from "chalk";
47
+ var pkg = require2("../package.json");
48
+ program.version(pkg.version).description(
49
+ `A tool for assigning magda access control role / permission. Version: ${pkg.version}`
50
+ ).command(
51
+ "permission <permissionId> <roleId>",
52
+ "Assign a permission to a role"
53
+ ).command("role <roleId> <userId>", "Assign a role to a user").on("command:*", function(cmds) {
54
+ if (["permission", "role"].indexOf(cmds[0]) === -1) {
55
+ console.error(
56
+ chalk.red(
57
+ `Invalid command: ${program.args.join(
58
+ " "
59
+ )}
60
+ See --help for a list of available commands.`
61
+ )
62
+ );
63
+ process.exit(1);
64
+ }
65
+ }).parse(process.argv);