mirror of
https://github.com/StarFleetCPTN/GoMFT.git
synced 2026-09-08 15:41:20 +02:00
fix: Update admin user deletion logic to prevent deletion of the last administrator
- Changed the logic in the HandleDeleteUser function to check for other administrators using the actual 'is_admin' column instead of counting all admins. - Prevented deletion if no other administrators exist, ensuring at least one admin remains in the system. https://github.com/StarFleetCPTN/GoMFT/issues/67
This commit is contained in:
@@ -1181,15 +1181,18 @@ func (h *Handlers) HandleDeleteUser(c *gin.Context) {
|
||||
|
||||
// Check if this is an admin user
|
||||
if user.GetIsAdmin() {
|
||||
// Count how many admins there are
|
||||
var adminCount int64
|
||||
if err := h.DB.Model(&db.User{}).Where("metadata->>'is_admin' = 'true'").Count(&adminCount).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check admin count"})
|
||||
// Check if other administrators exist
|
||||
var otherAdminCount int64
|
||||
// Use the actual 'is_admin' column, comparing against true
|
||||
if err := h.DB.Model(&db.User{}).
|
||||
Where("is_admin = ? AND id != ?", true, user.ID).
|
||||
Count(&otherAdminCount).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check for other administrators"})
|
||||
return
|
||||
}
|
||||
|
||||
// If this is the last admin, prevent deletion
|
||||
if adminCount <= 1 {
|
||||
// If no other administrators exist, prevent deletion
|
||||
if otherAdminCount == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Cannot delete the last administrator"})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user