fix: Correctly guard against last admin deleting their account (#2069)

* fix: Correctly guard against last admin deleting their account

* test
This commit is contained in:
Tom Moor
2021-04-24 20:52:46 -07:00
committed by GitHub
parent 3fbb3a2403
commit 2d22399bbc
5 changed files with 163 additions and 34 deletions

View File

@ -233,22 +233,6 @@ const removeIdentifyingInfo = async (model, options) => {
await model.save({ hooks: false, transaction: options.transaction });
};
const checkLastAdmin = async (model) => {
const teamId = model.teamId;
if (model.isAdmin) {
const userCount = await User.count({ where: { teamId } });
const adminCount = await User.count({ where: { isAdmin: true, teamId } });
if (userCount > 1 && adminCount <= 1) {
throw new ValidationError(
"Cannot delete account as only admin. Please transfer admin permissions to another user and try again."
);
}
}
};
User.beforeDestroy(checkLastAdmin);
User.beforeDestroy(removeIdentifyingInfo);
User.beforeSave(uploadAvatar);
User.beforeCreate(setRandomJwtSecret);