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.

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