mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Prevent changing active status of last admin user (#13309)
This commit is contained in:
@@ -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', {
|
||||
|
||||
Reference in New Issue
Block a user