@7365admin1/core 3.99.1-staging.384 → 3.99.1-staging.385

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.js CHANGED
@@ -39134,17 +39134,49 @@ function siteOwnerOnlyMessage(ownerName) {
39134
39134
  async function requireSiteOwnerEdit(actor, staff, siteId) {
39135
39135
  if (staff)
39136
39136
  return;
39137
+ const ownerOrg = await ownerOfSite(siteId);
39138
+ if (mayEditSiteDetails({ staff, actorOrgIds: actor.orgIds, ownerOrg }))
39139
+ return;
39140
+ throw new import_node_server_utils113.AppError(siteOwnerOnlyMessage(await ownerOrgName(ownerOrg)), 403);
39141
+ }
39142
+ async function ownerOfSite(siteId) {
39137
39143
  const id = String(siteId?.toString?.() ?? "");
39138
39144
  const db2 = import_node_server_utils113.useAtlas.getDb();
39139
39145
  const site = db2 && import_mongodb79.ObjectId.isValid(id) ? await db2.collection("sites").findOne({ _id: new import_mongodb79.ObjectId(id) }, { projection: { orgId: 1, org: 1 } }) : null;
39140
- const ownerOrg = site?.orgId ?? site?.org;
39141
- if (mayEditSiteDetails({ staff, actorOrgIds: actor.orgIds, ownerOrg }))
39146
+ return site?.orgId ?? site?.org;
39147
+ }
39148
+ async function ownerOrgName(ownerOrg) {
39149
+ if (!ownerOrg || !import_mongodb79.ObjectId.isValid(String(ownerOrg)))
39150
+ return void 0;
39151
+ const owner = await import_node_server_utils113.useAtlas.getDb()?.collection("organizations").findOne({ _id: new import_mongodb79.ObjectId(String(ownerOrg)) }, { projection: { name: 1 } });
39152
+ return owner?.name;
39153
+ }
39154
+ var DELETE_SITE_PERMISSION = "sites:delete-site";
39155
+ var NO_SITE_DELETE_PERMISSION = "You do not have permission to delete this site.";
39156
+ function siteDeleteOwnerOnlyMessage(ownerName) {
39157
+ const name = String(ownerName ?? "").trim() || "another organisation";
39158
+ return `This site belongs to ${name}. Only they can delete it. You can remove it from your Sites list instead.`;
39159
+ }
39160
+ function mayDeleteSite(permissions) {
39161
+ return permissions.includes("*") || permissions.includes(DELETE_SITE_PERMISSION);
39162
+ }
39163
+ async function requireSiteOwnerDelete(actor, staff, siteId) {
39164
+ if (staff)
39142
39165
  return;
39143
- const owner = ownerOrg && import_mongodb79.ObjectId.isValid(String(ownerOrg)) ? await db2?.collection("organizations").findOne(
39144
- { _id: new import_mongodb79.ObjectId(String(ownerOrg)) },
39145
- { projection: { name: 1 } }
39146
- ) : null;
39147
- throw new import_node_server_utils113.AppError(siteOwnerOnlyMessage(owner?.name), 403);
39166
+ const owner = String(await ownerOfSite(siteId) ?? "");
39167
+ if (owner && import_mongodb79.ObjectId.isValid(owner) && import_mongodb79.ObjectId.isValid(actor.id)) {
39168
+ const member = await import_node_server_utils113.useAtlas.getDb()?.collection("members").findOne(
39169
+ { user: new import_mongodb79.ObjectId(actor.id), org: new import_mongodb79.ObjectId(owner), type: "organization", status: "active" },
39170
+ { projection: { role: 1 } }
39171
+ );
39172
+ if (member) {
39173
+ const permissions = member.role ? await getUserPermissions({ user: actor.id, org: owner, role: member.role }) : [];
39174
+ if (mayDeleteSite(permissions))
39175
+ return;
39176
+ throw new import_node_server_utils113.AppError(NO_SITE_DELETE_PERMISSION, 403);
39177
+ }
39178
+ }
39179
+ throw new import_node_server_utils113.AppError(siteDeleteOwnerOnlyMessage(await ownerOrgName(owner)), 403);
39148
39180
  }
39149
39181
 
39150
39182
  // src/controllers/site.controller.ts
@@ -39455,7 +39487,8 @@ function useSiteController() {
39455
39487
  return;
39456
39488
  }
39457
39489
  try {
39458
- await requireSiteWrite(req, _id);
39490
+ const actor = await requireSiteWrite(req, _id);
39491
+ await requireSiteOwnerDelete(actor, staffMayBypass(actor), _id);
39459
39492
  await _deleteSite(_id);
39460
39493
  res.json({ message: "Successfully deleted site." });
39461
39494
  return;