Skip to content

Commit

Permalink
增加用户信息,菜单权限缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
107295472 committed Sep 11, 2018
1 parent 9ca85de commit 482e37f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 45 deletions.
5 changes: 4 additions & 1 deletion controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"fmt"
"strings"
"time"
"yin/apiadmin/utils"

"github.com/george518/PPGo_ApiAdmin/models"
cache "github.com/patrickmn/go-cache"
)

type AuthController struct {
Expand Down Expand Up @@ -101,7 +103,7 @@ func (self *AuthController) AjaxSave() {
self.ajaxMsg(err.Error(), MSG_ERR)
}
}

utils.Che.Set("menu"+utils.Int2String(self.user.Id), nil, cache.DefaultExpiration)
self.ajaxMsg("", MSG_OK)
}

Expand All @@ -114,5 +116,6 @@ func (self *AuthController) AjaxDel() {
if err := auth.Update(); err != nil {
self.ajaxMsg(err.Error(), MSG_ERR)
}
utils.Che.Set("menu"+utils.Int2String(self.user.Id), nil, cache.DefaultExpiration)
self.ajaxMsg("", MSG_OK)
}
118 changes: 74 additions & 44 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"fmt"
"strconv"
"strings"
"yin/apiadmin/utils"

"github.com/astaxie/beego"
"github.com/george518/PPGo_ApiAdmin/libs"
"github.com/george518/PPGo_ApiAdmin/models"
cache "github.com/patrickmn/go-cache"
)

