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.

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