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.

108 lines
3.1 KiB

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