@fonderie/workspaces 5.3.0 → 5.3.2

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/dist/index.cjs CHANGED
@@ -210,13 +210,21 @@ async function getUserRoles(userId, workspaceId, store) {
210
210
  );
211
211
  }
212
212
  async function addRoleToMember(userId, workspaceId, roleId, store) {
213
- await store.query(
213
+ const rows = await store.query(
214
214
  `INSERT INTO fonderie_role_user_workspaces (user_id, workspace_id, role_id, confirmed)
215
- VALUES ($1, $2, $3, true)
215
+ SELECT $1, $2, $3, true
216
+ WHERE EXISTS (
217
+ SELECT 1 FROM fonderie_roles r
218
+ WHERE r.id = $3
219
+ AND r.workspace_id = $2
220
+ AND r.is_system = false
221
+ )
216
222
  ON CONFLICT (user_id, workspace_id, role_id) DO UPDATE
217
- SET confirmed = true, removed = false, suspended = false`,
223
+ SET confirmed = true, removed = false, suspended = false
224
+ RETURNING user_id`,
218
225
  [userId, workspaceId, roleId]
219
226
  );
227
+ return rows.length > 0;
220
228
  }
221
229
  async function removeRoleFromMember(userId, workspaceId, roleId, store) {
222
230
  const remaining = await store.query(
@@ -548,10 +556,11 @@ async function findSystemRole(name2, store) {
548
556
  );
549
557
  return row ?? null;
550
558
  }
551
- async function getRoleById(id, store) {
559
+ async function getRoleById(id, workspaceId, store) {
552
560
  const [row] = await store.query(
553
- `SELECT ${SELECT_ROLE} FROM fonderie_roles WHERE id = $1`,
554
- [id]
561
+ `SELECT ${SELECT_ROLE} FROM fonderie_roles
562
+ WHERE id = $1 AND (workspace_id = $2 OR is_system = true)`,
563
+ [id, workspaceId]
555
564
  );
556
565
  return row ?? null;
557
566
  }
@@ -564,9 +573,9 @@ async function listWorkspaceRoles(workspaceId, store) {
564
573
  [workspaceId]
565
574
  );
566
575
  }
567
- async function updateRole(id, opts, store) {
576
+ async function updateRole(id, workspaceId, opts, store) {
568
577
  const sets = [];
569
- const params = [id];
578
+ const params = [id, workspaceId];
570
579
  if (opts.name !== void 0) {
571
580
  params.push(opts.name);
572
581
  sets.push(`name = $${params.length}`);
@@ -579,11 +588,11 @@ async function updateRole(id, opts, store) {
579
588
  params.push(opts.active);
580
589
  sets.push(`active = $${params.length}`);
581
590
  }
582
- if (sets.length === 0) return getRoleById(id, store);
591
+ if (sets.length === 0) return getRoleById(id, workspaceId, store);
583
592
  const [row] = await store.query(
584
593
  `UPDATE fonderie_roles
585
594
  SET ${sets.join(", ")}
586
- WHERE id = $1 AND is_system = false
595
+ WHERE id = $1 AND workspace_id = $2 AND is_system = false
587
596
  RETURNING ${SELECT_ROLE}`,
588
597
  params
589
598
  );
@@ -649,14 +658,14 @@ var RoleModel = class {
649
658
  findSystem(name2) {
650
659
  return findSystemRole(name2, this.store);
651
660
  }
652
- findById(id) {
653
- return getRoleById(id, this.store);
661
+ findById(id, workspaceId) {
662
+ return getRoleById(id, workspaceId, this.store);
654
663
  }
655
664
  list(workspaceId) {
656
665
  return listWorkspaceRoles(workspaceId, this.store);
657
666
  }
658
- update(id, opts) {
659
- return updateRole(id, opts, this.store);
667
+ update(id, workspaceId, opts) {
668
+ return updateRole(id, workspaceId, opts, this.store);
660
669
  }
661
670
  delete(id, workspaceId) {
662
671
  return deleteRole(id, workspaceId, this.store);
@@ -928,7 +937,14 @@ function memberController(store) {
928
937
  return (0, import_core3.setApiResponse)(import_core3.HTTP.UNPROCESSABLE, "INVALID_PARAMETER", "userId is required");
929
938
  if (!roleId)
930
939
  return (0, import_core3.setApiResponse)(import_core3.HTTP.UNPROCESSABLE, "INVALID_PARAMETER", "roleId is required");
931
- await members.addRole(userId, ctx.workspace.id, roleId);
940
+ const assigned = await members.addRole(userId, ctx.workspace.id, roleId);
941
+ if (!assigned) {
942
+ return (0, import_core3.setApiResponse)(
943
+ import_core3.HTTP.UNPROCESSABLE,
944
+ "INVALID_ROLE",
945
+ "Role is not assignable in this workspace."
946
+ );
947
+ }
932
948
  return (0, import_core3.setApiResponse)(import_core3.HTTP.OK, "ROLE_ASSIGNED", "Role assigned successfully.");
933
949
  },
934
950
  async removeRole(ctx) {
@@ -1001,7 +1017,7 @@ function roleController(store) {
1001
1017
  if (!roleId) {
1002
1018
  return (0, import_core4.setApiResponse)(import_core4.HTTP.UNPROCESSABLE, "INVALID_PARAMETER", "roleId is required");
1003
1019
  }
1004
- const role = await roles.findById(roleId);
1020
+ const role = await roles.findById(roleId, ctx.workspace.id);
1005
1021
  if (!role) {
1006
1022
  return (0, import_core4.setApiResponse)(import_core4.HTTP.NOT_FOUND, "NOT_FOUND", "Role not found");
1007
1023
  }
@@ -1032,7 +1048,7 @@ function roleController(store) {
1032
1048
  if (typeof body?.["active"] === "boolean") {
1033
1049
  opts.active = body["active"];
1034
1050
  }
1035
- const role = await roles.update(roleId, opts);
1051
+ const role = await roles.update(roleId, ctx.workspace.id, opts);
1036
1052
  if (!role) {
1037
1053
  return (0, import_core4.setApiResponse)(import_core4.HTTP.NOT_FOUND, "NOT_FOUND", "Role not found or is a system role");
1038
1054
  }
@@ -1061,7 +1077,7 @@ function roleController(store) {
1061
1077
  if (!roleId) {
1062
1078
  return (0, import_core4.setApiResponse)(import_core4.HTTP.UNPROCESSABLE, "INVALID_PARAMETER", "roleId is required");
1063
1079
  }
1064
- const role = await roles.findById(roleId);
1080
+ const role = await roles.findById(roleId, ctx.workspace.id);
1065
1081
  if (!role) {
1066
1082
  return (0, import_core4.setApiResponse)(import_core4.HTTP.NOT_FOUND, "NOT_FOUND", "Role not found");
1067
1083
  }
@@ -1080,6 +1096,10 @@ function roleController(store) {
1080
1096
  if (!roleId) {
1081
1097
  return (0, import_core4.setApiResponse)(import_core4.HTTP.UNPROCESSABLE, "INVALID_PARAMETER", "roleId is required");
1082
1098
  }
1099
+ const target = await roles.findById(roleId, ctx.workspace.id);
1100
+ if (!target || target.isSystem) {
1101
+ return (0, import_core4.setApiResponse)(import_core4.HTTP.NOT_FOUND, "NOT_FOUND", "Role not found");
1102
+ }
1083
1103
  const perms = body?.["permissions"];
1084
1104
  if (!Array.isArray(perms)) {
1085
1105
  return (0, import_core4.setApiResponse)(