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.

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