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.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
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. var AuthorityServiceApp = new(AuthorityService)
  18. func (authorityService *AuthorityService) CreateAuthority(auth system.SysAuthority) (err error, authority system.SysAuthority) {
  19. var authorityBox system.SysAuthority
  20. if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
  21. return errors.New("存在相同角色id"), auth
  22. }
  23. err = global.GVA_DB.Create(&auth).Error
  24. return err, auth
  25. }
  26. //@author: [piexlmax](https://github.com/piexlmax)
  27. //@function: CopyAuthority
  28. //@description: 复制一个角色
  29. //@param: copyInfo response.SysAuthorityCopyResponse
  30. //@return: err error, authority model.SysAuthority
  31. func (authorityService *AuthorityService) CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, authority system.SysAuthority) {
  32. var authorityBox system.SysAuthority
  33. if !errors.Is(global.GVA_DB.Where("authority_id = ?", copyInfo.Authority.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
  34. return errors.New("存在相同角色id"), authority
  35. }
  36. copyInfo.Authority.Children = []system.SysAuthority{}
  37. err, menus := MenuServiceApp.GetMenuAuthority(&request.GetAuthorityId{AuthorityId: copyInfo.OldAuthorityId})
  38. if err != nil {
  39. return
  40. }
  41. var baseMenu []system.SysBaseMenu
  42. for _, v := range menus {
  43. intNum, _ := strconv.Atoi(v.MenuId)
  44. v.SysBaseMenu.ID = uint(intNum)
  45. baseMenu = append(baseMenu, v.SysBaseMenu)
  46. }
  47. copyInfo.Authority.SysBaseMenus = baseMenu
  48. err = global.GVA_DB.Create(&copyInfo.Authority).Error
  49. if err != nil {
  50. return
  51. }
  52. paths := CasbinServiceApp.GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId)
  53. err = CasbinServiceApp.UpdateCasbin(copyInfo.Authority.AuthorityId, paths)
  54. if err != nil {
  55. _ = authorityService.DeleteAuthority(&copyInfo.Authority)
  56. }
  57. return err, copyInfo.Authority
  58. }
  59. //@author: [piexlmax](https://github.com/piexlmax)
  60. //@function: UpdateAuthority
  61. //@description: 更改一个角色
  62. //@param: auth model.SysAuthority
  63. //@return: err error, authority model.SysAuthority
  64. func (authorityService *AuthorityService) UpdateAuthority(auth system.SysAuthority) (err error, authority system.SysAuthority) {
  65. err = global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Updates(&auth).Error
  66. return err, auth
  67. }
  68. //@author: [piexlmax](https://github.com/piexlmax)
  69. //@function: DeleteAuthority
  70. //@description: 删除角色
  71. //@param: auth *model.SysAuthority
  72. //@return: err error
  73. func (authorityService *AuthorityService) DeleteAuthority(auth *system.SysAuthority) (err error) {
  74. if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&system.SysUser{}).Error, gorm.ErrRecordNotFound) {
  75. return errors.New("此角色有用户正在使用禁止删除")
  76. }
  77. if !errors.Is(global.GVA_DB.Where("parent_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
  78. return errors.New("此角色存在子角色不允许删除")
  79. }
  80. db := global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth)
  81. err = db.Unscoped().Delete(auth).Error
  82. if err != nil {
  83. return
  84. }
  85. if len(auth.SysBaseMenus) > 0 {
  86. err = global.GVA_DB.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus)
  87. if err != nil {
  88. return
  89. }
  90. // err = db.Association("SysBaseMenus").Delete(&auth)
  91. } else {
  92. err = db.Error
  93. if err != nil {
  94. return
  95. }
  96. }
  97. err = global.GVA_DB.Delete(&[]system.SysUseAuthority{}, "sys_authority_authority_id = ?", auth.AuthorityId).Error
  98. CasbinServiceApp.ClearCasbin(0, auth.AuthorityId)
  99. return err
  100. }
  101. //@author: [piexlmax](https://github.com/piexlmax)
  102. //@function: GetAuthorityInfoList
  103. //@description: 分页获取数据
  104. //@param: info request.PageInfo
  105. //@return: err error, list interface{}, total int64
  106. func (authorityService *AuthorityService) GetAuthorityInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  107. limit := info.PageSize
  108. offset := info.PageSize * (info.Page - 1)
  109. db := global.GVA_DB.Model(&system.SysAuthority{})
  110. err = db.Where("parent_id = ?", "0").Count(&total).Error
  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. }