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.

129 lines
3.9 KiB

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