sys_authority.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package system
  2. import (
  3. "errors"
  4. "strconv"
  5. "gorm.io/gorm"
  6. "log-server/global"
  7. "log-server/model/common/request"
  8. "log-server/model/system"
  9. "log-server/model/system/response"
  10. )
  11. var ErrRoleExistence = errors.New("存在相同角色id")
  12. //@author: [piexlmax](https://github.com/piexlmax)
  13. //@function: CreateAuthority
  14. //@description: 创建一个角色
  15. //@param: auth model.SysAuthority
  16. //@return: authority system.SysAuthority, err error
  17. type AuthorityService struct{}
  18. var AuthorityServiceApp = new(AuthorityService)
  19. func (authorityService *AuthorityService) CreateAuthority(auth system.SysAuthority) (authority system.SysAuthority, err error) {
  20. var authorityBox system.SysAuthority
  21. if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
  22. return auth, ErrRoleExistence
  23. }
  24. err = global.GVA_DB.Create(&auth).Error
  25. return auth, err
  26. }
  27. //@author: [piexlmax](https://github.com/piexlmax)
  28. //@function: CopyAuthority
  29. //@description: 复制一个角色
  30. //@param: copyInfo response.SysAuthorityCopyResponse
  31. //@return: authority system.SysAuthority, err error
  32. func (authorityService *AuthorityService) CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (authority system.SysAuthority, err error) {
  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 authority, ErrRoleExistence
  36. }
  37. copyInfo.Authority.Children = []system.SysAuthority{}
  38. menus, err := 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. var btns []system.SysAuthorityBtn
  54. err = global.GVA_DB.Find(&btns, "authority_id = ?", copyInfo.OldAuthorityId).Error
  55. if err != nil {
  56. return
  57. }
  58. if len(btns) > 0 {
  59. for i := range btns {
  60. btns[i].AuthorityId = copyInfo.Authority.AuthorityId
  61. }
  62. err = global.GVA_DB.Create(&btns).Error
  63. if err != nil {
  64. return
  65. }
  66. }
  67. paths := CasbinServiceApp.GetPolicyPathByAuthorityId(copyInfo.OldAuthorityId)
  68. err = CasbinServiceApp.UpdateCasbin(copyInfo.Authority.AuthorityId, paths)
  69. if err != nil {
  70. _ = authorityService.DeleteAuthority(&copyInfo.Authority)
  71. }
  72. return copyInfo.Authority, err
  73. }
  74. //@author: [piexlmax](https://github.com/piexlmax)
  75. //@function: UpdateAuthority
  76. //@description: 更改一个角色
  77. //@param: auth model.SysAuthority
  78. //@return: authority system.SysAuthority, err error
  79. func (authorityService *AuthorityService) UpdateAuthority(auth system.SysAuthority) (authority system.SysAuthority, err error) {
  80. err = global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Updates(&auth).Error
  81. return auth, err
  82. }
  83. //@author: [piexlmax](https://github.com/piexlmax)
  84. //@function: DeleteAuthority
  85. //@description: 删除角色
  86. //@param: auth *model.SysAuthority
  87. //@return: err error
  88. func (authorityService *AuthorityService) DeleteAuthority(auth *system.SysAuthority) (err error) {
  89. if errors.Is(global.GVA_DB.Debug().Preload("Users").First(&auth).Error, gorm.ErrRecordNotFound) {
  90. return errors.New("该角色不存在")
  91. }
  92. if len(auth.Users) != 0 {
  93. return errors.New("此角色有用户正在使用禁止删除")
  94. }
  95. if !errors.Is(global.GVA_DB.Where("authority_id = ?", auth.AuthorityId).First(&system.SysUser{}).Error, gorm.ErrRecordNotFound) {
  96. return errors.New("此角色有用户正在使用禁止删除")
  97. }
  98. if !errors.Is(global.GVA_DB.Where("parent_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
  99. return errors.New("此角色存在子角色不允许删除")
  100. }
  101. db := global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", auth.AuthorityId).First(auth)
  102. err = db.Unscoped().Delete(auth).Error
  103. if err != nil {
  104. return
  105. }
  106. if len(auth.SysBaseMenus) > 0 {
  107. err = global.GVA_DB.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus)
  108. if err != nil {
  109. return
  110. }
  111. // err = db.Association("SysBaseMenus").Delete(&auth)
  112. } else {
  113. err = db.Error
  114. if err != nil {
  115. return
  116. }
  117. }
  118. err = global.GVA_DB.Delete(&[]system.SysUseAuthority{}, "sys_authority_authority_id = ?", auth.AuthorityId).Error
  119. err = global.GVA_DB.Delete(&[]system.SysAuthorityBtn{}, "authority_id = ?", auth.AuthorityId).Error
  120. authorityId := strconv.Itoa(int(auth.AuthorityId))
  121. CasbinServiceApp.ClearCasbin(0, authorityId)
  122. return err
  123. }
  124. //@author: [piexlmax](https://github.com/piexlmax)
  125. //@function: GetAuthorityInfoList
  126. //@description: 分页获取数据
  127. //@param: info request.PageInfo
  128. //@return: list interface{}, total int64, err error
  129. func (authorityService *AuthorityService) GetAuthorityInfoList(info request.PageInfo) (list interface{}, total int64, err error) {
  130. limit := info.PageSize
  131. offset := info.PageSize * (info.Page - 1)
  132. db := global.GVA_DB.Model(&system.SysAuthority{})
  133. err = db.Where("parent_id = ?", "0").Count(&total).Error
  134. var authority []system.SysAuthority
  135. err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = ?", "0").Find(&authority).Error
  136. if len(authority) > 0 {
  137. for k := range authority {
  138. err = authorityService.findChildrenAuthority(&authority[k])
  139. }
  140. }
  141. return authority, total, err
  142. }
  143. //@author: [piexlmax](https://github.com/piexlmax)
  144. //@function: GetAuthorityInfo
  145. //@description: 获取所有角色信息
  146. //@param: auth model.SysAuthority
  147. //@return: sa system.SysAuthority, err error
  148. func (authorityService *AuthorityService) GetAuthorityInfo(auth system.SysAuthority) (sa system.SysAuthority, err error) {
  149. err = global.GVA_DB.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
  150. return sa, err
  151. }
  152. //@author: [piexlmax](https://github.com/piexlmax)
  153. //@function: SetDataAuthority
  154. //@description: 设置角色资源权限
  155. //@param: auth model.SysAuthority
  156. //@return: error
  157. func (authorityService *AuthorityService) SetDataAuthority(auth system.SysAuthority) error {
  158. var s system.SysAuthority
  159. global.GVA_DB.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
  160. err := global.GVA_DB.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)
  161. return err
  162. }
  163. //@author: [piexlmax](https://github.com/piexlmax)
  164. //@function: SetMenuAuthority
  165. //@description: 菜单与角色绑定
  166. //@param: auth *model.SysAuthority
  167. //@return: error
  168. func (authorityService *AuthorityService) SetMenuAuthority(auth *system.SysAuthority) error {
  169. var s system.SysAuthority
  170. global.GVA_DB.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
  171. err := global.GVA_DB.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
  172. return err
  173. }
  174. //@author: [piexlmax](https://github.com/piexlmax)
  175. //@function: findChildrenAuthority
  176. //@description: 查询子角色
  177. //@param: authority *model.SysAuthority
  178. //@return: err error
  179. func (authorityService *AuthorityService) findChildrenAuthority(authority *system.SysAuthority) (err error) {
  180. err = global.GVA_DB.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  181. if len(authority.Children) > 0 {
  182. for k := range authority.Children {
  183. err = authorityService.findChildrenAuthority(&authority.Children[k])
  184. }
  185. }
  186. return err
  187. }