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.

165 lines
5.8 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. "gin-vue-admin/model/response"
  8. "gorm.io/gorm"
  9. "strconv"
  10. )
  11. //@author: [piexlmax](https://github.com/piexlmax)
  12. //@function: CreateAuthority
  13. //@description: 创建一个角色
  14. //@param: auth model.SysAuthority
  15. //@return: err error, authority model.SysAuthority
  16. func CreateAuthority(auth model.SysAuthority) (err error, authority model.SysAuthority) {
  17. var authorityBox model.SysAuthority
  18. if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
  19. return errors.New("存在相同角色id"), auth
  20. }
  21. err = global.GVA_DB.Create(&auth).Error
  22. return err, auth
  23. }
  24. //@author: [piexlmax](https://github.com/piexlmax)
  25. //@function: CopyAuthority
  26. //@description: 复制一个角色
  27. //@param: copyInfo response.SysAuthorityCopyResponse
  28. //@return: err error, authority model.SysAuthority
  29. func CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, authority model.SysAuthority) {
  30. var authorityBox model.SysAuthority
  31. if !errors.Is(global.GVA_DB.Where("authority_id = ?", copyInfo.Authority.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
  32. return errors.New("存在相同角色id"), authority
  33. }
  34. copyInfo.Authority.Children = []model.SysAuthority{}
  35. err, menus := GetMenuAuthority(&request.GetAuthorityId{AuthorityId: copyInfo.OldAuthorityId})
  36. var baseMenu []model.SysBaseMenu
  37. for _, v := range menus {
  38. intNum, _ := strconv.Atoi(v.MenuId)
  39. v.SysBaseMenu.ID = uint(intNum)
  40. baseMenu = append(baseMenu, v.SysBaseMenu)
  41. }
  42. copyInfo.Authority.SysBaseMenus = baseMenu
  43. err = global.GVA_DB.Create(&copyInfo.Authority).Error
  44. paths := GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId)
  45. err = UpdateCasbin(copyInfo.Authority.AuthorityId, paths)
  46. if err != nil {
  47. _ = DeleteAuthority(&copyInfo.Authority)
  48. }
  49. return err, copyInfo.Authority
  50. }
  51. //@author: [piexlmax](https://github.com/piexlmax)
  52. //@function: UpdateAuthority
  53. //@description: 更改一个角色
  54. //@param: auth model.SysAuthority
  55. //@return: err error, authority model.SysAuthority
  56. func UpdateAuthority(auth model.SysAuthority) (err error, authority model.SysAuthority) {
  57. err = global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Updates(&auth).Error
  58. return err, auth
  59. }
  60. //@author: [piexlmax](https://github.com/piexlmax)
  61. //@function: DeleteAuthority
  62. //@description: 删除角色
  63. //@param: auth *model.SysAuthority
  64. //@return: err error
  65. func DeleteAuthority(auth *model.SysAuthority) (err error) {
  66. if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&model.SysUser{}).Error, gorm.ErrRecordNotFound) {
  67. return errors.New("此角色有用户正在使用禁止删除")
  68. }
  69. if !errors.Is(global.GVA_DB.Where("parent_id = ?", auth.AuthorityId).First(&model.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
  70. return errors.New("此角色存在子角色不允许删除")
  71. }
  72. db := global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth)
  73. err = db.Unscoped().Delete(auth).Error
  74. if len(auth.SysBaseMenus) > 0 {
  75. err = global.GVA_DB.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus)
  76. //err = db.Association("SysBaseMenus").Delete(&auth)
  77. } else {
  78. err = db.Error
  79. }
  80. ClearCasbin(0, auth.AuthorityId)
  81. return err
  82. }
  83. //@author: [piexlmax](https://github.com/piexlmax)
  84. //@function: GetAuthorityInfoList
  85. //@description: 分页获取数据
  86. //@param: info request.PageInfo
  87. //@return: err error, list interface{}, total int64
  88. func GetAuthorityInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  89. limit := info.PageSize
  90. offset := info.PageSize * (info.Page - 1)
  91. db := global.GVA_DB
  92. var authority []model.SysAuthority
  93. err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
  94. if len(authority) > 0 {
  95. for k := range authority {
  96. err = findChildrenAuthority(&authority[k])
  97. }
  98. }
  99. return err, authority, total
  100. }
  101. //@author: [piexlmax](https://github.com/piexlmax)
  102. //@function: GetAuthorityInfo
  103. //@description: 获取所有角色信息
  104. //@param: auth model.SysAuthority
  105. //@return: err error, sa model.SysAuthority
  106. func GetAuthorityInfo(auth model.SysAuthority) (err error, sa model.SysAuthority) {
  107. err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
  108. return err, sa
  109. }
  110. //@author: [piexlmax](https://github.com/piexlmax)
  111. //@function: SetDataAuthority
  112. //@description: 设置角色资源权限
  113. //@param: auth model.SysAuthority
  114. //@return: error
  115. func SetDataAuthority(auth model.SysAuthority) error {
  116. var s model.SysAuthority
  117. global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
  118. err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)
  119. return err
  120. }
  121. //@author: [piexlmax](https://github.com/piexlmax)
  122. //@function: SetMenuAuthority
  123. //@description: 菜单与角色绑定
  124. //@param: auth *model.SysAuthority
  125. //@return: error
  126. func SetMenuAuthority(auth *model.SysAuthority) error {
  127. var s model.SysAuthority
  128. global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
  129. err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
  130. return err
  131. }
  132. //@author: [piexlmax](https://github.com/piexlmax)
  133. //@function: findChildrenAuthority
  134. //@description: 查询子角色
  135. //@param: authority *model.SysAuthority
  136. //@return: err error
  137. func findChildrenAuthority(authority *model.SysAuthority) (err error) {
  138. err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  139. if len(authority.Children) > 0 {
  140. for k := range authority.Children {
  141. err = findChildrenAuthority(&authority.Children[k])
  142. }
  143. }
  144. return err
  145. }