Prevent changing active status of last admin user (#13309)

This commit is contained in:
Azri Kahar
2022-05-16 23:21:15 +08:00
committed by GitHub
parent 8d064fc646
commit a8908577dd

View File

@@ -120,6 +120,26 @@ export class UsersService extends ItemsService {
}
}
/**
* Make sure there's at least one active admin user when updating user status
*/
private async checkRemainingActiveAdmin(excludeKeys: PrimaryKey[]): Promise<void> {
const otherAdminUsers = await this.knex
.count('*', { as: 'count' })
.from('directus_users')
.whereNotIn('directus_users.id', excludeKeys)
.andWhere({ 'directus_roles.admin_access': true })
.andWhere({ 'directus_users.status': 'active' })
.leftJoin('directus_roles', 'directus_users.role', 'directus_roles.id')
.first();
const otherAdminUsersCount = +(otherAdminUsers?.count || 0);
if (otherAdminUsersCount === 0) {
throw new UnprocessableEntityException(`You can't change the active status of the last admin user.`);
}
}
/**
* Create a new user
*/
@@ -177,6 +197,10 @@ export class UsersService extends ItemsService {
}
}
if (data.status !== 'active') {
await this.checkRemainingActiveAdmin(keys);
}
if (data.email) {
if (keys.length > 1) {
throw new RecordNotUniqueException('email', {