1
0
Fork 0
mirror of synced 2024-10-02 01:56:57 +13:00

Remove user within the app on deletion

This commit is contained in:
Adria Navarro 2023-01-12 16:28:02 +00:00
parent 09b4533cc8
commit 09570e26f6
2 changed files with 31 additions and 0 deletions

View file

@ -188,6 +188,10 @@ const validateUniqueUser = async (email: string, tenantId: string) => {
} }
} }
function instanceOfUser(user: User | ThirdPartyUser): user is User {
return !!(user as User).roles
}
export const save = async ( export const save = async (
user: User | ThirdPartyUser, user: User | ThirdPartyUser,
opts: SaveUserOpts = {} opts: SaveUserOpts = {}
@ -257,6 +261,17 @@ export const save = async (
} }
} }
let appsToRemove: string[] = []
if (dbUser && instanceOfUser(user)) {
const newRoles = Object.keys(user.roles)
const existingRoles = Object.keys(dbUser.roles)
appsToRemove = existingRoles.filter(r => !newRoles.includes(r))
if (appsToRemove.length) {
console.log("Deleting access to apps", { appsToRemove })
}
}
try { try {
// save the user to db // save the user to db
let response = await db.put(builtUser) let response = await db.put(builtUser)
@ -265,6 +280,11 @@ export const save = async (
await eventHelpers.handleSaveEvents(builtUser, dbUser) await eventHelpers.handleSaveEvents(builtUser, dbUser)
await addTenant(tenantId, _id, email) await addTenant(tenantId, _id, email)
await cache.user.invalidateUser(response.id) await cache.user.invalidateUser(response.id)
for (const appId of appsToRemove) {
await apps.removeUserFromApp(_id, appId)
}
// let server know to sync user // let server know to sync user
await apps.syncUserInApps(_id) await apps.syncUserInApps(_id)

View file

@ -30,3 +30,14 @@ export async function syncUserInApps(userId: string) {
throw "Unable to sync user." throw "Unable to sync user."
} }
} }
export async function removeUserFromApp(userId: string, appId: string) {
const response = await makeAppRequest(
`/api/users/metadata/${userId}/app/${appId}`,
"DELETE",
undefined
)
if (response && response.status !== 200) {
throw "Unable to delete user from app."
}
}