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.

106 lines
3.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package system
  2. import (
  3. "errors"
  4. "sync"
  5. "github.com/casbin/casbin/v2"
  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"
  9. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  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(authorityId string, casbinInfos []request.CasbinInfo) error {
  20. casbinService.ClearCasbin(0, authorityId)
  21. rules := [][]string{}
  22. for _, v := range casbinInfos {
  23. cm := system.CasbinModel{
  24. Ptype: "p",
  25. AuthorityId: authorityId,
  26. Path: v.Path,
  27. Method: v.Method,
  28. }
  29. rules = append(rules, []string{cm.AuthorityId, cm.Path, cm.Method})
  30. }
  31. e := casbinService.Casbin()
  32. success, _ := e.AddPolicies(rules)
  33. if !success {
  34. return errors.New("存在相同api,添加失败,请联系管理员")
  35. }
  36. return nil
  37. }
  38. //@author: [piexlmax](https://github.com/piexlmax)
  39. //@function: UpdateCasbinApi
  40. //@description: API更新随动
  41. //@param: oldPath string, newPath string, oldMethod string, newMethod string
  42. //@return: error
  43. func (casbinService *CasbinService) UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
  44. err := global.GVA_DB.Table("casbin_rule").Model(&system.CasbinModel{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{
  45. "v1": newPath,
  46. "v2": newMethod,
  47. }).Error
  48. return err
  49. }
  50. //@author: [piexlmax](https://github.com/piexlmax)
  51. //@function: GetPolicyPathByAuthorityId
  52. //@description: 获取权限列表
  53. //@param: authorityId string
  54. //@return: pathMaps []request.CasbinInfo
  55. func (casbinService *CasbinService) GetPolicyPathByAuthorityId(authorityId string) (pathMaps []request.CasbinInfo) {
  56. e := casbinService.Casbin()
  57. list := e.GetFilteredPolicy(0, authorityId)
  58. for _, v := range list {
  59. pathMaps = append(pathMaps, request.CasbinInfo{
  60. Path: v[1],
  61. Method: v[2],
  62. })
  63. }
  64. return pathMaps
  65. }
  66. //@author: [piexlmax](https://github.com/piexlmax)
  67. //@function: ClearCasbin
  68. //@description: 清除匹配的权限
  69. //@param: v int, p ...string
  70. //@return: bool
  71. func (casbinService *CasbinService) ClearCasbin(v int, p ...string) bool {
  72. e := casbinService.Casbin()
  73. success, _ := e.RemoveFilteredPolicy(v, p...)
  74. return success
  75. }
  76. //@author: [piexlmax](https://github.com/piexlmax)
  77. //@function: Casbin
  78. //@description: 持久化到数据库 引入自定义规则
  79. //@return: *casbin.Enforcer
  80. var (
  81. syncedEnforcer *casbin.SyncedEnforcer
  82. once sync.Once
  83. )
  84. func (casbinService *CasbinService) Casbin() *casbin.SyncedEnforcer {
  85. once.Do(func() {
  86. a, _ := gormadapter.NewAdapterByDB(global.GVA_DB)
  87. syncedEnforcer, _ = casbin.NewSyncedEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  88. })
  89. _ = syncedEnforcer.LoadPolicy()
  90. return syncedEnforcer
  91. }