flipped-aurora/gin-vue-admin · error

该用户无此角色

Error message

该用户无此角色

What it means

SetUserAuthority only allows switching a user to an authority the user already holds in the sys_user_authority join table. If no row matches sys_user_id = id AND sys_authority_authority_id = authorityId, it returns errors.New("该用户无此角色"). This is a guard ensuring the single authority_id can only be set to a role from the user's existing role set.

Source

Thrown at server/service/system/sys_user.go:154

			}
		}
	}

	err = db.Limit(limit).Offset(offset).Order(orderStr).Preload("Authorities").Preload("Authority").Preload("Departments").Preload("Positions").Preload("Dept").Find(&userList).Error
	return userList, total, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: SetUserAuthority
//@description: 设置一个用户的权限
//@param: uuid uuid.UUID, authorityId string
//@return: err error

func (userService *UserService) SetUserAuthority(ctx context.Context, id uint, authorityId uint) (err error) {

	assignErr := global.GVA_DB.WithContext(ctx).Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&system.SysUserAuthority{}).Error
	if errors.Is(assignErr, gorm.ErrRecordNotFound) {
		return errors.New("该用户无此角色")
	}

	var authority system.SysAuthority
	err = global.GVA_DB.WithContext(ctx).Where("authority_id = ?", authorityId).First(&authority).Error
	if err != nil {
		return err
	}
	var authorityMenu []system.SysAuthorityMenu
	var authorityMenuIDs []string
	err = global.GVA_DB.WithContext(ctx).Where("sys_authority_authority_id = ?", authorityId).Find(&authorityMenu).Error
	if err != nil {
		return err
	}

	for i := range authorityMenu {
		authorityMenuIDs = append(authorityMenuIDs, authorityMenu[i].MenuId)
	}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. First call SetUserAuthorities to add the target authority to the user's role set, then call SetUserAuthority to make it the active one.
  2. Re-fetch the user's authorities before rendering the switch-role UI so only held roles are offered.
  3. Confirm the authorityId passed is the role's authority_id (not menu id or row id).

Example fix

// before
err := userService.SetUserAuthority(ctx, userId, newAuthorityId) // role not yet granted
// after
if err := userService.SetUserAuthorities(ctx, adminAuthorityID, userId, []uint{newAuthorityId}); err != nil { return err }
err := userService.SetUserAuthority(ctx, userId, newAuthorityId)
Defensive patterns

Strategy: validation

Validate before calling

// only offer roles the user already holds
const switchable = userAuthorities.filter(a => heldAuthorityIds.includes(a.authorityId))

Try / catch

try {
  await setUserAuthority({ id, authorityId })
} catch (e) {
  if (e.message === '该用户无此角色') ElMessage.error('请先为用户分配该角色')
  else throw e
}

Prevention

When it happens

Trigger: Calling SetUserAuthority(sys_user.go:154) with an authorityId that is not linked to the user in system.SysUserAuthority — e.g. switching to a role never granted via SetUserAuthorities, or after that role was removed from the user.

Common situations: Front end showing role options not refreshed after roles were changed; attempting to switch to a newly created role before assigning it to the user; using a stale user detail page.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/f8d4ecacb0727651. Report an issue: GitHub.