sys_casbin.go 4.1 KB

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