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.

139 lines
4.2 KiB

  1. package model
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "github.com/casbin/casbin"
  6. "github.com/casbin/casbin/util"
  7. gormadapter "github.com/casbin/gorm-adapter"
  8. "strings"
  9. )
  10. type CasbinModel struct {
  11. ID uint `json:"id" gorm:"column:_id"`
  12. Ptype string `json:"ptype" gorm:"column:ptype"`
  13. AuthorityId string `json:"rolename" gorm:"column:v0"`
  14. Path string `json:"path" gorm:"column:v1"`
  15. Method string `json:"method" gorm:"column:v2"`
  16. }
  17. // 供入参使用
  18. type CasbinInfo struct {
  19. Path string `json:"path"`
  20. Method string `json:"method"`
  21. }
  22. // 供入参使用
  23. type CasbinInReceive struct {
  24. AuthorityId string `json:"authorityId"`
  25. CasbinInfos []CasbinInfo `json:"casbinInfos"`
  26. }
  27. // @title UpdateCasbin
  28. // @description update casbin authority, 更新casbin权限
  29. // @auth (2020/04/05 20:22 )
  30. // @param authorityId string
  31. // @param casbinInfos []CasbinInfo
  32. // @return error
  33. func (c *CasbinModel) UpdateCasbin(authorityId string, casbinInfos []CasbinInfo) error {
  34. c.ClearCasbin(0, authorityId)
  35. for _, v := range casbinInfos {
  36. cm := CasbinModel{
  37. ID: 0,
  38. Ptype: "p",
  39. AuthorityId: authorityId,
  40. Path: v.Path,
  41. Method: v.Method,
  42. }
  43. addflag := c.AddCasbin(cm)
  44. if addflag == false {
  45. return errors.New("存在相同api,添加失败,请联系管理员")
  46. }
  47. }
  48. return nil
  49. }
  50. // @title UpdateCasbinApi
  51. // @description update casbin apis, API更新随动
  52. // @auth (2020/04/05 20:22 )
  53. // @param oldPath string
  54. // @param newPath string
  55. // @return error
  56. func (c *CasbinModel) UpdateCasbinApi(oldPath string, newPath string) error {
  57. var cs []CasbinModel
  58. err := global.GVA_DB.Table("casbin_rule").Where("v1 = ?", oldPath).Find(&cs).Update("v1", newPath).Error
  59. return err
  60. }
  61. // @title AddCasbin
  62. // @description add casbin authority, 添加权限
  63. // @auth (2020/04/05 20:22 )
  64. // @param cm CasbinModel
  65. // @return bool
  66. func (c *CasbinModel) AddCasbin(cm CasbinModel) bool {
  67. e := Casbin()
  68. return e.AddPolicy(cm.AuthorityId, cm.Path, cm.Method)
  69. }
  70. // @title GetPolicyPathByAuthorityId
  71. // @description get policy path by authorityId, 获取权限列表
  72. // @auth (2020/04/05 20:22 )
  73. // @param authorityId string
  74. // @return []string
  75. func (c *CasbinModel) GetPolicyPathByAuthorityId(authorityId string) []string {
  76. e := Casbin()
  77. var pathList []string
  78. list := e.GetFilteredPolicy(0, authorityId)
  79. for _, v := range list {
  80. pathList = append(pathList, v[1])
  81. }
  82. return pathList
  83. }
  84. // @title ClearCasbin
  85. // @description 清除匹配的权限
  86. // @auth (2020/04/05 20:22 )
  87. // @param v int
  88. // @param p string
  89. // @return bool
  90. func (c *CasbinModel) ClearCasbin(v int, p string) bool {
  91. e := Casbin()
  92. return e.RemoveFilteredPolicy(v, p)
  93. }
  94. // @title ParamsMatch
  95. // @description customized rule, 自定义规则函数
  96. // @auth (2020/04/05 20:22 )
  97. // @param fullNameKey1 string
  98. // @param key2 string
  99. // @return bool
  100. func ParamsMatch(fullNameKey1 string, key2 string) bool {
  101. key1 := strings.Split(fullNameKey1, "?")[0]
  102. //剥离路径后再使用casbin的keyMatch2
  103. return util.KeyMatch2(key1, key2)
  104. }
  105. // @title ParamsMatchFunc
  106. // @description customized function, 自定义规则函数
  107. // @auth (2020/04/05 20:22 )
  108. // @param args ...interface{}
  109. // @return interface{}
  110. // @return error
  111. func ParamsMatchFunc(args ...interface{}) (interface{}, error) {
  112. name1 := args[0].(string)
  113. name2 := args[1].(string)
  114. return (bool)(ParamsMatch(name1, name2)), nil
  115. }
  116. // @title Casbin
  117. // @description store to DB, 持久化到数据库 引入自定义规则
  118. // @auth (2020/04/05 20:22 )
  119. func Casbin() *casbin.Enforcer {
  120. a := gormadapter.NewAdapterByDB(global.GVA_DB)
  121. e := casbin.NewEnforcer(global.GVA_CONFIG.Casbin.ModelPath, a)
  122. e.AddFunction("ParamsMatch", ParamsMatchFunc)
  123. _ = e.LoadPolicy()
  124. return e
  125. }