sys_casbin.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. "github.com/casbin/casbin/util"
  8. "github.com/casbin/casbin/v2"
  9. gormadapter "github.com/casbin/gorm-adapter/v3"
  10. _ "github.com/go-sql-driver/mysql"
  11. "strings"
  12. )
  13. // @title UpdateCasbin
  14. // @description update casbin authority, 更新casbin权限
  15. // @auth (2020/04/05 20:22)
  16. // @param authorityId string
  17. // @param casbinInfos []CasbinInfo
  18. // @return error
  19. func UpdateCasbin(authorityId string, casbinInfos []request.CasbinInfo) error {
  20. ClearCasbin(0, authorityId)
  21. for _, v := range casbinInfos {
  22. cm := model.CasbinModel{
  23. Ptype: "p",
  24. AuthorityId: authorityId,
  25. Path: v.Path,
  26. Method: v.Method,
  27. }
  28. addflag := AddCasbin(cm)
  29. if addflag == false {
  30. return errors.New("存在相同api,添加失败,请联系管理员")
  31. }
  32. }
  33. return nil
  34. }
  35. // @title AddCasbin
  36. // @description add casbin authority, 添加权限
  37. // @auth (2020/04/05 20:22)
  38. // @param cm model.CasbinModel
  39. // @return bool
  40. func AddCasbin(cm model.CasbinModel) bool {
  41. e := Casbin()
  42. success, _ := e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
  43. return success
  44. }
  45. // @title UpdateCasbinApi
  46. // @description update casbin apis, API更新随动
  47. // @auth (2020/04/05 20:22)
  48. // @param oldPath string
  49. // @param newPath string
  50. // @param oldMethod string
  51. // @param newMethod string
  52. // @return error
  53. func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
  54. var cs []model.CasbinModel
  55. err := global.GVA_DB.Table("casbin_rule").Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Find(&cs).Updates(map[string]string{
  56. "v1": newPath,
  57. "v2": newMethod,
  58. }).Error
  59. return err
  60. }
  61. // @title GetPolicyPathByAuthorityId
  62. // @description get policy path by authorityId, 获取权限列表
  63. // @auth (2020/04/05 20:22)
  64. // @param authorityId string
  65. // @return []string
  66. func GetPolicyPathByAuthorityId(authorityId string) (pathMaps []request.CasbinInfo) {
  67. e := Casbin()
  68. list := e.GetFilteredPolicy(0, authorityId)
  69. for _, v := range list {
  70. pathMaps = append(pathMaps, request.CasbinInfo{
  71. Path: v[1],
  72. Method: v[2],
  73. })
  74. }
  75. return pathMaps
  76. }
  77. // @title ClearCasbin
  78. // @description 清除匹配的权限
  79. // @auth (2020/04/05 20:22)
  80. // @param v int
  81. // @param p string
  82. // @return bool
  83. func ClearCasbin(v int, p ...string) bool {
  84. e := Casbin()
  85. success, _ := e.RemoveFilteredPolicy(v, p...)
  86. return success
  87. }
  88. // @title Casbin
  89. // @description store to DB, 持久化到数据库 引入自定义规则
  90. // @auth (2020/04/05 20:22)
  91. func Casbin() *casbin.Enforcer {
  92. admin := global.GVA_CONFIG.Mysql
  93. a, _ := gormadapter.NewAdapter(global.GVA_CONFIG.System.DbType, admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname, true)
  94. e, _ := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  95. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  96. _ = e.LoadPolicy()
  97. return e
  98. }
  99. // @title ParamsMatch
  100. // @description customized rule, 自定义规则函数
  101. // @auth (2020/04/05 20:22)
  102. // @param fullNameKey1 string
  103. // @param key2 string
  104. // @return bool
  105. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  106. key1 := strings.Split(fullNameKey1, "?")[0]
  107. // 剥离路径后再使用casbin的keyMatch2
  108. return util.KeyMatch2(key1, key2)
  109. }
  110. // @title ParamsMatchFunc
  111. // @description customized function, 自定义规则函数
  112. // @auth (2020/04/05 20:22)
  113. // @param args ...interface{}
  114. // @return interface{}
  115. // @return error
  116. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  117. name1 := args[0].(string)
  118. name2 := args[1].(string)
  119. return ParamsMatch(name1, name2), nil
  120. }