Remove user within the app on deletion
This commit is contained in:
parent
09b4533cc8
commit
09570e26f6
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -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."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue