Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复菜单显示问题 #37

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

admin
2 changes: 1 addition & 1 deletion Router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package admin

import (
"github.com/astaxie/beego"
"github.com/beego/admin/src/rbac"
"admin/src/rbac"
)

func router() {
Expand Down
4 changes: 2 additions & 2 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"

"github.com/astaxie/beego"
. "github.com/beego/admin/src/lib"
"github.com/beego/admin/src/models"
. "admin/src/lib"
"admin/src/models"
)

const VERSION = "0.1.1"
Expand Down
8 changes: 4 additions & 4 deletions src/models/AdminInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"log"
"os"

. "admin/src/lib"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
. "github.com/beego/admin/src/lib"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
Expand Down Expand Up @@ -52,14 +52,14 @@ func Connect() {
db_sslmode := beego.AppConfig.String("db_sslmode")
switch db_type {
case "mysql":
orm.RegisterDriver("mysql", orm.DR_MySQL)
orm.RegisterDriver("mysql", orm.DRMySQL)
dns = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", db_user, db_pass, db_host, db_port, db_name)
break
case "postgres":
orm.RegisterDriver("postgres", orm.DR_Postgres)
orm.RegisterDriver("postgres", orm.DRPostgres)
dns = fmt.Sprintf("dbname=%s host=%s user=%s password=%s port=%s sslmode=%s", db_name, db_host, db_user, db_pass, db_port, db_sslmode)
case "sqlite3":
orm.RegisterDriver("sqlite3", orm.DR_Sqlite)
orm.RegisterDriver("sqlite3", orm.DRSqlite)
if db_path == "" {
db_path = "./"
}
Expand Down
1 change: 1 addition & 0 deletions src/models/RoleModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func DelGroupNode(roleid int64, groupid int64) error {
}
return nil
}

func AddRoleNode(roleid int64, nodeid int64) (int64, error) {
o := orm.NewOrm()
role := Role{Id: roleid}
Expand Down
2 changes: 1 addition & 1 deletion src/models/UserModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"log"
"time"

. "admin/src/lib"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/validation"
. "github.com/beego/admin/src/lib"
)

//用户表
Expand Down
6 changes: 3 additions & 3 deletions src/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strconv"
"strings"

. "admin/src/lib"
m "admin/src/models"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
. "github.com/beego/admin/src/lib"
m "github.com/beego/admin/src/models"
)

//check access and register user's nodes
Expand Down Expand Up @@ -43,7 +43,7 @@ func AccessRegister() {

ret := AccessDecision(params, accesslist)
if !ret {
ctx.Output.Json(&map[string]interface{}{"status": false, "info": "权限不足"}, true, false)
ctx.Output.JSON(&map[string]interface{}{"status": false, "info": "权限不足"}, true, false)
}
}

Expand Down
147 changes: 137 additions & 10 deletions src/rbac/common.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package rbac

import (
. "admin/src"
m "admin/src/models"
"fmt"
"github.com/astaxie/beego"
. "github.com/beego/admin/src"
m "github.com/beego/admin/src/models"
"strings"
)

