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.

138 lines
4.1 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. for _, v := range casbinInfos {
  22. cm := model.CasbinModel{
  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. success, _ := e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
  43. return success
  44. }
  45. // @title UpdateCasbinApi
  46. // @description update casbin apis, API更新随动
  47. // @auth (2020/04/05 20:22)
  48. // @param oldPath string
  49. // @param newPath string
  50. // @param oldMethod string
  51. // @param newMethod string
  52. // @return error
  53. func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
  54. err := global.GVA_DB.Table("casbin_rule").Model(&model.CasbinModel{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{
  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. success, _ := e.RemoveFilteredPolicy(v, p...)
  85. return success
  86. }
  87. // @title Casbin
  88. // @description store to DB, 持久化到数据库 引入自定义规则
  89. // @auth (2020/04/05 20:22)
  90. func Casbin() *casbin.Enforcer {
  91. admin := global.GVA_CONFIG.Mysql
  92. a, _ := gormadapter.NewAdapter(global.GVA_CONFIG.System.DbType, admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname, true)
  93. e, _ := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  94. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  95. _ = e.LoadPolicy()
  96. return e
  97. }
  98. // @title ParamsMatch
  99. // @description customized rule, 自定义规则函数
  100. // @auth (2020/04/05 20:22)
  101. // @param fullNameKey1 string
  102. // @param key2 string
  103. // @return bool
  104. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  105. key1 := strings.Split(fullNameKey1, "?")[0]
  106. // 剥离路径后再使用casbin的keyMatch2
  107. return util.KeyMatch2(key1, key2)
  108. }
  109. // @title ParamsMatchFunc
  110. // @description customized function, 自定义规则函数
  111. // @auth (2020/04/05 20:22)
  112. // @param args ...interface{}
  113. // @return interface{}
  114. // @return error
  115. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  116. name1 := args[0].(string)
  117. name2 := args[1].(string)
  118. return ParamsMatch(name1, name2), nil
  119. }