@7365admin1/core 3.107.0 → 3.109.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.
package/dist/index.mjs CHANGED
@@ -4735,6 +4735,22 @@ import {
4735
4735
  useCache as useCache8
4736
4736
  } from "@7365admin1/node-server-utils";
4737
4737
  import { ObjectId as ObjectId17 } from "mongodb";
4738
+
4739
+ // src/utils/invite-origin.util.ts
4740
+ var SEVEN365_INVITE_ORIGIN = "seven365";
4741
+ function seven365InviteQuery() {
4742
+ return {
4743
+ $or: [
4744
+ { "metadata.origin": SEVEN365_INVITE_ORIGIN },
4745
+ {
4746
+ "metadata.origin": { $exists: false },
4747
+ "metadata.org": { $in: ["", null] }
4748
+ }
4749
+ ]
4750
+ };
4751
+ }
4752
+
4753
+ // src/repositories/verification.repo.ts
4738
4754
  function useVerificationRepo() {
4739
4755
  const db2 = useAtlas11.getDb();
4740
4756
  if (!db2) {
@@ -4821,7 +4837,8 @@ function useVerificationRepo() {
4821
4837
  type = {},
4822
4838
  email = "",
4823
4839
  siteId = "",
4824
- orgId = ""
4840
+ orgId = "",
4841
+ origin = ""
4825
4842
  }) {
4826
4843
  page = page > 0 ? page - 1 : 0;
4827
4844
  const query2 = { status };
@@ -4866,6 +4883,10 @@ function useVerificationRepo() {
4866
4883
  query2["metadata.org"] = { $in: orgCandidates };
4867
4884
  cacheOptions.orgId = orgId;
4868
4885
  }
4886
+ if (origin === SEVEN365_INVITE_ORIGIN) {
4887
+ Object.assign(query2, seven365InviteQuery());
4888
+ cacheOptions.origin = origin;
4889
+ }
4869
4890
  const cacheKey = makeCacheKey6(namespace_collection3, cacheOptions);
4870
4891
  const cachedData = await getCache(cacheKey);
4871
4892
  if (cachedData) {
@@ -13004,6 +13025,23 @@ async function requireRoleInvitable(role, org, apps, site) {
13004
13025
  if (refusal)
13005
13026
  throw new BadRequestError33(refusal);
13006
13027
  }
13028
+ async function requireInviteOrigin(req, org, role, fromConsole = false) {
13029
+ if (!org) {
13030
+ await requirePlatformStaff(req);
13031
+ return { origin: SEVEN365_INVITE_ORIGIN };
13032
+ }
13033
+ if (!role)
13034
+ throw new BadRequestError33("Choose a role for this invitation.");
13035
+ if (fromConsole) {
13036
+ const staff = await requirePlatformStaff(req).then(
13037
+ () => true,
13038
+ () => false
13039
+ );
13040
+ if (staff)
13041
+ return { origin: SEVEN365_INVITE_ORIGIN };
13042
+ }
13043
+ return {};
13044
+ }
13007
13045
  async function requireInviteReach(req, org, app, role, site) {
13008
13046
  const apps = Array.isArray(app) ? app : [app];
13009
13047
  if (apps.some((a) => a === PLATFORM_STAFF_MEMBER_TYPE)) {
@@ -26670,6 +26708,7 @@ function useVerificationController() {
26670
26708
  const siteName = req.body.siteName ?? "";
26671
26709
  try {
26672
26710
  await requireInviteReach(req, org, app, role, siteId);
26711
+ const origin = await requireInviteOrigin(req, org, role);
26673
26712
  await _createUserInvite({
26674
26713
  email,
26675
26714
  metadata: {
@@ -26678,7 +26717,8 @@ function useVerificationController() {
26678
26717
  name,
26679
26718
  org,
26680
26719
  siteId,
26681
- siteName
26720
+ siteName,
26721
+ ...origin
26682
26722
  }
26683
26723
  });
26684
26724
  res.status(201).json({ message: "Successfully invited user." });
@@ -26771,6 +26811,7 @@ function useVerificationController() {
26771
26811
  clientModules.residentAppModules = residentAppModules;
26772
26812
  }
26773
26813
  await requireInviteReach(req, org, app, role, siteId);
26814
+ const origin = await requireInviteOrigin(req, org, role, true);
26774
26815
  const verificationIds = await _createSimpleUserInvite({
26775
26816
  email,
26776
26817
  metadata: {
@@ -26780,7 +26821,8 @@ function useVerificationController() {
26780
26821
  org,
26781
26822
  siteId,
26782
26823
  siteName,
26783
- ...clientModules
26824
+ ...clientModules,
26825
+ ...origin
26784
26826
  }
26785
26827
  });
26786
26828
  const responseBody = {
@@ -26836,6 +26878,7 @@ function useVerificationController() {
26836
26878
  const siteName = req.body.siteName ?? "";
26837
26879
  try {
26838
26880
  await requireInviteReach(req, org, app, role, siteId);
26881
+ const origin = await requireInviteOrigin(req, org, role);
26839
26882
  await _createSimpleMemberInvite({
26840
26883
  email,
26841
26884
  metadata: {
@@ -26844,7 +26887,8 @@ function useVerificationController() {
26844
26887
  name,
26845
26888
  org,
26846
26889
  siteId,
26847
- siteName
26890
+ siteName,
26891
+ ...origin
26848
26892
  }
26849
26893
  });
26850
26894
  const responseBody = {
@@ -27014,6 +27058,8 @@ function useVerificationController() {
27014
27058
  email: Joi29.string().optional().allow("", null),
27015
27059
  siteId: Joi29.string().hex().optional().allow("", null),
27016
27060
  orgId: Joi29.string().hex().optional().allow("", null),
27061
+ // "seven365": only the invitations Seven365 sent (the console's lists).
27062
+ origin: Joi29.string().valid(SEVEN365_INVITE_ORIGIN).optional().allow("", null),
27017
27063
  limit: Joi29.number().integer().min(1).allow("", null).default(10)
27018
27064
  });
27019
27065
  const query2 = { ...req.query };
@@ -27031,6 +27077,7 @@ function useVerificationController() {
27031
27077
  const siteId = req.query.siteId ?? "";
27032
27078
  const orgId = req.query.orgId ?? "";
27033
27079
  const limit = parseInt(req.query.limit ?? "10");
27080
+ const origin = req.query.origin ?? "";
27034
27081
  const type = req.query.type ? String(req.query.type).split(",") : "";
27035
27082
  const typeObj = {};
27036
27083
  if (type && Array.isArray(type) && type.length) {
@@ -27039,8 +27086,9 @@ function useVerificationController() {
27039
27086
  });
27040
27087
  }
27041
27088
  try {
27042
- await requireVerificationListReach(req, orgId);
27089
+ await requireVerificationListReach(req, origin ? null : orgId);
27043
27090
  const data = await _getVerifications({
27091
+ origin,
27044
27092
  search,
27045
27093
  page,
27046
27094
  status,
@@ -38068,17 +38116,49 @@ function siteOwnerOnlyMessage(ownerName) {
38068
38116
  async function requireSiteOwnerEdit(actor, staff, siteId) {
38069
38117
  if (staff)
38070
38118
  return;
38119
+ const ownerOrg = await ownerOfSite(siteId);
38120
+ if (mayEditSiteDetails({ staff, actorOrgIds: actor.orgIds, ownerOrg }))
38121
+ return;
38122
+ throw new AppError18(siteOwnerOnlyMessage(await ownerOrgName(ownerOrg)), 403);
38123
+ }
38124
+ async function ownerOfSite(siteId) {
38071
38125
  const id = String(siteId?.toString?.() ?? "");
38072
38126
  const db2 = useAtlas60.getDb();
38073
38127
  const site = db2 && ObjectId79.isValid(id) ? await db2.collection("sites").findOne({ _id: new ObjectId79(id) }, { projection: { orgId: 1, org: 1 } }) : null;
38074
- const ownerOrg = site?.orgId ?? site?.org;
38075
- if (mayEditSiteDetails({ staff, actorOrgIds: actor.orgIds, ownerOrg }))
38128
+ return site?.orgId ?? site?.org;
38129
+ }
38130
+ async function ownerOrgName(ownerOrg) {
38131
+ if (!ownerOrg || !ObjectId79.isValid(String(ownerOrg)))
38132
+ return void 0;
38133
+ const owner = await useAtlas60.getDb()?.collection("organizations").findOne({ _id: new ObjectId79(String(ownerOrg)) }, { projection: { name: 1 } });
38134
+ return owner?.name;
38135
+ }
38136
+ var DELETE_SITE_PERMISSION = "sites:delete-site";
38137
+ var NO_SITE_DELETE_PERMISSION = "You do not have permission to delete this site.";
38138
+ function siteDeleteOwnerOnlyMessage(ownerName) {
38139
+ const name = String(ownerName ?? "").trim() || "another organisation";
38140
+ return `This site belongs to ${name}. Only they can delete it. You can remove it from your Sites list instead.`;
38141
+ }
38142
+ function mayDeleteSite(permissions) {
38143
+ return permissions.includes("*") || permissions.includes(DELETE_SITE_PERMISSION);
38144
+ }
38145
+ async function requireSiteOwnerDelete(actor, staff, siteId) {
38146
+ if (staff)
38076
38147
  return;
38077
- const owner = ownerOrg && ObjectId79.isValid(String(ownerOrg)) ? await db2?.collection("organizations").findOne(
38078
- { _id: new ObjectId79(String(ownerOrg)) },
38079
- { projection: { name: 1 } }
38080
- ) : null;
38081
- throw new AppError18(siteOwnerOnlyMessage(owner?.name), 403);
38148
+ const owner = String(await ownerOfSite(siteId) ?? "");
38149
+ if (owner && ObjectId79.isValid(owner) && ObjectId79.isValid(actor.id)) {
38150
+ const member = await useAtlas60.getDb()?.collection("members").findOne(
38151
+ { user: new ObjectId79(actor.id), org: new ObjectId79(owner), type: "organization", status: "active" },
38152
+ { projection: { role: 1 } }
38153
+ );
38154
+ if (member) {
38155
+ const permissions = member.role ? await getUserPermissions({ user: actor.id, org: owner, role: member.role }) : [];
38156
+ if (mayDeleteSite(permissions))
38157
+ return;
38158
+ throw new AppError18(NO_SITE_DELETE_PERMISSION, 403);
38159
+ }
38160
+ }
38161
+ throw new AppError18(siteDeleteOwnerOnlyMessage(await ownerOrgName(owner)), 403);
38082
38162
  }
38083
38163
 
38084
38164
  // src/controllers/site.controller.ts
@@ -38389,7 +38469,8 @@ function useSiteController() {
38389
38469
  return;
38390
38470
  }
38391
38471
  try {
38392
- await requireSiteWrite(req, _id);
38472
+ const actor = await requireSiteWrite(req, _id);
38473
+ await requireSiteOwnerDelete(actor, staffMayBypass(actor), _id);
38393
38474
  await _deleteSite(_id);
38394
38475
  res.json({ message: "Successfully deleted site." });
38395
38476
  return;
@@ -93577,6 +93658,7 @@ function useVerificationControllerV2() {
93577
93658
  const { email, app, role, name, org, siteId, siteName } = value;
93578
93659
  try {
93579
93660
  await requireInviteReach(req, org, app, role, siteId);
93661
+ const origin = await requireInviteOrigin(req, org, role);
93580
93662
  await _createUserInvite({
93581
93663
  email,
93582
93664
  metadata: {
@@ -93585,7 +93667,8 @@ function useVerificationControllerV2() {
93585
93667
  name,
93586
93668
  org,
93587
93669
  siteId,
93588
- siteName
93670
+ siteName,
93671
+ ...origin
93589
93672
  }
93590
93673
  });
93591
93674
  res.status(201).json({ message: "Successfully invited user." });