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.

125 lines
3.8 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) []string {
  66. e := Casbin()
  67. var pathList []string
  68. list := e.GetFilteredPolicy(0, authorityId)
  69. for _, v := range list {
  70. pathList = append(pathList, v[1])
  71. }
  72. return pathList
  73. }
  74. // @title ClearCasbin
  75. // @description 清除匹配的权限
  76. // @auth (2020/04/05 20:22)
  77. // @param v int
  78. // @param p string
  79. // @return bool
  80. func ClearCasbin(v int, p ...string) bool {
  81. e := Casbin()
  82. return e.RemoveFilteredPolicy(v, p...)
  83. }
  84. // @title Casbin
  85. // @description store to DB, 持久化到数据库 引入自定义规则
  86. // @auth (2020/04/05 20:22)
  87. func Casbin() *casbin.Enforcer {
  88. a := gormadapter.NewAdapterByDB(global.GVA_DB)
  89. e := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  90. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  91. _ = e.LoadPolicy()
  92. return e
  93. }
  94. // @title ParamsMatch
  95. // @description customized rule, 自定义规则函数
  96. // @auth (2020/04/05 20:22)
  97. // @param fullNameKey1 string
  98. // @param key2 string
  99. // @return bool
  100. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  101. key1 := strings.Split(fullNameKey1, "?")[0]
  102. //剥离路径后再使用casbin的keyMatch2
  103. return util.KeyMatch2(key1, key2)
  104. }
  105. // @title ParamsMatchFunc
  106. // @description customized function, 自定义规则函数
  107. // @auth (2020/04/05 20:22)
  108. // @param args ...interface{}
  109. // @return interface{}
  110. // @return error
  111. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  112. name1 := args[0].(string)
  113. name2 := args[1].(string)
  114. return (bool)(ParamsMatch(name1, name2)), nil
  115. }