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.

186 lines
6.7 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
  111. var authority []system.SysAuthority
  112. err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = 0").Find(&authority).Error
  113. if len(authority) > 0 {
  114. for k := range authority {
  115. err = authorityService.findChildrenAuthority(&authority[k])
  116. }
  117. }
  118. return err, authority, total
  119. }
  120. //@author: [piexlmax](https://github.com/piexlmax)
  121. //@function: GetAuthorityInfo
  122. //@description: 获取所有角色信息
  123. //@param: auth model.SysAuthority
  124. //@return: err error, sa model.SysAuthority
  125. func (authorityService *AuthorityService) GetAuthorityInfo(auth system.SysAuthority) (err error, sa system.SysAuthority) {
  126. err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
  127. return err, sa
  128. }
  129. //@author: [piexlmax](https://github.com/piexlmax)
  130. //@function: SetDataAuthority
  131. //@description: 设置角色资源权限
  132. //@param: auth model.SysAuthority
  133. //@return: error
  134. func (authorityService *AuthorityService) SetDataAuthority(auth system.SysAuthority) error {
  135. var s system.SysAuthority
  136. global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
  137. err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)
  138. return err
  139. }
  140. //@author: [piexlmax](https://github.com/piexlmax)
  141. //@function: SetMenuAuthority
  142. //@description: 菜单与角色绑定
  143. //@param: auth *model.SysAuthority
  144. //@return: error
  145. func (authorityService *AuthorityService) SetMenuAuthority(auth *system.SysAuthority) error {
  146. var s system.SysAuthority
  147. global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
  148. err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
  149. return err
  150. }
  151. //@author: [piexlmax](https://github.com/piexlmax)
  152. //@function: findChildrenAuthority
  153. //@description: 查询子角色
  154. //@param: authority *model.SysAuthority
  155. //@return: err error
  156. func (authorityService *AuthorityService) findChildrenAuthority(authority *system.SysAuthority) (err error) {
  157. err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  158. if len(authority.Children) > 0 {
  159. for k := range authority.Children {
  160. err = authorityService.findChildrenAuthority(&authority.Children[k])
  161. }
  162. }
  163. return err
  164. }