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.

124 lines
3.4 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. //@author: [piexlmax](https://github.com/piexlmax)
  14. //@function: UpdateCasbin
  15. //@description: 更新casbin权限
  16. //@param: authorityId string, casbinInfos []request.CasbinInfo
  17. //@return: error
  18. func UpdateCasbin(authorityId string, casbinInfos []request.CasbinInfo) error {
  19. ClearCasbin(0, authorityId)
  20. rules := [][]string{}
  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. rules = append(rules, []string{cm.AuthorityId, cm.Path, cm.Method})
  29. }
  30. e := Casbin()
  31. success, _ := e.AddPolicies(rules)
  32. if success == false {
  33. return errors.New("存在相同api,添加失败,请联系管理员")
  34. }
  35. return nil
  36. }
  37. //@author: [piexlmax](https://github.com/piexlmax)
  38. //@function: UpdateCasbinApi
  39. //@description: API更新随动
  40. //@param: oldPath string, newPath string, oldMethod string, newMethod string
  41. //@return: error
  42. func UpdateCasbinApi(oldPath string, newPath string, oldMethod string, newMethod string) error {
  43. err := global.GVA_DB.Table("casbin_rule").Model(&model.CasbinModel{}).Where("v1 = ? AND v2 = ?", oldPath, oldMethod).Updates(map[string]interface{}{
  44. "v1": newPath,
  45. "v2": newMethod,
  46. }).Error
  47. return err
  48. }
  49. //@author: [piexlmax](https://github.com/piexlmax)
  50. //@function: GetPolicyPathByAuthorityId
  51. //@description: 获取权限列表
  52. //@param: authorityId string
  53. //@return: pathMaps []request.CasbinInfo
  54. func GetPolicyPathByAuthorityId(authorityId string) (pathMaps []request.CasbinInfo) {
  55. e := Casbin()
  56. list := e.GetFilteredPolicy(0, authorityId)
  57. for _, v := range list {
  58. pathMaps = append(pathMaps, request.CasbinInfo{
  59. Path: v[1],
  60. Method: v[2],
  61. })
  62. }
  63. return pathMaps
  64. }
  65. //@author: [piexlmax](https://github.com/piexlmax)
  66. //@function: ClearCasbin
  67. //@description: 清除匹配的权限
  68. //@param: v int, p ...string
  69. //@return: bool
  70. func ClearCasbin(v int, p ...string) bool {
  71. e := Casbin()
  72. success, _ := e.RemoveFilteredPolicy(v, p...)
  73. return success
  74. }
  75. //@author: [piexlmax](https://github.com/piexlmax)
  76. //@function: Casbin
  77. //@description: 持久化到数据库 引入自定义规则
  78. //@return: *casbin.Enforcer
  79. func Casbin() *casbin.Enforcer {
  80. admin := global.GVA_CONFIG.Mysql
  81. a, _ := gormadapter.NewAdapter(global.GVA_CONFIG.System.DbType, admin.Username+":"+admin.Password+"@("+admin.Path+")/"+admin.Dbname, true)
  82. e, _ := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  83. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  84. _ = e.LoadPolicy()
  85. return e
  86. }
  87. //@author: [piexlmax](https://github.com/piexlmax)
  88. //@function: ParamsMatch
  89. //@description: 自定义规则函数
  90. //@param: fullNameKey1 string, key2 string
  91. //@return: bool
  92. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  93. key1 := strings.Split(fullNameKey1, "?")[0]
  94. // 剥离路径后再使用casbin的keyMatch2
  95. return util.KeyMatch2(key1, key2)
  96. }
  97. //@author: [piexlmax](https://github.com/piexlmax)
  98. //@function: ParamsMatchFunc
  99. //@description: 自定义规则函数
  100. //@param: args ...interface{}
  101. //@return: interface{}, error
  102. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  103. name1 := args[0].(string)
  104. name2 := args[1].(string)
  105. return ParamsMatch(name1, name2), nil
  106. }