package userService import ( "GoClouds/core/comm" "GoClouds/models" "GoClouds/modeluis" "GoClouds/service/sysService" "encoding/json" "fmt" "github.com/go-macaron/cache" ruisUtil "github.com/mgr9525/go-ruisutil" "github.com/xormplus/builder" "gopkg.in/macaron.v1" "strings" ) func FindTreePermission() []*modeluis.TreePermssion { rets := make([]*modeluis.TreePermssion, 0) err := comm.DbSys.Where("parent is null or parent=''").OrderBy("sort ASC,id ASC").Find(&rets) if err != nil { println("findPermChilds err:" + err.Error()) return nil } for _, v := range rets { findPermChilds(v, nil) } return rets } func findPermChilds(parent *modeluis.TreePermssion, upms map[string]bool) { if parent == nil { return } childs := make([]*modeluis.TreePermssion, 0) err := comm.DbSys.Where("parent=?", parent.Xid).OrderBy("sort ASC,id ASC").Find(&childs) if err != nil { println("findPermChilds err:" + err.Error()) return } for _, v := range childs { findPermChilds(v, upms) } if len(childs) > 0 && len(parent.Value) > 0 { t := *parent ts := []*modeluis.TreePermssion{&t} childs = append(ts, childs...) parent.Xid = "" } parent.Childs = childs } func FindUPermissions(uid string) []*modeluis.SysPermssion { if len(uid) <= 0 { return nil } perms := make(map[string]*modeluis.SysPermssion) userRole := FindUserRole(uid) //comm.DbSys.SqlTemplateClient("role.stpl") //rids:=strings.Split(userRole.RoleCodes,",") ses := comm.DbSys.Where("xid='common'") if len(userRole.RoleCodes) > 0 { rcds := strings.Split(userRole.RoleCodes, ",") ses.Or(builder.In("xid", rcds)) } roles := make([]*models.SysRole, 0) err := ses.Find(&roles) if err != nil { println("FindUserPermission err:" + err.Error()) return nil } for _, role := range roles { if len(role.Perms) <= 0 { continue } permssions := make([]*modeluis.SysPermssion, 0) ses := comm.DbSys.Where(builder.In("xid", strings.Split(role.Perms, ","))) err := ses.Find(&permssions) if err != nil { println("FindUserPermission err:" + err.Error()) continue } limids := map[string]bool{} if len(userRole.Limits) > 0 { rcds := strings.Split(userRole.Limits, ",") for _, v := range rcds { limids[v] = true } } for _, perm := range permssions { if len(perm.Value) > 0 { if limids[perm.Xid] == true { perm.Had = true } perms[perm.Value] = perm } } } rets := make([]*modeluis.SysPermssion, 0) for _, v := range perms { rets = append(rets, v) } return rets } func FindUserRole(uid string) *models.SysUserRole { userRole := new(models.SysUserRole) ok, err := comm.DbSys.Where("user_code=?", uid).Get(userRole) if err != nil { println("FindUserPermission err:" + err.Error()) } else if !ok { userRole.UserCode = uid comm.DbSys.Insert(userRole) } return userRole } func FindUserRoles(uid string) []*modeluis.SysURole { userRole := FindUserRole(uid) rcds := make(map[string]bool) if len(userRole.RoleCodes) > 0 { rids := strings.Split(userRole.RoleCodes, ",") for _, v := range rids { rcds[v] = true } } roles := make([]*modeluis.SysURole, 0) err := comm.DbSys.Where("xid!='common'").Find(&roles) if err != nil { println("FindUserRoles:" + err.Error()) } for _, v := range roles { v.Had = rcds[v.Xid] == true v.Permls = sysService.FindPermissions(v.Perms) } return roles } func FindUserPermission(uid string) map[string]bool { perms := make(map[string]bool) userRole := FindUserRole(uid) //comm.DbSys.SqlTemplateClient("role.stpl") //rids:=strings.Split(userRole.RoleCodes,",") ses := comm.DbSys.Where("xid='common'") if len(userRole.RoleCodes) > 0 { rcds := strings.Split(userRole.RoleCodes, ",") ses.Or(builder.In("xid", rcds)) } roles := make([]*models.SysRole, 0) err := ses.Find(&roles) if err != nil { println("FindUserPermission err:" + err.Error()) return perms } for _, role := range roles { if len(role.Perms) <= 0 { continue } permssions := make([]*models.SysPermssion, 0) ses := comm.DbSys.Where(builder.In("xid", strings.Split(role.Perms, ","))) if len(userRole.Limits) > 0 { rcds := strings.Split(userRole.Limits, ",") ses.And(builder.NotIn("xid", rcds)) } err := ses.Find(&permssions) if err != nil { println("FindUserPermission err:" + err.Error()) continue } for _, perm := range permssions { if len(perm.Value) > 0 { perms[perm.Value] = true } } } return perms } func FindUserPermissionCache(uid string, ch cache.Cache) map[string]bool { key := fmt.Sprintf("user-permission:%s", uid) perms := make(map[string]bool) if ch.IsExist(key) { bts := ch.Get(key).([]byte) json.Unmarshal(bts, &perms) } else { perms = FindUserPermission(uid) bts, err := json.Marshal(perms) if err == nil { err = ch.Put(key, bts, 3600*10) if err != nil { println("cache put err:" + err.Error()) } } } return perms } func CheckPermission(uid, perm string, ch cache.Cache) (rb bool) { key := fmt.Sprintf("user-permission:%s", uid) defer ruisUtil.Recovers("CheckPermmsion", func(errs string) { rb = false ch.Delete(key) }) if len(perm) <= 0 { return false } perms := FindUserPermissionCache(uid, ch) return perms[perm] } func CheckUPermission(perm string, c *macaron.Context, ch cache.Cache) (rb bool) { defer ruisUtil.Recovers("CheckUPermmsion", func(errs string) { rb = false }) lguser := GetCurrentUser(c) if lguser == nil { return false } return CheckPermission(lguser.Xid, perm, ch) }