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.

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