You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.1 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. )
  8. // @title CreateAuthority
  9. // @description 创建一个角色
  10. // @auth (2020/04/05 20:22)
  11. // @param auth model.SysAuthority
  12. // @return error
  13. // @return authority model.SysAuthority
  14. func CreateAuthority(auth model.SysAuthority) (err error, authority model.SysAuthority) {
  15. err = global.GVA_DB.Create(&auth).Error
  16. return err, auth
  17. }
  18. // @title DeleteAuthority
  19. // @description 删除角色
  20. // @auth (2020/04/05 20:22)
  21. // @param auth model.SysAuthority
  22. // @return error
  23. // 删除角色
  24. func DeleteAuthority(auth model.SysAuthority) (err error) {
  25. err = global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).Find(&model.SysUser{}).Error
  26. if err == nil {
  27. err = errors.New("此角色有用户正在使用禁止删除")
  28. return
  29. }
  30. err = global.GVA_DB.Where("parent_id = ?", auth.AuthorityId).Find(&model.SysAuthority{}).Error
  31. if err == nil {
  32. err = errors.New("此角色存在子角色不允许删除")
  33. return
  34. }
  35. db := global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth).Unscoped().Delete(auth)
  36. if len(auth.SysBaseMenus) > 0 {
  37. err = db.Association("SysBaseMenus").Delete(auth.SysBaseMenus).Error
  38. } else {
  39. err = db.Error
  40. }
  41. ClearCasbin(0, auth.AuthorityId)
  42. return err
  43. }
  44. // @title GetInfoList
  45. // @description 删除文件切片记录
  46. // @auth (2020/04/05 20:22)
  47. // @param info request.PaveInfo
  48. // @return error
  49. // 分页获取数据
  50. func GetAuthorityInfoList(info request.PageInfo) (err error, list interface{}, total int) {
  51. limit := info.PageSize
  52. offset := info.PageSize * (info.Page - 1)
  53. db := global.GVA_DB
  54. var authority []model.SysAuthority
  55. err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
  56. if len(authority) > 0 {
  57. for k, _ := range authority {
  58. err = findChildrenAuthority(&authority[k])
  59. }
  60. }
  61. return err, authority, total
  62. }
  63. // @title GetAuthorityInfo
  64. // @description 获取所有角色信息
  65. // @auth (2020/04/05 20:22)
  66. // @param auth model.SysAuthority
  67. // @return error
  68. // @param authority model.SysAuthority
  69. func GetAuthorityInfo(auth model.SysAuthority) (err error, sa model.SysAuthority) {
  70. err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
  71. return err, sa
  72. }
  73. // @title SetDataAuthority
  74. // @description 设置角色资源权限
  75. // @auth (2020/04/05 20:22)
  76. // @param auth model.SysAuthority
  77. // @return error
  78. func SetDataAuthority(auth model.SysAuthority) error {
  79. var s model.SysAuthority
  80. global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
  81. err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId).Error
  82. return err
  83. }
  84. // @title SetMenuAuthority
  85. // @description 菜单与角色绑定
  86. // @auth (2020/04/05 20:22)
  87. // @param auth *model.SysAuthority
  88. // @return error
  89. func SetMenuAuthority(auth *model.SysAuthority) error {
  90. var s model.SysAuthority
  91. global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
  92. err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus).Error
  93. return err
  94. }
  95. // @title findChildrenAuthority
  96. // @description 查询子角色
  97. // @auth (2020/04/05 20:22)
  98. // @param auth *model.SysAuthority
  99. // @return error
  100. func findChildrenAuthority(authority *model.SysAuthority) (err error) {
  101. err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  102. if len(authority.Children) > 0 {
  103. for k, _ := range authority.Children {
  104. err = findChildrenAuthority(&authority.Children[k])
  105. }
  106. }
  107. return err
  108. }