From ae8d3b38302bb2216713102e776ff44d9d58aeb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Tue, 27 Oct 2020 22:06:46 +0800 Subject: [PATCH] =?UTF-8?q?rbac=E6=9D=83=E9=99=90=E9=83=A8=E5=88=86?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/go-fly.sql | 2 +- middleware/rbac.go | 61 +++++++++++++++++++++++++++--------------- static/js/chat-main.js | 8 +++++- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/config/go-fly.sql b/config/go-fly.sql index 7b25d38..763862a 100644 --- a/config/go-fly.sql +++ b/config/go-fly.sql @@ -71,7 +71,7 @@ CREATE TABLE `role` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8| INSERT INTO `role` (`id`, `name`, `method`, `path`) VALUES -(1, '普通客服', 'GET', '/kefuinfo,/kefulist,/roles'), +(1, '普通客服', 'GET', 'GET:/kefuinfo,GET:/kefulist,GET:/roles'), (2, '管理员', '*', '*')| DROP TABLE IF EXISTS `welcome`| diff --git a/middleware/rbac.go b/middleware/rbac.go index 28d5a30..4ab2cff 100644 --- a/middleware/rbac.go +++ b/middleware/rbac.go @@ -1,6 +1,7 @@ package middleware import ( + "fmt" "github.com/gin-gonic/gin" "github.com/taoshihan1991/imaptool/models" "strings" @@ -9,30 +10,13 @@ import ( func RbacAuth(c *gin.Context) { roleId, _ := c.Get("role_id") role := models.FindRole(roleId) - var methodFlag bool - rPaths := strings.Split(c.Request.RequestURI, "?") - if role.Method != "*" { - methods := strings.Split(role.Method, ",") - for _, m := range methods { - if c.Request.Method == m { - methodFlag = true - break - } - } - if !methodFlag { - c.JSON(200, gin.H{ - "code": 403, - "msg": "没有权限:" + c.Request.Method + "," + rPaths[0], - }) - c.Abort() - return - } - } var flag bool - if role.Path != "*" { + rPaths := strings.Split(c.Request.RequestURI, "?") + uriParam := fmt.Sprintf("%s:%s", c.Request.Method, rPaths[0]) + if role.Method != "*" || role.Path != "*" { paths := strings.Split(role.Path, ",") for _, p := range paths { - if rPaths[0] == p { + if uriParam == p { flag = true break } @@ -40,10 +24,43 @@ func RbacAuth(c *gin.Context) { if !flag { c.JSON(200, gin.H{ "code": 403, - "msg": "没有权限:" + rPaths[0], + "msg": "没有权限:" + uriParam, }) c.Abort() return } + //methods := strings.Split(role.Method, ",") + //for _, m := range methods { + // if c.Request.Method == m { + // methodFlag = true + // break + // } + //} + //if !methodFlag { + // c.JSON(200, gin.H{ + // "code": 403, + // "msg": "没有权限:" + c.Request.Method + "," + rPaths[0], + // }) + // c.Abort() + // return + //} } + //var flag bool + //if role.Path != "*" { + // paths := strings.Split(role.Path, ",") + // for _, p := range paths { + // if rPaths[0] == p { + // flag = true + // break + // } + // } + // if !flag { + // c.JSON(200, gin.H{ + // "code": 403, + // "msg": "没有权限:" + rPaths[0], + // }) + // c.Abort() + // return + // } + //} } diff --git a/static/js/chat-main.js b/static/js/chat-main.js index 24491c3..9c5b971 100644 --- a/static/js/chat-main.js +++ b/static/js/chat-main.js @@ -186,7 +186,13 @@ var app=new Vue({ mes.from_id = this.kfConfig.id; mes.to_id = this.currentGuest; mes.content = this.messageContent; - $.post("/2/message",mes,function(){ + $.post("/2/message",mes,function(res){ + if(res.code!=200){ + _this.$message({ + message: data.msg, + type: 'error' + }); + } _this.messageContent = ""; });