删除角色时,检查是否有用户绑定

This commit is contained in:
linwenxiang committed 2020-09-03 11:51:25 +08:00
1 parent 4b8c372038
commit 3a9831fc8c
2 files changed
+11 -1

No files matched your search

+1 -1
View File
@@ -145,6 +145,6 @@ func DeleteRole(c *gin.Context) {
IDS := tools.IdsStrToIdsIntGroup("roleId", c)
_, err := Role.BatchDelete(IDS)
tools.HasError(err, "删除失败1", -1)
tools.HasError(err, "删除失败", -1)
app.OK(c, "", "删除成功")
}
+10
View File
@@ -191,6 +191,16 @@ func (role *SysRole) BatchDelete(id []int) (Result bool, err error) {
return false, err
}
var count int64
if err := tx.Table("sys_user").Where("role_id in (?)", id).Count(&count).Error; err != nil {
tx.Rollback()
return false, err
}
if count > 0 {
tx.Rollback()
return false, errors.New("存在绑定用户,请解绑后重试")
}
// 删除角色
if err = tx.Table(role.TableName()).Where("role_id in (?)", id).Unscoped().Delete(&SysRole{}).Error; err != nil {
tx.Rollback()