@budibase/backend-core 2.33.3 → 2.33.5

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
@@ -67267,32 +67267,13 @@ __export(tenancy_exports, {
67267
67267
  addTenantToUrl: () => addTenantToUrl,
67268
67268
  getTenantDB: () => getTenantDB,
67269
67269
  getTenantIDFromCtx: () => getTenantIDFromCtx,
67270
- getTenantInfo: () => getTenantInfo,
67271
- isUserInAppTenant: () => isUserInAppTenant,
67272
- saveTenantInfo: () => saveTenantInfo
67270
+ isUserInAppTenant: () => isUserInAppTenant
67273
67271
  });
67274
67272
 
67275
67273
  // src/tenancy/db.ts
67276
67274
  function getTenantDB(tenantId) {
67277
67275
  return getDB(getGlobalDBName(tenantId));
67278
67276
  }
67279
- async function saveTenantInfo(tenantInfo) {
67280
- const db = getTenantDB(tenantInfo.tenantId);
67281
- return db.put({
67282
- _id: "tenant_info",
67283
- ...tenantInfo
67284
- });
67285
- }
67286
- async function getTenantInfo(tenantId) {
67287
- try {
67288
- const db = getTenantDB(tenantId);
67289
- const tenantInfo = await db.get("tenant_info");
67290
- delete tenantInfo.owner.password;
67291
- return tenantInfo;
67292
- } catch {
67293
- return void 0;
67294
- }
67295
- }
67296
67277
 
67297
67278
  // src/tenancy/tenancy.ts
