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.

105 lines
2.6 KiB

  1. package sysModel
  2. import (
  3. "errors"
  4. "gin-vue-admin/config"
  5. "gin-vue-admin/init/qmsql"
  6. "github.com/casbin/casbin"
  7. "github.com/casbin/casbin/util"
  8. gormadapter "github.com/casbin/gorm-adapter"
  9. "strings"
  10. )
  11. type CasbinModel struct {
  12. ID uint `json:"id" gorm:"column:_id"`
  13. Ptype string `json:"ptype" gorm:"column:ptype"`
  14. AuthorityId string `json:"rolename" gorm:"column:v0"`
  15. Path string `json:"path" gorm:"column:v1"`
  16. Method string `json:"method" gorm:"column:v2"`
  17. }
  18. // 供入参使用
  19. type CasbinInfo struct {
  20. Path string `json:"path"`
  21. Method string `json:"method"`
  22. }
  23. // 供入参使用
  24. type CasbinInReceive struct {
  25. AuthorityId string `json:"authorityId"`
  26. CasbinInfos []CasbinInfo `json:"casbinInfos"`
  27. }
  28. // 更新权限
  29. func (c *CasbinModel) CasbinPUpdata(AuthorityId string, casbinInfos []CasbinInfo) error {
  30. c.clearCasbin(0, AuthorityId)
  31. for _, v := range casbinInfos {
  32. cm := CasbinModel{
  33. ID: 0,
  34. Ptype: "p",
  35. AuthorityId: AuthorityId,
  36. Path: v.Path,
  37. Method: v.Method,
  38. }
  39. addflag := c.AddCasbin(cm)
  40. if addflag == false {
  41. return errors.New("存在相同api,添加失败,请联系管理员")
  42. }
  43. }
  44. return nil
  45. }
  46. // API更新随动
  47. func (c *CasbinModel) CasbinApiUpdata(oldPath string, newPath string) error {
  48. var cs []CasbinModel
  49. err := qmsql.DEFAULTDB.Table("casbin_rule").Where("v1 = ?", oldPath).Find(&cs).Update("v1", newPath).Error
  50. return err
  51. }
  52. //添加权限
  53. func (c *CasbinModel) AddCasbin(cm CasbinModel) bool {
  54. e := Casbin()
  55. return e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
  56. }
  57. //获取权限列表
  58. func (c *CasbinModel) GetPolicyPathByAuthorityId(AuthorityId string) []string {
  59. e := Casbin()
  60. var pathList []string
  61. list := e.GetFilteredPolicy(0, AuthorityId)
  62. for _, v := range list {
  63. pathList = append(pathList, v[1])
  64. }
  65. return pathList
  66. }
  67. //清除匹配的权限
  68. func (c *CasbinModel) clearCasbin(v int, p string) bool {
  69. e := Casbin()
  70. return e.RemoveFilteredPolicy(v, p)
  71. }
  72. // 自定义规则函数
  73. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  74. key1 := strings.Split(fullNameKey1, "?")[0]
  75. //剥离路径后再使用casbin的keyMatch2
  76. return util.KeyMatch2(key1, key2)
  77. }
  78. // 自定义规则函数
  79. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  80. name1 := args[0].(string)
  81. name2 := args[1].(string)
  82. return (bool)(ParamsMatch(name1, name2)), nil
  83. }
  84. //持久化到数据库 引入自定义规则
  85. func Casbin() *casbin.Enforcer {
  86. a := gormadapter.NewAdapterByDB(qmsql.DEFAULTDB)
  87. e := casbin.NewEnforcer(config.GinVueAdminconfig.CasbinConfig.ModelPath, a)
  88. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  89. e.LoadPolicy()
  90. return e
  91. }