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.

172 lines
6.6 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. var baseMenu []system.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 := CasbinServiceApp.GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId)
  48. err = CasbinServiceApp.UpdateCasbin(copyInfo.Authority.AuthorityId, paths)
  49. if err != nil {
  50. _ = authorityService.DeleteAuthority(&copyInfo.Authority)
  51. }
  52. return err, copyInfo.Authority
  53. }
  54. //@author: [piexlmax](https://github.com/piexlmax)
  55. //@function: UpdateAuthority
  56. //@description: 更改一个角色
  57. //@param: auth model.SysAuthority
  58. //@return: err error, authority model.SysAuthority
  59. func (authorityService *AuthorityService) UpdateAuthority(auth system.SysAuthority) (err error, authority system.SysAuthority) {
  60. err = global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Updates(&auth).Error
  61. return err, auth
  62. }
  63. //@author: [piexlmax](https://github.com/piexlmax)
  64. //@function: DeleteAuthority
  65. //@description: 删除角色
  66. //@param: auth *model.SysAuthority
  67. //@return: err error
  68. func (authorityService *AuthorityService) DeleteAuthority(auth *system.SysAuthority) (err error) {
  69. if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&system.SysUser{}).Error, gorm.ErrRecordNotFound) {
  70. return errors.New("此角色有用户正在使用禁止删除")
  71. }
  72. if !errors.Is(global.GVA_DB.Where("parent_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
  73. return errors.New("此角色存在子角色不允许删除")
  74. }
  75. db := global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth)
  76. err = db.Unscoped().Delete(auth).Error
  77. if len(auth.SysBaseMenus) > 0 {
  78. err = global.GVA_DB.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus)
  79. //err = db.Association("SysBaseMenus").Delete(&auth)
  80. } else {
  81. err = db.Error
  82. }
  83. err = global.GVA_DB.Delete(&[]system.SysUseAuthority{}, "sys_authority_authority_id = ?", auth.AuthorityId).Error
  84. CasbinServiceApp.ClearCasbin(0, auth.AuthorityId)
  85. return err
  86. }
  87. //@author: [piexlmax](https://github.com/piexlmax)
  88. //@function: GetAuthorityInfoList
  89. //@description: 分页获取数据
  90. //@param: info request.PageInfo
  91. //@return: err error, list interface{}, total int64
  92. func (authorityService *AuthorityService) GetAuthorityInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  93. limit := info.PageSize
  94. offset := info.PageSize * (info.Page - 1)
  95. db := global.GVA_DB
  96. var authority []system.SysAuthority
  97. err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
  98. if len(authority) > 0 {
  99. for k := range authority {
  100. err = authorityService.findChildrenAuthority(&authority[k])
  101. }
  102. }
  103. return err, authority, total
  104. }
  105. //@author: [piexlmax](https://github.com/piexlmax)
  106. //@function: GetAuthorityInfo
  107. //@description: 获取所有角色信息
  108. //@param: auth model.SysAuthority
  109. //@return: err error, sa model.SysAuthority
  110. func (authorityService *AuthorityService) GetAuthorityInfo(auth system.SysAuthority) (err error, sa system.SysAuthority) {
  111. err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
  112. return err, sa
  113. }
  114. //@author: [piexlmax](https://github.com/piexlmax)
  115. //@function: SetDataAuthority
  116. //@description: 设置角色资源权限
  117. //@param: auth model.SysAuthority
  118. //@return: error
  119. func (authorityService *AuthorityService) SetDataAuthority(auth system.SysAuthority) error {
  120. var s system.SysAuthority
  121. global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
  122. err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)
  123. return err
  124. }
  125. //@author: [piexlmax](https://github.com/piexlmax)
  126. //@function: SetMenuAuthority
  127. //@description: 菜单与角色绑定
  128. //@param: auth *model.SysAuthority
  129. //@return: error
  130. func (authorityService *AuthorityService) SetMenuAuthority(auth *system.SysAuthority) error {
  131. var s system.SysAuthority
  132. global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
  133. err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
  134. return err
  135. }
  136. //@author: [piexlmax](https://github.com/piexlmax)
  137. //@function: findChildrenAuthority
  138. //@description: 查询子角色
  139. //@param: authority *model.SysAuthority
  140. //@return: err error
  141. func (authorityService *AuthorityService) findChildrenAuthority(authority *system.SysAuthority) (err error) {
  142. err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  143. if len(authority.Children) > 0 {
  144. for k := range authority.Children {
  145. err = authorityService.findChildrenAuthority(&authority.Children[k])
  146. }
  147. }
  148. return err
  149. }