fix : role deletion problem (#208)

This commit is contained in:
zhangwenjian committed 2020-08-21 15:07:03 +08:00
1 parent 2c8c7688b4
commit b9d3483ba3
3 files changed
+56 -17

No files matched your search

+13 -13
View File
@@ -2,7 +2,6 @@ package system
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"go-admin/models"
"go-admin/tools"
"go-admin/tools/app"
@@ -75,14 +74,17 @@ func GetRole(c *gin.Context) {
func InsertRole(c *gin.Context) {
var data models.SysRole
data.CreateBy = tools.GetUserIdStr(c)
err := c.BindWith(&data, binding.JSON)
tools.HasError(err, "", 500)
err := c.Bind(&data)
tools.HasError(err, "数据解析失败", 500)
id, err := data.Insert()
tools.HasError(err, "", -1)
data.RoleId = id
tools.HasError(err, "", -1)
var t models.RoleMenu
_, err = t.Insert(id, data.MenuIds)
tools.HasError(err, "", -1)
if len(data.MenuIds)>0 {
_, err = t.Insert(id, data.MenuIds)
tools.HasError(err, "", -1)
}
app.OK(c, data, "添加成功")
}
@@ -104,10 +106,11 @@ func UpdateRole(c *gin.Context) {
tools.HasError(err, "", -1)
var t models.RoleMenu
_, err = t.DeleteRoleMenu(data.RoleId)
tools.HasError(err, "添加失败1", -1)
_, err2 := t.Insert(data.RoleId, data.MenuIds)
tools.HasError(err2, "添加失败2", -1)
tools.HasError(err, "修改失败(delete rm)", -1)
if len(data.MenuIds)>0 {
_, err2 := t.Insert(data.RoleId, data.MenuIds)
tools.HasError(err2, "修改失败(insert)", -1)
}
app.OK(c, result, "修改成功")
}
@@ -142,8 +145,5 @@ func DeleteRole(c *gin.Context) {
IDS := tools.IdsStrToIdsIntGroup("roleId", c)
_, err := Role.BatchDelete(IDS)
tools.HasError(err, "删除失败1", -1)
var t models.RoleMenu
_, err = t.BatchDeleteRoleMenu(IDS)
tools.HasError(err, "删除失败1", -1)
app.OK(c, "", "删除成功")
}
+42 -4
View File
@@ -167,9 +167,47 @@ func (role *SysRole) Update(id int) (update SysRole, err error) {
//批量删除
func (role *SysRole) BatchDelete(id []int) (Result bool, err error) {
if err = orm.Eloquent.Table(role.TableName()).Where("role_id in (?)", id).Delete(SysRole{}).Error; err != nil {
return
tx := orm.Eloquent.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
if err := tx.Error; err != nil {
return false, err
}
Result = true
return
// 查询角色
var roles []SysRole
if err := tx.Table("sys_role").Where("role_id in (?)", id).Find(&roles).Error; err != nil {
tx.Rollback()
return false, err
}
// 删除角色
if err = tx.Table(role.TableName()).Where("role_id in (?)", id).Unscoped().Delete(&SysRole{}).Error; err != nil {
tx.Rollback()
return false, err
}
// 删除角色菜单
if err := tx.Table("sys_role_menu").Where("role_id in (?)", id).Delete(&RoleMenu{}).Error; err != nil {
tx.Rollback()
return false, err
}
// 删除casbin配置
for i := 0; i < len(roles); i++ {
if err := tx.Table("casbin_rule").Where("v0 in (?)", id).Delete(&CasbinRule{}).Error; err != nil {
tx.Rollback()
return false, err
}
}
if err := tx.Commit().Error; err != nil {
return false, err
}
return true, nil
}
+1
View File
@@ -102,6 +102,7 @@ func (rm *RoleMenu) DeleteRoleMenu(roleId int) (bool, error) {
}
// 该方法即将弃用
func (rm *RoleMenu) BatchDeleteRoleMenu(roleIds []int) (bool, error) {
tx := orm.Eloquent.Begin()
defer func() {