sys_casbin.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package system
  2. import (
  3. "errors"
  4. "strconv"
  5. "gorm.io/gorm"
  6. gormadapter "github.com/casbin/gorm-adapter/v3"
  7. "github.com/flipped-aurora/gin-vue-admin/server/global"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  9. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  10. _ "github.com/go-sql-driver/mysql"
  11. )
  12. //@author: [piexlmax](https://github.com/piexlmax)
  13. //@function: UpdateCasbin
  14. //@description: 更新casbin权限
  15. //@param: authorityId string, casbinInfos []request.CasbinInfo
  16. //@return: error
  17. type CasbinService struct{}
  18. var CasbinServiceApp = new(CasbinService)
  19. func (casbinService *CasbinService) UpdateCasbin(adminAuthorityID, AuthorityID uint, casbinInfos []request.CasbinInfo) error {
  20. err := AuthorityServiceApp.CheckAuthorityIDAuth(adminAuthorityID, AuthorityID)
  21. if err != nil {
  22. return err
  23. }
  24. if global.GVA_CONFIG.System.UseStrictAuth {
  25. apis, e := ApiServiceApp.GetAllApis(adminAuthorityID)
  26. if e != nil {
  27. return e
  28. }
  29. for i := range casbinInfos {
  30. hasApi := false
  31. for j := range apis {
  32. if apis[j].Path == casbinInfos[i].Path && apis[j].Method == casbinInfos[i].Method {
  33. hasApi = true
  34. break
  35. }
  36. }
  37. if !hasApi {
  38. return errors.New("存在api不在权限列表中")
  39. }
  40. }
  41. }
  42. authorityId := strconv.Itoa(int(AuthorityID))
  43. casbinService.ClearCasbin(0, authorityId)
  44. rules := [][]string{}
  45. //做权限去重处理
  46. deduplicateMap := make(map[string]bool)
  47. for _, v := range casbinInfos {
  48. key := authorityId + v.Path + v.Method
  49. if _, ok := deduplicateMap[key]; !ok {
  50. deduplicateMap[key] = true
  51. rules = append(rules, []string{authorityId, v.Path, v.Method})
  52. }
  53. }
  54. if len(rules) == 0 {
  55. return nil
  56. } // 设置空权限无需调用 AddPolicies 方法
  57. e := utils.GetCasbin()
  58. success, _ := e.AddPolicies(rules)
  59. if !success {
  60. return errors.New("存在相同api,添加失败,请联系管理员")
  61. }
  62. return nil
  63. }
  64. //@author: [piexlmax](https://github.com/piexlmax)
  65. //@function: UpdateCasbinApi
  66. //@description: API更新随动
  67. //@param: oldPath string, newPath string, oldMethod string, newMethod string
  68. //@return: error
  69. func (casbinService *CasbinService) UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
  70. err := global.GVA_DB.Model(&gormadapter.CasbinRule{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{
  71. "v1": newPath,
  72. "v2": newMethod,
  73. }).Error
  74. if err != nil {
  75. return err
  76. }
  77. e := utils.GetCasbin()
  78. return e.LoadPolicy()
  79. }
  80. //@author: [piexlmax](https://github.com/piexlmax)
  81. //@function: GetPolicyPathByAuthorityId
  82. //@description: 获取权限列表
  83. //@param: authorityId string
  84. //@return: pathMaps []request.CasbinInfo
  85. func (casbinService *CasbinService) GetPolicyPathByAuthorityId(AuthorityID uint) (pathMaps []request.CasbinInfo) {
  86. e := utils.GetCasbin()
  87. authorityId := strconv.Itoa(int(AuthorityID))
  88. list, _ := e.GetFilteredPolicy(0, authorityId)
  89. for _, v := range list {
  90. pathMaps = append(pathMaps, request.CasbinInfo{
  91. Path: v[1],
  92. Method: v[2],
  93. })
  94. }
  95. return pathMaps
  96. }
  97. //@author: [piexlmax](https://github.com/piexlmax)
  98. //@function: ClearCasbin
  99. //@description: 清除匹配的权限
  100. //@param: v int, p ...string
  101. //@return: bool
  102. func (casbinService *CasbinService) ClearCasbin(v int, p ...string) bool {
  103. e := utils.GetCasbin()
  104. success, _ := e.RemoveFilteredPolicy(v, p...)
  105. return success
  106. }
  107. //@author: [piexlmax](https://github.com/piexlmax)
  108. //@function: RemoveFilteredPolicy
  109. //@description: 使用数据库方法清理筛选的politicy 此方法需要调用FreshCasbin方法才可以在系统中即刻生效
  110. //@param: db *gorm.DB, authorityId string
  111. //@return: error
  112. func (casbinService *CasbinService) RemoveFilteredPolicy(db *gorm.DB, authorityId string) error {
  113. return db.Delete(&gormadapter.CasbinRule{}, "v0 = ?", authorityId).Error
  114. }
  115. //@author: [piexlmax](https://github.com/piexlmax)
  116. //@function: SyncPolicy
  117. //@description: 同步目前数据库的policy 此方法需要调用FreshCasbin方法才可以在系统中即刻生效
  118. //@param: db *gorm.DB, authorityId string, rules [][]string
  119. //@return: error
  120. func (casbinService *CasbinService) SyncPolicy(db *gorm.DB, authorityId string, rules [][]string) error {
  121. err := casbinService.RemoveFilteredPolicy(db, authorityId)
  122. if err != nil {
  123. return err
  124. }
  125. return casbinService.AddPolicies(db, rules)
  126. }
  127. //@author: [piexlmax](https://github.com/piexlmax)
  128. //@function: AddPolicies
  129. //@description: 添加匹配的权限
  130. //@param: v int, p ...string
  131. //@return: bool
  132. func (casbinService *CasbinService) AddPolicies(db *gorm.DB, rules [][]string) error {
  133. var casbinRules []gormadapter.CasbinRule
  134. for i := range rules {
  135. casbinRules = append(casbinRules, gormadapter.CasbinRule{
  136. Ptype: "p",
  137. V0: rules[i][0],
  138. V1: rules[i][1],
  139. V2: rules[i][2],
  140. })
  141. }
  142. return db.Create(&casbinRules).Error
  143. }
  144. func (casbinService *CasbinService) FreshCasbin() (err error) {
  145. e := utils.GetCasbin()
  146. err = e.LoadPolicy()
  147. return err
  148. }