const (
Expand Down Expand Up @@ -58,23 +59,32 @@ func (self *BaseController) Prepare() {

//登录权限验证
func (self *BaseController) auth() {

arr := strings.Split(self.Ctx.GetCookie("auth"), "|")
self.userId = 0
if len(arr) == 2 {
idstr, password := arr[0], arr[1]
userId, _ := strconv.Atoi(idstr)
if userId > 0 {
user, err := models.AdminGetById(userId)
if err == nil && password == libs.Md5([]byte(self.getClientIp()+"|"+user.Password+user.Salt)) {
var err error
cheUser, found := utils.Che.Get("uid" + utils.Int2String(userId))
user := &models.Admin{}
if found && cheUser != nil { //从缓存取用户
user = cheUser.(*models.Admin)
} else {
user, err = models.AdminGetById(userId)
utils.Che.Set("uid"+utils.Int2String(user.Id), user, cache.DefaultExpiration)
}
if err == nil && password == utils.Md5([]byte(self.getClientIp()+"|"+user.Password+user.Salt)) {
self.userId = user.Id

self.loginName = user.LoginName
self.userName = user.RealName
self.user = user
self.AdminAuth()
}

isHasAuth := strings.Contains(self.allowUrl, self.controllerName+"/"+self.actionName)
//不需要权限检查
noAuth := "ajaxsave/ajaxdel/table/loginin/loginout/getnodes/start/show/ajaxapisave/index/group/public/env/code/apidetail"
isNoAuth := strings.Contains(noAuth, self.actionName)
if isHasAuth == false && isNoAuth == false {
Expand All @@ -91,51 +101,71 @@ func (self *BaseController) auth() {
}

func (self *BaseController) AdminAuth() {
// 左侧导航栏
filters := make([]interface{}, 0)
filters = append(filters, "status", 1)
if self.userId != 1 {
//普通管理员
adminAuthIds, _ := models.RoleAuthGetByIds(self.user.RoleIds)
adminAuthIdArr := strings.Split(adminAuthIds, ",")
filters = append(filters, "id__in", adminAuthIdArr)
}
result, _ := models.AuthGetList(1, 1000, filters...)
list := make([]map[string]interface{}, len(result))
list2 := make([]map[string]interface{}, len(result))
allow_url := ""
i, j := 0, 0
for _, v := range result {
if v.AuthUrl != " " || v.AuthUrl != "/" {
allow_url += v.AuthUrl
}
row := make(map[string]interface{})
if v.Pid == 1 && v.IsShow == 1 {
row["Id"] = int(v.Id)
row["Sort"] = v.Sort
row["AuthName"] = v.AuthName
row["AuthUrl"] = v.AuthUrl
row["Icon"] = v.Icon
row["Pid"] = int(v.Pid)
list[i] = row
i++
cheMen, found := utils.Che.Get("menu" + utils.Int2String(self.user.Id))
if found && cheMen != nil { //从缓存取菜单
menu := cheMen.(*CheMenu)
//fmt.Println("调用显示菜单")
self.Data["SideMenu1"] = menu.List1 //一级菜单
self.Data["SideMenu2"] = menu.List2 //二级菜单
self.allowUrl = menu.AllowUrl
} else {
// 左侧导航栏
filters := make([]interface{}, 0)
filters = append(filters, "status", 1)
if self.userId != 1 {
//普通管理员
adminAuthIds, _ := models.RoleAuthGetByIds(self.user.RoleIds)
adminAuthIdArr := strings.Split(adminAuthIds, ",")
filters = append(filters, "id__in", adminAuthIdArr)
}
if v.Pid != 1 && v.IsShow == 1 {
row["Id"] = int(v.Id)
row["Sort"] = v.Sort
row["AuthName"] = v.AuthName
row["AuthUrl"] = v.AuthUrl
row["Icon"] = v.Icon
row["Pid"] = int(v.Pid)
list2[j] = row
j++
result, _ := models.AuthGetList(1, 1000, filters...)
list := make([]map[string]interface{}, len(result))
list2 := make([]map[string]interface{}, len(result))
allow_url := ""
i, j := 0, 0
for _, v := range result {
if v.AuthUrl != " " || v.AuthUrl != "/" {
allow_url += v.AuthUrl
}
row := make(map[string]interface{})
if v.Pid == 1 && v.IsShow == 1 {
row["Id"] = int(v.Id)
row["Sort"] = v.Sort
row["AuthName"] = v.AuthName
row["AuthUrl"] = v.AuthUrl
row["Icon"] = v.Icon
row["Pid"] = int(v.Pid)
list[i] = row
i++
}
if v.Pid != 1 && v.IsShow == 1 {
row["Id"] = int(v.Id)
row["Sort"] = v.Sort
row["AuthName"] = v.AuthName
row["AuthUrl"] = v.AuthUrl
row["Icon"] = v.Icon
row["Pid"] = int(v.Pid)
list2[j] = row
j++
}
}
self.Data["SideMenu1"] = list[:i] //一级菜单
self.Data["SideMenu2"] = list2[:j] //二级菜单

self.allowUrl = allow_url + "/home/index"
cheM := &CheMenu{}
cheM.AllowUrl = self.allowUrl
cheM.List1 = self.Data["SideMenu1"].([]map[string]interface{})
cheM.List2 = self.Data["SideMenu2"].([]map[string]interface{})
utils.Che.Set("menu"+utils.Int2String(self.user.Id), cheM, cache.DefaultExpiration)
}

self.Data["SideMenu1"] = list[:i] //一级菜单
self.Data["SideMenu2"] = list2[:j] //二级菜单
}

self.allowUrl = allow_url + "/home/index"
type CheMenu struct {
List1 []map[string]interface{}
List2 []map[string]interface{}
AllowUrl string
}

// 是否POST提交
Expand Down
3 changes: 3 additions & 0 deletions controllers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/astaxie/beego"
"github.com/george518/PPGo_ApiAdmin/libs"
"github.com/george518/PPGo_ApiAdmin/models"
cache "github.com/patrickmn/go-cache"
"gitub.com/george518/PPGo_ApiAdmin/utils"
)

type LoginController struct {
Expand Down Expand Up @@ -46,6 +48,7 @@ func (self *LoginController) LoginIn() {
user.LastIp = self.getClientIp()
user.LastLogin = time.Now().Unix()
user.Update()
utils.Che.Set("uid"+utils.Int2String(user.Id), user, cache.DefaultExpiration)
authkey := libs.Md5([]byte(self.getClientIp() + "|" + user.Password + user.Salt))
self.Ctx.SetCookie("auth", strconv.Itoa(user.Id)+"|"+authkey, 7*86400)

Expand Down
3 changes: 3 additions & 0 deletions controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ package controllers
import (
"strings"
"time"
"yin/apiadmin/utils"

"github.com/george518/PPGo_ApiAdmin/libs"
"github.com/george518/PPGo_ApiAdmin/models"
cache "github.com/patrickmn/go-cache"
)

type UserController struct {
Expand All @@ -30,6 +32,7 @@ func (self *UserController) Edit() {
row["phone"] = Admin.Phone
row["email"] = Admin.Email
self.Data["admin"] = row
utils.Che.Set("uid"+utils.Int2String(self.user.Id), nil, cache.DefaultExpiration)
self.display()
}

Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package main

import (
"time"

"github.com/george518/PPGo_ApiAdmin/models"
_ "github.com/george518/PPGo_ApiAdmin/routers"

"github.com/astaxie/beego"
"github.com/george518/PPGo_ApiAdmin/utils"
cache "github.com/patrickmn/go-cache"
)

func main() {
models.Init()
utils.Che = cache.New(60*time.Minute, 120*time.Minute)
beego.Run()
}

0 comments on commit 482e37f

Please sign in to comment.