Remove users that should not be there anymore when syncGlobalUsers

This commit is contained in:
adrinr 2023-03-29 15:35:55 +01:00
parent 0257617ba1
commit e25429632b
1 changed files with 14 additions and 6 deletions

View File

@ -6,25 +6,33 @@ import {
InternalTables, InternalTables,
} from "../../db/utils" } from "../../db/utils"
import { isEqual } from "lodash" 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 // skip users with no access
if ( if (
user.roleId == null || user.roleId == null ||
user.roleId === rolesCore.BUILTIN_ROLE_IDS.PUBLIC 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 return null
} }
delete user._rev delete user._rev
const metadataId = generateUserMetadataID(user._id)
const newDoc = { const newDoc = {
...user, ...user,
_id: metadataId, _id: metadataId,
tableId: InternalTables.USER_METADATA, 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 // copy rev over for the purposes of equality check
if (found) { if (found) {
newDoc._rev = found._rev newDoc._rev = found._rev
@ -58,7 +66,7 @@ export async function syncGlobalUsers() {
]) ])
const toWrite = [] const toWrite = []
for (let user of users) { for (let user of users) {
const combined = await combineMetadataAndUser(user, metadata) const combined = combineMetadataAndUser(user, metadata)
if (combined) { if (combined) {
toWrite.push(combined) toWrite.push(combined)
} }