type CommonController struct {
Expand All @@ -13,7 +15,7 @@ type CommonController struct {

func (this *CommonController) Rsp(status bool, str string) {
this.Data["json"] = &map[string]interface{}{"status": status, "info": str}
this.ServeJson()
this.ServeJSON()
}

func (this *CommonController) GetTemplatetype() string {
Expand All @@ -24,25 +26,150 @@ func (this *CommonController) GetTemplatetype() string {
return templatetype
}

func (this *CommonController) GetTree() []Tree {
func (this *CommonController) GetTree(userinfo interface{}) []Tree {
nodes, _ := m.GetNodeTree(0, 1)
tree := make([]Tree, len(nodes))
if nil == userinfo {
return tree
}
fmt.Println("******* userinfo:", userinfo)
accesslist, _ := GetAccessRightList(userinfo.(m.User).Id)
fmt.Println("******* accesslist:", accesslist)
adminuser := beego.AppConfig.String("rbac_admin_user")
isAdminUser := false
if userinfo.(m.User).Username == adminuser {
isAdminUser = true
}
for k, v := range nodes {
tree[k].Id = v["Id"].(int64)
tree[k].Text = v["Title"].(string)
children, _ := m.GetNodeTree(v["Id"].(int64), 2)
tree[k].Children = make([]Tree, len(children))
for k1, v1 := range children {
tree[k].Children[k1].Id = v1["Id"].(int64)
tree[k].Children[k1].Text = v1["Title"].(string)
tree[k].Children[k1].Attributes.Url = "/" + v["Name"].(string) + "/" + v1["Name"].(string)
tree[k].Children = []Tree{}
for _, v1 := range children {
url := v["Name"].(string) + "/" + v1["Name"].(string)
if !isAdminUser {
if r := hasAccessRight(accesslist, url); !r {
continue
}
}
node := Tree{}
node.Id = v1["Id"].(int64)
node.Text = v1["Title"].(string)
node.Attributes.Url = "/" + url
tree[k].Children = append(tree[k].Children, node)
}
}
for i := 0; i < len(tree); i++ {
if len(tree[i].Children) == 0 {
if i == len(tree) {
tree = tree[:i]
break
} else {
tree = append(tree[:i], tree[i+1:]...)
i--
}

}
}

return tree
}

//Access permissions list
func GetAccessRightList(uid int64) (map[string]bool, error) {
list, err := m.AccessList(uid)
if err != nil {
return nil, err
}
alist := make([]*AccessNode, 0)
for _, l := range list {
if l["Pid"].(int64) == 0 && l["Level"].(int64) == 1 {
anode := new(AccessNode)
anode.Id = l["Id"].(int64)
anode.Name = l["Name"].(string)
alist = append(alist, anode)
}
}
for _, l := range list {
if l["Level"].(int64) == 2 {
for _, an := range alist {
if an.Id == l["Pid"].(int64) {
anode := new(AccessNode)
anode.Id = l["Id"].(int64)
anode.Name = l["Name"].(string)
an.Childrens = append(an.Childrens, anode)
}
}
}
}
for _, l := range list {
if l["Level"].(int64) == 3 {
for _, an := range alist {
for _, an1 := range an.Childrens {
if an1.Id == l["Pid"].(int64) {
anode := new(AccessNode)
anode.Id = l["Id"].(int64)
anode.Name = l["Name"].(string)
an1.Childrens = append(an1.Childrens, anode)
}
}

}
}
}
accesslist := make(map[string]bool)
for _, v := range alist {
for _, v1 := range v.Childrens {
for _, v2 := range v1.Childrens {
vname := strings.Split(v.Name, "/")
v1name := strings.Split(v1.Name, "/")
v2name := strings.Split(v2.Name, "/")
str := fmt.Sprintf("%s/%s/%s", strings.ToLower(vname[0]), strings.ToLower(v1name[0]), strings.ToLower(v2name[0]))
accesslist[str] = true
}
}
}
return accesslist, nil
}

func hasAccessRight(accesslist interface{}, url string) bool {
params := strings.Split(strings.ToLower(url), "/")
ret := AccessRightDecision(params, accesslist.(map[string]bool))
return ret
}

//To test whether permissions
func AccessRightDecision(params []string, accesslist map[string]bool) bool {
if CheckAccessRight(params) {
s := fmt.Sprintf("%s/%s/%s", params[0], params[1], params[2])
if len(accesslist) < 1 {
return false
}
_, ok := accesslist[s]
if ok != false {
return true
}
} else {
return true
}
return false
}

//Determine whether need to verify
func CheckAccessRight(params []string) bool {
if len(params) < 3 {
return false
}
for _, nap := range strings.Split(beego.AppConfig.String("not_auth_package"), ",") {
if params[1] == nap {
return false
}
}
return true
}

func init() {

//验证权限
AccessRegister()
// AccessRegister()
}
6 changes: 3 additions & 3 deletions src/rbac/group.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package rbac

import (
m "github.com/beego/admin/src/models"
m "admin/src/models"
)

type GroupController struct {
Expand All @@ -23,10 +23,10 @@ func (this *GroupController) Index() {
}
nodes, count := m.GetGrouplist(page, page_size, sort)
this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
this.ServeJson()
this.ServeJSON()
return
} else {
this.TplNames = this.GetTemplatetype() + "/rbac/group.tpl"
this.TplName = this.GetTemplatetype() + "/rbac/group.tpl"
}

}
Expand Down
8 changes: 4 additions & 4 deletions src/rbac/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package rbac
import (
"encoding/json"

m "admin/src/models"
"github.com/astaxie/beego/orm"
m "github.com/beego/admin/src/models"
)

type NodeController struct {
Expand All @@ -13,7 +13,7 @@ type NodeController struct {

func (this *NodeController) Rsp(status bool, str string) {
this.Data["json"] = &map[string]interface{}{"status": status, "info": str}
this.ServeJson()
this.ServeJSON()
}

func (this *NodeController) Index() {
Expand Down Expand Up @@ -41,13 +41,13 @@ func (this *NodeController) Index() {
nodes = []orm.Params{}
}
this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
this.ServeJson()
this.ServeJSON()
return
} else {
grouplist := m.GroupList()
b, _ := json.Marshal(grouplist)
this.Data["grouplist"] = string(b)
this.TplNames = this.GetTemplatetype() + "/rbac/node.tpl"
this.TplName = this.GetTemplatetype() + "/rbac/node.tpl"
}

}
Expand Down
14 changes: 7 additions & 7 deletions src/rbac/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package rbac

import (
//"fmt"
. "admin/src"
m "admin/src/models"
"github.com/astaxie/beego"
. "github.com/beego/admin/src"
m "github.com/beego/admin/src/models"
)

type MainController struct {
Expand All @@ -31,20 +31,20 @@ func (this *MainController) Index() {
if userinfo == nil {
this.Ctx.Redirect(302, beego.AppConfig.String("rbac_auth_gateway"))
}
tree:=this.GetTree()
tree := this.GetTree(userinfo)
if this.IsAjax() {
this.Data["json"] = &tree
this.ServeJson()
this.ServeJSON()
return
} else {
groups := m.GroupList()
this.Data["userinfo"] = userinfo
this.Data["groups"] = groups
this.Data["tree"] = &tree
if this.GetTemplatetype() != "easyui"{
if this.GetTemplatetype() != "easyui" {
this.Layout = this.GetTemplatetype() + "/public/layout.tpl"
}
this.TplNames = this.GetTemplatetype() + "/public/index.tpl"
this.TplName = this.GetTemplatetype() + "/public/index.tpl"
}
}

Expand All @@ -71,7 +71,7 @@ func (this *MainController) Login() {
if userinfo != nil {
this.Ctx.Redirect(302, "/public/index")
}
this.TplNames = this.GetTemplatetype() + "/public/login.tpl"
this.TplName = this.GetTemplatetype() + "/public/login.tpl"
}

//退出
Expand Down
Loading