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.

187 lines
6.8 KiB

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