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.

120 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 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. // @return error
  50. func UpdateCasbinApi(oldPath string, newPath string,oldMethod string, newMethod string) error {
  51. var cs []model.CasbinModel
  52. err := global.GVA_DB.Table("casbin_rule").Where("v1 = ? AND v2 = ?", oldPath,oldMethod).Find(&cs).Update("v1", newPath).Update("v2", newMethod).Error
  53. return err
  54. }
  55. // @title GetPolicyPathByAuthorityId
  56. // @description get policy path by authorityId, 获取权限列表
  57. // @auth (2020/04/05 20:22 )
  58. // @param authorityId string
  59. // @return []string
  60. func GetPolicyPathByAuthorityId(authorityId string) []string {
  61. e := Casbin()
  62. var pathList []string
  63. list := e.GetFilteredPolicy(0, authorityId)
  64. for _, v := range list {
  65. pathList = append(pathList, v[1])
  66. }
  67. return pathList
  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. return e.RemoveFilteredPolicy(v, p...)
  78. }
  79. // @title Casbin
  80. // @description store to DB, 持久化到数据库 引入自定义规则
  81. // @auth (2020/04/05 20:22 )
  82. func Casbin() *casbin.Enforcer {
  83. a := gormadapter.NewAdapterByDB(global.GVA_DB)
  84. e := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  85. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  86. _ = e.LoadPolicy()
  87. return e
  88. }
  89. // @title ParamsMatch
  90. // @description customized rule, 自定义规则函数
  91. // @auth (2020/04/05 20:22 )
  92. // @param fullNameKey1 string
  93. // @param key2 string
  94. // @return bool
  95. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  96. key1 := strings.Split(fullNameKey1, "?")[0]
  97. //剥离路径后再使用casbin的keyMatch2
  98. return util.KeyMatch2(key1, key2)
  99. }
  100. // @title ParamsMatchFunc
  101. // @description customized function, 自定义规则函数
  102. // @auth (2020/04/05 20:22 )
  103. // @param args ...interface{}
  104. // @return interface{}
  105. // @return error
  106. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  107. name1 := args[0].(string)
  108. name2 := args[1].(string)
  109. return (bool)(ParamsMatch(name1, name2)), nil
  110. }