diff --git a/packages/server/src/sdk/users/utils.ts b/packages/server/src/sdk/users/utils.ts index 5c369754a1..82a46acfac 100644 --- a/packages/server/src/sdk/users/utils.ts +++ b/packages/server/src/sdk/users/utils.ts @@ -6,25 +6,33 @@ import { InternalTables, } from "../../db/utils" import { isEqual } from "lodash" +import { ContextUser, UserMetadata } from "@budibase/types" -export function combineMetadataAndUser(user: any, metadata: any) { +export function combineMetadataAndUser( + user: ContextUser, + metadata: UserMetadata | UserMetadata[] +) { + const metadataId = generateUserMetadataID(user._id!) + const found = Array.isArray(metadata) + ? metadata.find(doc => doc._id === metadataId) + : metadata // skip users with no access if ( user.roleId == null || user.roleId === rolesCore.BUILTIN_ROLE_IDS.PUBLIC ) { + // If it exists and it should not, we must remove it + if (found) { + return { ...found, _deleted: true } + } return null } delete user._rev - const metadataId = generateUserMetadataID(user._id) const newDoc = { ...user, _id: metadataId, tableId: InternalTables.USER_METADATA, } - const found = Array.isArray(metadata) - ? metadata.find(doc => doc._id === metadataId) - : metadata // copy rev over for the purposes of equality check if (found) { newDoc._rev = found._rev @@ -58,7 +66,7 @@ export async function syncGlobalUsers() { ]) const toWrite = [] for (let user of users) { - const combined = await combineMetadataAndUser(user, metadata) + const combined = combineMetadataAndUser(user, metadata) if (combined) { toWrite.push(combined) }