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.

136 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"
  8. "github.com/casbin/casbin/util"
  9. gormadapter "github.com/casbin/gorm-adapter"
  10. "strings"
  11. )
  12. // @title UpdateCasbin
  13. // @description update casbin authority, 更新casbin权限
  14. // @auth (2020/04/05 20:22)
  15. // @param authorityId string
  16. // @param casbinInfos []CasbinInfo
  17. // @return error
  18. func UpdateCasbin(authorityId string, casbinInfos []request.CasbinInfo) error {
  19. ClearCasbin(0, authorityId)
  20. for _, v := range casbinInfos {
  21. cm := model.CasbinModel{
  22. ID: 0,
  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. return e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
  43. }
  44. // @title UpdateCasbinApi
  45. // @description update casbin apis, API更新随动
  46. // @auth (2020/04/05 20:22)
  47. // @param oldPath string
  48. // @param newPath string
  49. // @param oldMethod string
  50. // @param newMethod string
  51. // @return error
  52. func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
  53. var cs []model.CasbinModel
  54. err := global.GVA_DB.Table("casbin_rule").Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Find(&cs).Updates(map[string]string{
  55. "v1": newPath,
  56. "v2": newMethod,
  57. }).Error
  58. return err
  59. }
  60. // @title GetPolicyPathByAuthorityId
  61. // @description get policy path by authorityId, 获取权限列表
  62. // @auth (2020/04/05 20:22)
  63. // @param authorityId string
  64. // @return []string
  65. func GetPolicyPathByAuthorityId(authorityId string) (pathMaps []request.CasbinInfo) {
  66. e := Casbin()
  67. list := e.GetFilteredPolicy(0, authorityId)
  68. for _, v := range list {
  69. pathMaps = append(pathMaps, request.CasbinInfo{
  70. Path: v[1],
  71. Method: v[2],
  72. })
  73. }
  74. return pathMaps
  75. }
  76. // @title ClearCasbin
  77. // @description 清除匹配的权限
  78. // @auth (2020/04/05 20:22)
  79. // @param v int
  80. // @param p string
  81. // @return bool
  82. func ClearCasbin(v int, p ...string) bool {
  83. e := Casbin()
  84. return e.RemoveFilteredPolicy(v, p...)
  85. }
  86. // @title Casbin
  87. // @description store to DB, 持久化到数据库 引入自定义规则
  88. // @auth (2020/04/05 20:22)
  89. func Casbin() *casbin.Enforcer {
  90. a := gormadapter.NewAdapterByDB(global.GVA_DB)
  91. e := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  92. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  93. _ = e.LoadPolicy()
  94. return e
  95. }
  96. // @title ParamsMatch
  97. // @description customized rule, 自定义规则函数
  98. // @auth (2020/04/05 20:22)
  99. // @param fullNameKey1 string
  100. // @param key2 string
  101. // @return bool
  102. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  103. key1 := strings.Split(fullNameKey1, "?")[0]
  104. //剥离路径后再使用casbin的keyMatch2
  105. return util.KeyMatch2(key1, key2)
  106. }
  107. // @title ParamsMatchFunc
  108. // @description customized function, 自定义规则函数
  109. // @auth (2020/04/05 20:22)
  110. // @param args ...interface{}
  111. // @return interface{}
  112. // @return error
  113. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  114. name1 := args[0].(string)
  115. name2 := args[1].(string)
  116. return (bool)(ParamsMatch(name1, name2)), nil
  117. }