67298
67279
  function addTenantToUrl(url) {
@@ -69124,8 +69105,9 @@ __export(users_exports3, {
69124
69105
  bulkUpdateGlobalUsers: () => bulkUpdateGlobalUsers,
69125
69106
  cleanseUserObject: () => cleanseUserObject,
69126
69107
  doesUserExist: () => doesUserExist,
69127
- getAccountHolderFromUserIds: () => getAccountHolderFromUserIds,
69108
+ getAccountHolderFromUsers: () => getAccountHolderFromUsers,
69128
69109
  getAllUserIds: () => getAllUserIds,
69110
+ getAllUsers: () => getAllUsers,
69129
69111
  getById: () => getById,
69130
69112
  getCreatorCount: () => getCreatorCount,
69131
69113
  getExistingAccounts: () => getExistingAccounts,
@@ -69157,7 +69139,7 @@ __export(users_exports3, {
69157
69139
  // src/users/utils.ts
69158
69140
  var utils_exports6 = {};
69159
69141
  __export(utils_exports6, {
69160
- getAccountHolderFromUserIds: () => getAccountHolderFromUserIds,
69142
+ getAccountHolderFromUsers: () => getAccountHolderFromUsers,
69161
69143
  hasAdminPermissions: () => hasAdminPermissions2,
69162
69144
  hasAppBuilderPermissions: () => hasAppBuilderPermissions2,
69163
69145
  hasBuilderPermissions: () => hasBuilderPermissions2,
@@ -69720,7 +69702,7 @@ function getBuiltinRole(roleId) {
69720
69702
  function builtinRoleToNumber(id) {
69721
69703
  const builtins = getBuiltinRoles();
69722
69704
  const MAX = Object.values(builtins).length + 1;
69723
- if (id === BUILTIN_IDS.ADMIN || id === BUILTIN_IDS.BUILDER) {
69705
+ if (compareRoleIds(id, BUILTIN_IDS.ADMIN) || compareRoleIds(id, BUILTIN_IDS.BUILDER)) {
69724
69706
  return MAX;
69725
69707
  }
69726
69708
  let role = builtins[id], count = 0;
@@ -69750,7 +69732,9 @@ async function roleToNumber(id) {
69750
69732
  }
69751
69733
  if (Array.isArray(role.inherits)) {
69752
69734
  const highestBuiltin = role.inherits.map((roleId) => {
69753
- const foundRole = hierarchy.find((role2) => role2._id === roleId);
69735
+ const foundRole = hierarchy.find(
69736
+ (role2) => compareRoleIds(role2._id, roleId)
69737
+ );
69754
69738
  if (foundRole) {
69755
69739
  return findNumber(foundRole) + 1;
69756
69740
  }
@@ -69827,7 +69811,7 @@ async function saveRoles(roles) {
69827
69811
  }
69828
69812
  async function getAllUserRoles(userRoleId, opts) {
69829
69813
  const allRoles = await getAllRoles();
69830
- if (userRoleId === BUILTIN_IDS.ADMIN) {
69814
+ if (compareRoleIds(userRoleId, BUILTIN_IDS.ADMIN)) {
69831
69815
  return allRoles;
69832
69816
  }
69833
69817
  const foundRole = findRole(userRoleId, allRoles, opts);
@@ -69902,14 +69886,19 @@ async function getAllRoles(appId) {
69902
69886
  for (let builtinRoleId of externalBuiltinRoles) {
69903
69887
  const builtinRole = builtinRoles[builtinRoleId];
69904
69888
  const dbBuiltin = roles.filter(
69905
- (dbRole) => getExternalRoleID(dbRole._id, dbRole.version) === builtinRoleId
69889
+ (dbRole) => compareRoleIds(dbRole._id, builtinRoleId)
69906
69890
  )[0];
69907
69891
  if (dbBuiltin == null) {
69908
69892
  roles.push(builtinRole || builtinRoles.BASIC);
69909
69893
  } else {
69910
69894
  roles = roles.filter((role) => role._id !== dbBuiltin._id);
69911
- dbBuiltin._id = getExternalRoleID(dbBuiltin._id, dbBuiltin.version);
69912
- roles.push(Object.assign(builtinRole, dbBuiltin));
69895
+ dbBuiltin._id = getExternalRoleID(builtinRole._id, dbBuiltin.version);
69896
+ roles.push({
69897
+ ...builtinRole,
69898
+ ...dbBuiltin,
69899
+ name: builtinRole.name,
69900
+ _id: getExternalRoleID(builtinRole._id, builtinRole.version)
69901
+ });
69913
69902
  }
69914
69903
  }
69915
69904
  for (let role of roles) {
@@ -69940,7 +69929,7 @@ var AccessController = class {
69940
69929
  this.userHierarchies = {};
69941
69930
  }
69942
69931
  async hasAccess(tryingRoleId, userRoleId) {
69943
- if (tryingRoleId == null || tryingRoleId === "" || tryingRoleId === userRoleId || tryingRoleId === BUILTIN_IDS.BUILDER || userRoleId === BUILTIN_IDS.BUILDER) {
69932
+ if (tryingRoleId == null || tryingRoleId === "" || compareRoleIds(tryingRoleId, BUILTIN_IDS.BUILDER) || compareRoleIds(userRoleId, tryingRoleId) || compareRoleIds(userRoleId, BUILTIN_IDS.BUILDER)) {
69944
69933
  return true;
69945
69934
  }
69946
69935
  let roleIds = userRoleId ? this.userHierarchies[userRoleId] : null;
@@ -70042,17 +70031,14 @@ async function validateUniqueUser(email, tenantId) {
70042
70031
  }
70043
70032
  }
70044
70033
  }
70045
- async function getAccountHolderFromUserIds(userIds) {
70034
+ async function getAccountHolderFromUsers(users) {
70046
70035
  if (!environment_default.SELF_HOSTED && !environment_default.DISABLE_ACCOUNT_PORTAL) {
70047
- const tenantId = getTenantId();
70048
- const account = await getAccountByTenantId(tenantId);
70049
- if (!account) {
70050
- throw new Error(`Account not found for tenantId=${tenantId}`);
70051
- }
70052
- const budibaseUserId = account.budibaseUserId;
70053
- if (userIds.includes(budibaseUserId)) {
70054
- return account;
70055
- }
70036
+ const accountMetadata = await getExistingAccounts(
70037
+ users.map((user) => user.email)
70038
+ );
70039
+ return users.find(
70040
+ (user) => accountMetadata.map((metadata) => metadata.email).includes(user.email)
70041
+ );
70056
70042
  }
70057
70043
  }
70058
70044
 
@@ -70999,8 +70985,9 @@ var identifyUser = async (user, account, timestamp) => {
70999
70985
  if (isSSOUser(user)) {
71000
70986
  providerType = user.providerType;
71001
70987
  }
71002
- const accountHolder = account?.budibaseUserId === user._id || false;
71003
- const verified2 = account && account?.budibaseUserId === user._id ? account.verified : false;
70988
+ const accountHolder = await getExistingAccounts([user.email]);
70989
+ const isAccountHolder = accountHolder.length > 0;
70990
+ const verified2 = !!account && isAccountHolder && account.verified;
71004
70991
  const installationId = await getInstallationId();
71005
70992
  const hosting = account ? account.hosting : getHostingFromEnv();
71006
70993
  const environment2 = getDeploymentEnvironment();
@@ -71011,7 +70998,7 @@ var identifyUser = async (user, account, timestamp) => {
71011
70998
  installationId,
71012
70999
  tenantId,
71013
71000
  verified: verified2,
71014
- accountHolder,
71001
+ accountHolder: isAccountHolder,
71015
71002
  providerType,
71016
71003
  builder,
71017
71004
  admin,
@@ -71030,8 +71017,9 @@ var identifyAccount = async (account) => {
71030
71017
  const installationId = await getInstallationId();
71031
71018
  const environment2 = getDeploymentEnvironment();
71032
71019
  if (isCloudAccount(account)) {
71033
- if (account.budibaseUserId) {
71034
- id = account.budibaseUserId;
71020
+ const user = await getGlobalUserByEmail(account.email);
71021
+ if (user?._id) {
71022
+ id = user._id;
71035
71023
  }
71036
71024
  }
71037
71025
  const identity = {
@@ -73016,24 +73004,24 @@ var UserDB = class _UserDB {
73016
73004
  }
73017
73005
  );
73018
73006
  }
73019
- static async bulkDelete(userIds) {
73007
+ static async bulkDelete(users) {
73020
73008
  const db = getGlobalDB();
73021
73009
  const response = {
73022
73010
  successful: [],
73023
73011
  unsuccessful: []
73024
73012
  };
73025
- const account = await getAccountHolderFromUserIds(userIds);
73026
- if (account) {
73027
- userIds = userIds.filter((u) => u !== account.budibaseUserId);
73013
+ const accountHolder = await getAccountHolderFromUsers(users);
73014
+ if (accountHolder) {
73015
+ users = users.filter((u) => u.userId !== accountHolder.userId);
73028
73016
  response.unsuccessful.push({
73029
- _id: account.budibaseUserId,
73030
- email: account.email,
73017
+ _id: accountHolder.userId,
73018
+ email: accountHolder.email,
73031
73019
  reason: "Account holder cannot be deleted"
73032
73020
  });
73033
73021
  }
73034
73022
  const allDocsResponse = await db.allDocs({
73035
73023
  include_docs: true,
73036
- keys: userIds
73024
+ keys: users.map((u) => u.userId)
73037
73025
  });
73038
73026
  const usersToDelete = allDocsResponse.rows.map((user) => {
73039
73027
  return user.doc;
@@ -73180,6 +73168,16 @@ async function getAllUserIds() {
73180
73168
  });
73181
73169
  return response.rows.map((row) => row.id);
73182
73170
  }
73171
+ async function getAllUsers() {
73172
+ const db = getGlobalDB();
73173
+ const startKey = `${"us" /* USER */}${SEPARATOR}`;
73174
+ const response = await db.allDocs({
73175
+ startkey: startKey,
73176
+ endkey: `${startKey}${UNICODE_MAX}`,
73177
+ include_docs: true
73178
+ });
73179
+ return response.rows.map((row) => row.doc);
73180
+ }
73183
73181
  async function bulkUpdateGlobalUsers(users) {
73184
73182
  const db = getGlobalDB();
73185
73183
  return await db.bulkDocs(users);