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.

122 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).Update("v1", newPath).Update("v2", newMethod).Error
  55. return err
  56. }
  57. // @title GetPolicyPathByAuthorityId
  58. // @description get policy path by authorityId, 获取权限列表
  59. // @auth (2020/04/05 20:22)
  60. // @param authorityId string
  61. // @return []string
  62. func GetPolicyPathByAuthorityId(authorityId string) []string {
  63. e := Casbin()
  64. var pathList []string
  65. list := e.GetFilteredPolicy(0, authorityId)
  66. for _, v := range list {
  67. pathList = append(pathList, v[1])
  68. }
  69. return pathList
  70. }
  71. // @title ClearCasbin
  72. // @description 清除匹配的权限
  73. // @auth (2020/04/05 20:22)
  74. // @param v int
  75. // @param p string
  76. // @return bool
  77. func ClearCasbin(v int, p ...string) bool {
  78. e := Casbin()
  79. return e.RemoveFilteredPolicy(v, p...)
  80. }
  81. // @title Casbin
  82. // @description store to DB, 持久化到数据库 引入自定义规则
  83. // @auth (2020/04/05 20:22)
  84. func Casbin() *casbin.Enforcer {
  85. a := gormadapter.NewAdapterByDB(global.GVA_DB)
  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 (bool)(ParamsMatch(name1, name2)), nil
  112. }