sys_authority.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package system
  2. import (
  3. "errors"
  4. "strconv"
  5. "gorm.io/gorm"
  6. "lcfns/global"
  7. "lcfns/model/common/request"
  8. "lcfns/model/system"
  9. "lcfns/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.Db.Where("authority_id = ?", auth.AuthorityId).First(&authorityBox).Error, gorm.ErrRecordNotFound) {
  22. return auth, ErrRoleExistence
  23. }
  24. err = global.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.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.Db.Create(&copyInfo.Authority).Error
  50. if err != nil {
  51. return
  52. }
  53. var btns []system.SysAuthorityBtn
  54. err = global.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.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.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.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.Db.Where("authority_id = ?", auth.AuthorityId).First(&system.SysUser{}).Error, gorm.ErrRecordNotFound) {
  96. return errors.New("此角色有用户正在使用禁止删除")
  97. }
  98. if !errors.Is(global.Db.Where("parent_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
  99. return errors.New("此角色存在子角色不允许删除")
  100. }
  101. db := global.Db.Preload("SysBaseMenus").Preload("DataAuthorityId").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.Db.Model(auth).Association("SysBaseMenus").Delete(auth.SysBaseMenus)
  108. if err != nil {
  109. return
  110. }
  111. // err = db.Association("SysBaseMenus").Delete(&auth)
  112. }
  113. if len(auth.DataAuthorityId) > 0 {
  114. err = global.Db.Model(auth).Association("DataAuthorityId").Delete(auth.DataAuthorityId)
  115. if err != nil {
  116. return
  117. }
  118. }
  119. err = global.Db.Delete(&[]system.SysUserAuthority{}, "sys_authority_authority_id = ?", auth.AuthorityId).Error
  120. if err != nil {
  121. return
  122. }
  123. err = global.Db.Delete(&[]system.SysAuthorityBtn{}, "authority_id = ?", auth.AuthorityId).Error
  124. if err != nil {
  125. return
  126. }
  127. authorityId := strconv.Itoa(int(auth.AuthorityId))
  128. CasbinServiceApp.ClearCasbin(0, authorityId)
  129. return err
  130. }
  131. //@author: [piexlmax](https://github.com/piexlmax)
  132. //@function: GetAuthorityInfoList
  133. //@description: 分页获取数据
  134. //@param: info request.PageInfo
  135. //@return: list interface{}, total int64, err error
  136. func (authorityService *AuthorityService) GetAuthorityInfoList(info request.PageInfo) (list interface{}, total int64, err error) {
  137. limit := info.PageSize
  138. offset := info.PageSize * (info.Page - 1)
  139. db := global.Db.Model(&system.SysAuthority{})
  140. if err = db.Where("parent_id = ?", "0").Count(&total).Error; total == 0 || err != nil {
  141. return
  142. }
  143. var authority []system.SysAuthority
  144. err = db.Limit(limit).Offset(offset).Preload("DataAuthorityId").Where("parent_id = ?", "0").Find(&authority).Error
  145. for k := range authority {
  146. err = authorityService.findChildrenAuthority(&authority[k])
  147. }
  148. return authority, total, err
  149. }
  150. //@author: [piexlmax](https://github.com/piexlmax)
  151. //@function: GetAuthorityInfo
  152. //@description: 获取所有角色信息
  153. //@param: auth model.SysAuthority
  154. //@return: sa system.SysAuthority, err error
  155. func (authorityService *AuthorityService) GetAuthorityInfo(auth system.SysAuthority) (sa system.SysAuthority, err error) {
  156. err = global.Db.Preload("DataAuthorityId").Where("authority_id = ?", auth.AuthorityId).First(&sa).Error
  157. return sa, err
  158. }
  159. //@author: [piexlmax](https://github.com/piexlmax)
  160. //@function: SetDataAuthority
  161. //@description: 设置角色资源权限
  162. //@param: auth model.SysAuthority
  163. //@return: error
  164. func (authorityService *AuthorityService) SetDataAuthority(auth system.SysAuthority) error {
  165. var s system.SysAuthority
  166. global.Db.Preload("DataAuthorityId").First(&s, "authority_id = ?", auth.AuthorityId)
  167. err := global.Db.Model(&s).Association("DataAuthorityId").Replace(&auth.DataAuthorityId)
  168. return err
  169. }
  170. //@author: [piexlmax](https://github.com/piexlmax)
  171. //@function: SetMenuAuthority
  172. //@description: 菜单与角色绑定
  173. //@param: auth *model.SysAuthority
  174. //@return: error
  175. func (authorityService *AuthorityService) SetMenuAuthority(auth *system.SysAuthority) error {
  176. var s system.SysAuthority
  177. global.Db.Preload("SysBaseMenus").First(&s, "authority_id = ?", auth.AuthorityId)
  178. err := global.Db.Model(&s).Association("SysBaseMenus").Replace(&auth.SysBaseMenus)
  179. return err
  180. }
  181. //@author: [piexlmax](https://github.com/piexlmax)
  182. //@function: findChildrenAuthority
  183. //@description: 查询子角色
  184. //@param: authority *model.SysAuthority
  185. //@return: err error
  186. func (authorityService *AuthorityService) findChildrenAuthority(authority *system.SysAuthority) (err error) {
  187. err = global.Db.Preload("DataAuthorityId").Where("parent_id = ?", authority.AuthorityId).Find(&authority.Children).Error
  188. if len(authority.Children) > 0 {
  189. for k := range authority.Children {
  190. err = authorityService.findChildrenAuthority(&authority.Children[k])
  191. }
  192. }
  193. return err
  194. }