chore: Warn user about default role of SAML before deleting role

This commit is contained in:
Faruk AYDIN
2023-08-07 15:21:32 +02:00
parent 8101c9f0bc
commit 43eea965c5

View File

@@ -1,4 +1,5 @@
import Role from '../../models/role';
import SamlAuthProvider from '../../models/saml-auth-provider.ee';
import Context from '../../types/express/context';
type Params = {
@@ -14,14 +15,8 @@ const deleteRole = async (
) => {
context.currentUser.can('delete', 'Role');
const role = await Role
.query()
.findById(params.input.id)
.throwIfNotFound();
const count = await role
.$relatedQuery('users')
.resultSize();
const role = await Role.query().findById(params.input.id).throwIfNotFound();
const count = await role.$relatedQuery('users').resultSize();
if (count > 0) {
throw new Error('All users must be migrated away from the role!');
@@ -31,6 +26,17 @@ const deleteRole = async (
throw new Error('Admin role cannot be deleted!');
}
const samlAuthProviderUsingDefaultRole = await SamlAuthProvider.query()
.where({ default_role_id: role.id })
.limit(1)
.first();
if (samlAuthProviderUsingDefaultRole) {
throw new Error(
'You need to change the default role in the SAML configuration before deleting this role.'
);
}
// delete permissions first
await role.$relatedQuery('permissions').delete();
await role.$query().delete();