@magda/acs-cmd 2.3.3 → 3.0.0-alpha.1

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 +18 -14
  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,136 @@
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-list-roles.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/acs-cmd-list-roles.js
80
+ import { table } from "table";
81
+ var pkg = require2("../package.json");
82
+ var pool2 = getDBPool_default();
83
+ program.description("List all roles").version(pkg.version).action(async () => {
84
+ try {
85
+ const selectFields = ["id", "name", "description"];
86
+ const result = await pool2.query(
87
+ `SELECT ${selectFields.join(", ")} FROM roles`
88
+ );
89
+ if (!result || !result.rows || !result.rows.length) {
90
+ throw new Error("Cannot find any records!");
91
+ }
92
+ const data = [["ID", "Name", "Description", "Permissions"]];
93
+ const options = {
94
+ columns: {
95
+ 0: {
96
+ width: 36
97
+ },
98
+ 1: {
99
+ width: 20
100
+ },
101
+ 2: {
102
+ width: 25
103
+ },
104
+ 3: {
105
+ width: 37
106
+ }
107
+ }
108
+ };
109
+ for (let i = 0; i < result.rows.length; i++) {
110
+ const role = result.rows[i];
111
+ const permissions = await getPermissionsByRoleId(role["id"]);
112
+ data.push(
113
+ selectFields.map((k) => role[k]).concat([
114
+ permissions.map((p) => `${p.id}:
115
+ ${p.name}`).join("\n\n")
116
+ ])
117
+ );
118
+ }
119
+ console.log(table(data, options));
120
+ } catch (e) {
121
+ console.error(chalk.red(`Error: ${e}`));
122
+ }
123
+ process.exit(0);
124
+ }).parse(process.argv);
125
+ async function getPermissionsByRoleId(roleId) {
126
+ const result = await pool2.query(
127
+ `SELECT p.*
128
+ FROM role_permissions AS rp
129
+ LEFT JOIN permissions p ON p.id = rp.permission_id
130
+ WHERE rp.role_id = $1`,
131
+ [roleId]
132
+ );
133
+ if (!result || !result.rows || !result.rows.length)
134
+ return [];
135
+ return result.rows;
136
+ }
@@ -0,0 +1,147 @@
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-list-users.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/acs-cmd-list-users.js
80
+ import { table } from "table";
81
+ var pkg = require2("../package.json");
82
+ var pool2 = getDBPool_default();
83
+ program.description("List all users").version(pkg.version).action(async () => {
84
+ try {
85
+ const selectFields = ["id", "displayName", "orgUnitId"];
86
+ const result = await pool2.query(
87
+ `SELECT ${selectFields.map((n) => `"${n}"`).join(", ")} FROM users`
88
+ );
89
+ if (!result || !result.rows || !result.rows.length) {
90
+ throw new Error("Cannot find any records!");
91
+ }
92
+ const data = [["ID", "Name", "Org Unit", "Roles"]];
93
+ const options = {
94
+ columns: {
95
+ 0: {
96
+ width: 36
97
+ },
98
+ 1: {
99
+ width: 15
100
+ },
101
+ 2: {
102
+ width: 20
103
+ },
104
+ 3: {
105
+ width: 37
106
+ }
107
+ }
108
+ };
109
+ for (let i = 0; i < result.rows.length; i++) {
110
+ const user = result.rows[i];
111
+ const roles = await getRolesByUserId(user["id"]);
112
+ const row = selectFields.map((k) => user[k]).concat([
113
+ roles.map((r) => `${r.id}:
114
+ ${r.name}`).join("\n\n")
115
+ ]);
116
+ row[2] = await getOrgUnitNameById(row[2]);
117
+ data.push(row);
118
+ }
119
+ console.log(table(data, options));
120
+ } catch (e) {
121
+ console.error(chalk.red(`Error: ${e}`));
122
+ }
123
+ process.exit(0);
124
+ }).parse(process.argv);
125
+ async function getRolesByUserId(userId) {
126
+ const result = await pool2.query(
127
+ `SELECT r.*
128
+ FROM user_roles AS ur
129
+ LEFT JOIN roles r ON r.id = ur.role_id
130
+ WHERE ur.user_id = $1`,
131
+ [userId]
132
+ );
133
+ if (!result || !result.rows || !result.rows.length)
134
+ return [];
135
+ return result.rows;
136
+ }
137
+ async function getOrgUnitNameById(id) {
138
+ const result = await pool2.query(
139
+ `SELECT name
140
+ FROM org_units
141
+ WHERE id = $1`,
142
+ [id]
143
+ );
144
+ if (!result || !result.rows || !result.rows.length)
145
+ return null;
146
+ return result.rows[0]["name"];
147
+ }
@@ -0,0 +1,62 @@
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-list.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 viewing magda access control data. Version: ${pkg.version}`
50
+ ).command("permissions", "List all permissions").command("roles", "List all roles").command("users", "List all users").on("command:*", function(cmds) {
51
+ if (["permissions", "roles", "users"].indexOf(cmds[0]) === -1) {
52
+ console.error(
53
+ chalk.red(
54
+ `Invalid command: ${program.args.join(
55
+ " "
56
+ )}
57
+ See --help for a list of available commands.`
58
+ )
59
+ );
60
+ process.exit(1);
61
+ }
62
+ }).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-remove-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-remove-permission.js
100
+ var pkg = require2("../package.json");
101
+ var pool2 = getDBPool_default();
102
+ program.description("Remove a permission from a role").argument("<permissionId>", "Permission ID").argument("<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 remove the permission: Role (id: ${roleId}) has no permission with id: ${ropermissionIdleId}!`
121
+ );
122
+ }
123
+ await pool2.query(
124
+ `DELETE FROM role_permissions WHERE role_id = $1 AND permission_id = $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-remove-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-remove-role.js
100
+ var pkg = require2("../package.json");
101
+ var pool2 = getDBPool_default();
102
+ program.description("Remove a role from a user").argument("<roleId>", "Role ID").argument("<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 remove the role: User (id: ${userId}) has no Role with id: ${roleId}!`
119
+ );
120
+ }
121
+ await pool2.query(
122
+ `DELETE FROM user_roles WHERE role_id = $1 AND user_id = $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-remove.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 removing magda access control role / permission assignment. Version: ${pkg.version}`
50
+ ).command(
51
+ "permission <permissionId> <roleId>",
52
+ "Remove a permission from a role"
53
+ ).command("role <roleId> <userId>", "Remove a role from 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);