@wirechunk/cli 0.0.1-rc.3 → 0.0.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.
package/build/main.js CHANGED
@@ -57385,6 +57385,7 @@ const createUser = async (opts, env2) => {
57385
57385
  }
57386
57386
  let platformId = opts.platformId;
57387
57387
  let orgId = opts.orgId;
57388
+ let orgPrimary = false;
57388
57389
  try {
57389
57390
  const user = await db.transaction(async (db2) => {
57390
57391
  if (!platformId) {
@@ -57429,12 +57430,20 @@ const createUser = async (opts, env2) => {
57429
57430
  if (opts.verbose) {
57430
57431
  console.log(`Created org ID ${orgId}`);
57431
57432
  }
57433
+ orgPrimary = true;
57432
57434
  }
57433
57435
  const user2 = await db2.one(
57434
57436
  distExports$1.sql.type(
57435
57437
  insertUserResult
57436
57438
  )`insert into "Users" ("platformId", "email", "emailVerified", "password", "passwordStatus", "orgId", "role", "status", "firstName", "lastName") values (${platformId}, ${email}, ${opts.emailVerified}, ${password}, 'Ok', ${orgId}, ${role}, ${status}, ${firstName}, ${lastName}) returning "id"`
57437
57439
  );
57440
+ if (orgPrimary) {
57441
+ await db2.maybeOne(
57442
+ distExports$1.sql.type(
57443
+ voidSelectSchema
57444
+ )`update "Orgs" set "primaryUserId" = ${user2.id} where "id" = ${orgId}`
57445
+ );
57446
+ }
57438
57447
  return user2;
57439
57448
  });
57440
57449
  console.log(`Created user (ID ${user.id})`);
@@ -57447,13 +57456,107 @@ const createUser = async (opts, env2) => {
57447
57456
  process.exit(1);
57448
57457
  }
57449
57458
  };
57450
- const findUserSchema = z.object({
57459
+ const Permission = {
57460
+ /** Create (i.e., add) extensions. */
57461
+ CreateExtension: "CreateExtension",
57462
+ /** Create sites. */
57463
+ CreateSite: "CreateSite",
57464
+ /** Create page and form templates. */
57465
+ CreateTemplate: "CreateTemplate",
57466
+ /** Create a user in any org. */
57467
+ CreateUser: "CreateUser",
57468
+ /** Edit or manage everything else not covered by other permissions. */
57469
+ Edit: "Edit",
57470
+ /** Edit, including creating and deleting, any component. */
57471
+ EditComponent: "EditComponent",
57472
+ /** Edit, including creating and deleting, any course. */
57473
+ EditCourse: "EditCourse",
57474
+ /** Edit, including creating and deleting, any custom component. */
57475
+ EditCustomComponent: "EditCustomComponent",
57476
+ /** Edit, including creating and deleting, any custom field. */
57477
+ EditCustomField: "EditCustomField",
57478
+ /** Edit any customer site, including its pages and forms, but not necessarily domain. */
57479
+ EditCustomerSite: "EditCustomerSite",
57480
+ /** Edit any extension. */
57481
+ EditExtension: "EditExtension",
57482
+ /** Edit any help ticket's status. */
57483
+ EditHelpTicketStatus: "EditHelpTicketStatus",
57484
+ /** Edit any platform site, including its pages and forms, but not necessarily domain. */
57485
+ EditPlatformSite: "EditPlatformSite",
57486
+ /** Edit, including creating and deleting, any sequence. */
57487
+ EditSequence: "EditSequence",
57488
+ /** Edit any user's position in a sequence. */
57489
+ EditSequenceUser: "EditSequenceUser",
57490
+ /** Edit any site's settings, pages, forms, and layouts. */
57491
+ EditSite: "EditSite",
57492
+ /** Edit any site's domain. */
57493
+ EditSiteDomain: "EditSiteDomain",
57494
+ /** Edit any site's TLS certificate, including creating and deleting certificates. Does not including editing TLS certificates. */
57495
+ EditSiteTlsCertificate: "EditSiteTlsCertificate",
57496
+ /** Edit any subscription. */
57497
+ EditSubscription: "EditSubscription",
57498
+ /** Edit any page and form template. */
57499
+ EditTemplate: "EditTemplate",
57500
+ /** Edit any user's email address. */
57501
+ EditUserEmail: "EditUserEmail",
57502
+ /** Edit which org any user is in and whether a user is an org owner. */
57503
+ EditUserOrg: "EditUserOrg",
57504
+ /** Edit any user's first and last name. */
57505
+ EditUserProfile: "EditUserProfile",
57506
+ /** Edit any user's role. */
57507
+ EditUserRole: "EditUserRole",
57508
+ /** Edit any user's status. */
57509
+ EditUserStatus: "EditUserStatus",
57510
+ /** Sync any form template to forms. */
57511
+ SyncFormTemplateToForms: "SyncFormTemplateToForms",
57512
+ /** Sync any page template to pages. */
57513
+ SyncPageTemplateToPages: "SyncPageTemplateToPages",
57514
+ /** View anything except for sites. */
57515
+ View: "View",
57516
+ /** View any course. */
57517
+ ViewCourse: "ViewCourse",
57518
+ /** View any extension. */
57519
+ ViewExtension: "ViewExtension",
57520
+ /** View any site, including pages, forms, and layouts, and components. */
57521
+ ViewSite: "ViewSite",
57522
+ /** View any page or form template. */
57523
+ ViewTemplate: "ViewTemplate"
57524
+ };
57525
+ const allPermissions = Object.values(Permission);
57526
+ const revokeAllUserPlatformPermissions = async ({
57527
+ platformAdminId
57528
+ }, db) => {
57529
+ await db.query(
57530
+ distExports$1.sql.type(
57531
+ voidSelectSchema
57532
+ )`delete from "PlatformAdminPermissions" where "id" = ${platformAdminId}`
57533
+ );
57534
+ };
57535
+ const grantAllUserPlatformPermissions = async ({
57536
+ platformAdminId
57537
+ }, db) => {
57538
+ await db.query(
57539
+ distExports$1.sql.type(
57540
+ voidSelectSchema
57541
+ )`insert into "PlatformAdminPermissions" ("id", "platformAdminId", "permission") values ${distExports$1.sql.join(
57542
+ allPermissions.map(
57543
+ (permission) => distExports$1.sql.fragment`(${cleanSmallId()}, ${platformAdminId}, ${permission})`
57544
+ ),
57545
+ distExports$1.sql.fragment`,`
57546
+ )} on conflict ("platformAdminId", "permission") do nothing`
57547
+ );
57548
+ };
57549
+ const findPlatformAdminSchema = z.object({
57451
57550
  id: z.string(),
57452
- platformId: z.string()
57551
+ platformId: z.string(),
57552
+ active: z.boolean()
57553
+ });
57554
+ const findUserSchema = z.object({
57555
+ id: z.string()
57453
57556
  });
57454
57557
  const editAdmin = async (opts, env2) => {
57455
57558
  const db = await distExports$1.createPool(requireCoreDbUrl(env2));
57456
- const { platformId, userId, owner, revokeAllPermissions } = opts;
57559
+ const { platformId, userId, owner, active, revokeAllPermissions } = opts;
57457
57560
  if (owner && revokeAllPermissions) {
57458
57561
  console.error(
57459
57562
  "Cannot set a user as a platform owner and revoke all permissions at the same time"
@@ -57462,9 +57565,9 @@ const editAdmin = async (opts, env2) => {
57462
57565
  }
57463
57566
  try {
57464
57567
  await db.transaction(async (db2) => {
57465
- const platformAdmin = await db2.maybeOne(
57568
+ let platformAdmin = await db2.maybeOne(
57466
57569
  distExports$1.sql.type(
57467
- findUserSchema
57570
+ findPlatformAdminSchema
57468
57571
  )`select "id" from "PlatformAdmins" where "platformId" = ${platformId} and "userId" = ${userId}`
57469
57572
  );
57470
57573
  if (!platformAdmin) {
@@ -57475,6 +57578,66 @@ const editAdmin = async (opts, env2) => {
57475
57578
  throw new Error(`User with ID ${userId} not found`);
57476
57579
  }
57477
57580
  }
57581
+ if (owner) {
57582
+ if (!platformAdmin) {
57583
+ platformAdmin = await db2.one(
57584
+ distExports$1.sql.type(findPlatformAdminSchema)`
57585
+ insert into "PlatformAdmins" ("id", "platformId", "userId", "owner", "active")
57586
+ values (${cleanSmallId()}, ${platformId}, ${userId}, ${active ?? true}, true)
57587
+ returning "id", "platformId", "active"
57588
+ `
57589
+ );
57590
+ }
57591
+ await grantAllUserPlatformPermissions({ platformAdminId: platformAdmin.id }, db2);
57592
+ if (opts.verbose) {
57593
+ console.log("Set the user as an owner on the platform");
57594
+ }
57595
+ } else if (owner === false) {
57596
+ if (platformAdmin) {
57597
+ await db2.query(
57598
+ distExports$1.sql.type(voidSelectSchema)`
57599
+ update "PlatformAdmins"
57600
+ set "owner" = false
57601
+ where "id" = ${platformAdmin.id}
57602
+ `
57603
+ );
57604
+ if (opts.verbose) {
57605
+ console.log("Removed the user’s owner privileges on the platform");
57606
+ }
57607
+ } else {
57608
+ console.log("This user is not an admin on this platform");
57609
+ }
57610
+ }
57611
+ if (typeof active === "boolean") {
57612
+ if (platformAdmin) {
57613
+ await db2.query(
57614
+ distExports$1.sql.type(voidSelectSchema)`
57615
+ update "PlatformAdmins"
57616
+ set "active" = ${active}
57617
+ where "id" = ${platformAdmin.id}
57618
+ `
57619
+ );
57620
+ } else {
57621
+ if (active) {
57622
+ await db2.one(
57623
+ distExports$1.sql.type(voidSelectSchema)`
57624
+ insert into "PlatformAdmins" ("id", "platformId", "userId", "owner", "active")
57625
+ values (${cleanSmallId()}, ${platformId}, ${userId}, false, ${active})
57626
+ `
57627
+ );
57628
+ } else {
57629
+ console.log("This user is not an admin on this platform");
57630
+ }
57631
+ }
57632
+ }
57633
+ if (revokeAllPermissions) {
57634
+ if (platformAdmin) {
57635
+ await revokeAllUserPlatformPermissions({ platformAdminId: platformAdmin.id }, db2);
57636
+ console.log("Revoked all platform permissions of user");
57637
+ } else {
57638
+ console.log("This user is not an admin on this platform");
57639
+ }
57640
+ }
57478
57641
  });
57479
57642
  } catch (e) {
57480
57643
  if (e instanceof distExports$1.UniqueIntegrityConstraintViolationError) {
@@ -57735,5 +57898,5 @@ extDev.command("init-db").description(
57735
57898
  "--extension-id <string>",
57736
57899
  "the ID of the extension, can be set with an EXTENSION_ID environment variable instead"
57737
57900
  ).option("--db-name <string>", "a custom name for the database, applicable only for testing").action(withOptionsAndEnv(initDb));
57738
- program.command("edit-admin").description("edit a platform admin user").requiredOption("--platform-id <string>", "the ID of the platform to edit").requiredOption("--user-id <string>", "the ID of the admin user to edit").option("--owner", "grants the user full permission to manage everything on the platform").option("--revoke-all-permissions", "revokes all permission of the user on their platform").action(withOptionsAndEnv(editAdmin));
57901
+ program.command("edit-admin").description("edit a platform admin user or make a user a platform admin").requiredOption("--platform-id <string>", "the ID of the platform to edit").requiredOption("--user-id <string>", "the ID of the admin user to edit").option("--owner", "grants the user full permission to manage everything on the platform").option("--no-owner", "removes owner privileges on the platform").option("--active", "activates or deactivates the user’s admin access on the platform").option("--no-active", "deactivates the user’s admin access on the platform").option("--revoke-all-permissions", "revokes all permission of the user on their platform").action(withOptionsAndEnv(editAdmin));
57739
57902
  await program.parseAsync();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirechunk/cli",
3
- "version": "0.0.1-rc.3",
3
+ "version": "0.0.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {
@@ -67,8 +67,7 @@ export const createUser = async (
67
67
 
68
68
  let platformId: string | null | undefined = opts.platformId;
69
69
  let orgId: string | null | undefined = opts.orgId;
70
- // TODO
71
- // let orgPrimary = false;
70
+ let orgPrimary = false;
72
71
 
73
72
  try {
74
73
  const user = await db.transaction(async (db) => {
@@ -115,7 +114,7 @@ export const createUser = async (
115
114
  if (opts.verbose) {
116
115
  console.log(`Created org ID ${orgId}`);
117
116
  }
118
- // orgPrimary = true;
117
+ orgPrimary = true;
119
118
  }
120
119
 
121
120
  const user = await db.one(
@@ -124,9 +123,13 @@ export const createUser = async (
124
123
  )`insert into "Users" ("platformId", "email", "emailVerified", "password", "passwordStatus", "orgId", "role", "status", "firstName", "lastName") values (${platformId}, ${email}, ${opts.emailVerified}, ${password}, 'Ok', ${orgId}, ${role}, ${status}, ${firstName}, ${lastName}) returning "id"`,
125
124
  );
126
125
 
127
- // if (opts.admin) {
128
- // await grantAllUserPlatformPermissions({ userId: user.id, platformId }, db);
129
- // }
126
+ if (orgPrimary) {
127
+ await db.maybeOne(
128
+ sql.type(
129
+ voidSelectSchema,
130
+ )`update "Orgs" set "primaryUserId" = ${user.id} where "id" = ${orgId}`,
131
+ );
132
+ }
130
133
 
131
134
  return user;
132
135
  });
@@ -1,19 +1,31 @@
1
+ import { cleanSmallId } from '@wirechunk/lib/clean-small-id.ts';
1
2
  import { createPool, sql, UniqueIntegrityConstraintViolationError } from 'slonik';
2
3
  import { z } from 'zod';
3
4
  import type { Env } from '../env.ts';
4
5
  import { requireCoreDbUrl } from '../env.ts';
5
6
  import { detailedUniqueIntegrityConstraintViolationError } from '../errors.ts';
6
7
  import type { WithGlobalOptions } from '../global-options.ts';
8
+ import {
9
+ grantAllUserPlatformPermissions,
10
+ revokeAllUserPlatformPermissions,
11
+ } from '../users/permissions.ts';
12
+ import { voidSelectSchema } from '../util.ts';
7
13
 
8
- const findUserSchema = z.object({
14
+ const findPlatformAdminSchema = z.object({
9
15
  id: z.string(),
10
16
  platformId: z.string(),
17
+ active: z.boolean(),
18
+ });
19
+
20
+ const findUserSchema = z.object({
21
+ id: z.string(),
11
22
  });
12
23
 
13
24
  type EditAdminOptions = {
14
25
  platformId: string;
15
26
  userId: string;
16
27
  owner?: boolean;
28
+ active?: boolean;
17
29
  revokeAllPermissions?: boolean;
18
30
  };
19
31
 
@@ -22,7 +34,7 @@ export const editAdmin = async (
22
34
  env: Env,
23
35
  ): Promise<void> => {
24
36
  const db = await createPool(requireCoreDbUrl(env));
25
- const { platformId, userId, owner, revokeAllPermissions } = opts;
37
+ const { platformId, userId, owner, active, revokeAllPermissions } = opts;
26
38
 
27
39
  if (owner && revokeAllPermissions) {
28
40
  console.error(
@@ -33,9 +45,9 @@ export const editAdmin = async (
33
45
 
34
46
  try {
35
47
  await db.transaction(async (db) => {
36
- const platformAdmin = await db.maybeOne(
48
+ let platformAdmin = await db.maybeOne(
37
49
  sql.type(
38
- findUserSchema,
50
+ findPlatformAdminSchema,
39
51
  )`select "id" from "PlatformAdmins" where "platformId" = ${platformId} and "userId" = ${userId}`,
40
52
  );
41
53
  if (!platformAdmin) {
@@ -46,17 +58,67 @@ export const editAdmin = async (
46
58
  throw new Error(`User with ID ${userId} not found`);
47
59
  }
48
60
  }
49
-
50
- // TODO
51
-
52
- // if (owner) {
53
- // await grantAllUserPlatformPermissions({ userId: id, platformId: user.platformId }, db);
54
- // console.log('Set user as a platform admin');
55
- // }
56
- // if (revokeAllPermissions) {
57
- // await revokeAllUserPlatformPermissions({ userId: id }, db);
58
- // console.log('Revoked all platform permissions of user');
59
- // }
61
+ if (owner) {
62
+ if (!platformAdmin) {
63
+ platformAdmin = await db.one(
64
+ sql.type(findPlatformAdminSchema)`
65
+ insert into "PlatformAdmins" ("id", "platformId", "userId", "owner", "active")
66
+ values (${cleanSmallId()}, ${platformId}, ${userId}, ${active ?? true}, true)
67
+ returning "id", "platformId", "active"
68
+ `,
69
+ );
70
+ }
71
+ await grantAllUserPlatformPermissions({ platformAdminId: platformAdmin.id }, db);
72
+ if (opts.verbose) {
73
+ console.log('Set the user as an owner on the platform');
74
+ }
75
+ } else if (owner === false) {
76
+ if (platformAdmin) {
77
+ await db.query(
78
+ sql.type(voidSelectSchema)`
79
+ update "PlatformAdmins"
80
+ set "owner" = false
81
+ where "id" = ${platformAdmin.id}
82
+ `,
83
+ );
84
+ if (opts.verbose) {
85
+ console.log('Removed the user’s owner privileges on the platform');
86
+ }
87
+ } else {
88
+ console.log('This user is not an admin on this platform');
89
+ }
90
+ }
91
+ if (typeof active === 'boolean') {
92
+ if (platformAdmin) {
93
+ await db.query(
94
+ sql.type(voidSelectSchema)`
95
+ update "PlatformAdmins"
96
+ set "active" = ${active}
97
+ where "id" = ${platformAdmin.id}
98
+ `,
99
+ );
100
+ } else {
101
+ if (active) {
102
+ // Automatically create a platform admin.
103
+ await db.one(
104
+ sql.type(voidSelectSchema)`
105
+ insert into "PlatformAdmins" ("id", "platformId", "userId", "owner", "active")
106
+ values (${cleanSmallId()}, ${platformId}, ${userId}, false, ${active})
107
+ `,
108
+ );
109
+ } else {
110
+ console.log('This user is not an admin on this platform');
111
+ }
112
+ }
113
+ }
114
+ if (revokeAllPermissions) {
115
+ if (platformAdmin) {
116
+ await revokeAllUserPlatformPermissions({ platformAdminId: platformAdmin.id }, db);
117
+ console.log('Revoked all platform permissions of user');
118
+ } else {
119
+ console.log('This user is not an admin on this platform');
120
+ }
121
+ }
60
122
  });
61
123
  } catch (e) {
62
124
  if (e instanceof UniqueIntegrityConstraintViolationError) {
package/src/main.ts CHANGED
@@ -118,10 +118,13 @@ extDev
118
118
 
119
119
  program
120
120
  .command('edit-admin')
121
- .description('edit a platform admin user')
121
+ .description('edit a platform admin user or make a user a platform admin')
122
122
  .requiredOption('--platform-id <string>', 'the ID of the platform to edit')
123
123
  .requiredOption('--user-id <string>', 'the ID of the admin user to edit')
124
124
  .option('--owner', 'grants the user full permission to manage everything on the platform')
125
+ .option('--no-owner', 'removes owner privileges on the platform')
126
+ .option('--active', 'activates or deactivates the user’s admin access on the platform')
127
+ .option('--no-active', 'deactivates the user’s admin access on the platform')
125
128
  .option('--revoke-all-permissions', 'revokes all permission of the user on their platform')
126
129
  .action(withOptionsAndEnv(editAdmin));
127
130
 
@@ -1,3 +1,4 @@
1
+ import { cleanSmallId } from '@wirechunk/lib/clean-small-id.ts';
1
2
  import { Permission } from '@wirechunk/lib/graphql-api-enums.ts';
2
3
  import type { CommonQueryMethods } from 'slonik';
3
4
  import { sql } from 'slonik';
@@ -7,33 +8,35 @@ export const allPermissions = Object.values(Permission);
7
8
 
8
9
  export const revokeAllUserPlatformPermissions = async (
9
10
  {
10
- userId,
11
+ platformAdminId,
11
12
  }: {
12
- userId: string;
13
+ platformAdminId: string;
13
14
  },
14
15
  db: CommonQueryMethods,
15
16
  ): Promise<void> => {
16
17
  await db.query(
17
- sql.type(voidSelectSchema)`delete from "UserPlatformPermissions" where "userId" = ${userId}`,
18
+ sql.type(
19
+ voidSelectSchema,
20
+ )`delete from "PlatformAdminPermissions" where "id" = ${platformAdminId}`,
18
21
  );
19
22
  };
20
23
 
21
24
  export const grantAllUserPlatformPermissions = async (
22
25
  {
23
- userId,
24
- platformId,
26
+ platformAdminId,
25
27
  }: {
26
- userId: string;
27
- platformId: string;
28
+ platformAdminId: string;
28
29
  },
29
30
  db: CommonQueryMethods,
30
31
  ): Promise<void> => {
31
32
  await db.query(
32
33
  sql.type(
33
34
  voidSelectSchema,
34
- )`insert into "UserPlatformPermissions" ("userId", "platformId", "permission") values ${sql.join(
35
- allPermissions.map((permission) => sql.fragment`(${userId}, ${platformId}, ${permission})`),
35
+ )`insert into "PlatformAdminPermissions" ("id", "platformAdminId", "permission") values ${sql.join(
36
+ allPermissions.map(
37
+ (permission) => sql.fragment`(${cleanSmallId()}, ${platformAdminId}, ${permission})`,
38
+ ),
36
39
  sql.fragment`,`,
37
- )} on conflict on constraint "UserPlatformPermissions_pkey" do nothing`,
40
+ )} on conflict ("platformAdminId", "permission") do nothing`,
38
41
  );
39
42
  };