sys_casbin.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. "gin-vue-admin/model/request"
  6. resp "gin-vue-admin/model/response"
  7. "gin-vue-admin/service"
  8. "gin-vue-admin/utils"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // @Tags casbin
  12. // @Summary 更改角色api权限
  13. // @Security ApiKeyAuth
  14. // @accept application/json
  15. // @Produce application/json
  16. // @Param data body request.CasbinInReceive true "更改角色api权限"
  17. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  18. // @Router /casbin/UpdateCasbin [post]
  19. func UpdateCasbin(c *gin.Context) {
  20. var cmr request.CasbinInReceive
  21. _ = c.ShouldBindJSON(&cmr)
  22. CasbinVerify := utils.Rules{
  23. "AuthorityId": {utils.NotEmpty()},
  24. }
  25. WKVerifyErr := utils.Verify(cmr, CasbinVerify)
  26. if WKVerifyErr!=nil {
  27. response.FailWithMessage(WKVerifyErr.Error(), c)
  28. return
  29. }
  30. err := service.UpdateCasbin(cmr.AuthorityId, cmr.CasbinInfos)
  31. if err != nil {
  32. response.FailWithMessage(fmt.Sprintf("添加规则失败,%v", err), c)
  33. } else {
  34. response.OkWithMessage("添加规则成功", c)
  35. }
  36. }
  37. // @Tags casbin
  38. // @Summary 获取权限列表
  39. // @Security ApiKeyAuth
  40. // @accept application/json
  41. // @Produce application/json
  42. // @Param data body request.CasbinInReceive true "获取权限列表"
  43. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  44. // @Router /casbin/getPolicyPathByAuthorityId [post]
  45. func GetPolicyPathByAuthorityId(c *gin.Context) {
  46. var cmr request.CasbinInReceive
  47. _ = c.ShouldBindJSON(&cmr)
  48. CasbinVerify := utils.Rules{
  49. "AuthorityId": {utils.NotEmpty()},
  50. }
  51. WKVerifyErr := utils.Verify(cmr, CasbinVerify)
  52. if WKVerifyErr!=nil {
  53. response.FailWithMessage(WKVerifyErr.Error(), c)
  54. return
  55. }
  56. paths := service.GetPolicyPathByAuthorityId(cmr.AuthorityId)
  57. response.OkWithData(resp.PolicyPathResponse{Paths: paths}, c)
  58. }
  59. // @Tags casbin
  60. // @Summary casb RBAC RESTFUL测试路由
  61. // @Security ApiKeyAuth
  62. // @accept application/json
  63. // @Produce application/json
  64. // @Param data body request.CasbinInReceive true "获取权限列表"
  65. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  66. // @Router /casbin/CasbinTest [get]
  67. func CasbinTest(c *gin.Context) {
  68. // 测试restful以及占位符代码 随意书写
  69. pathParam := c.Param("pathParam")
  70. query := c.Query("query")
  71. response.OkDetailed(gin.H{"pathParam": pathParam, "query": query}, "获取规则成功", c)
  72. }