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.

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