You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
209 lines
5.1 KiB
209 lines
5.1 KiB
package userService
|
|
|
|
import (
|
|
"GoClouds/core/comms"
|
|
"GoClouds/models"
|
|
"GoClouds/modeluis"
|
|
"GoClouds/service/sysService"
|
|
"fmt"
|
|
gocloud "github.com/mgr9525/go-cloud"
|
|
"strings"
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
func FindTreePermission() []*modeluis.TreePermssion {
|
|
rets := make([]*modeluis.TreePermssion, 0)
|
|
err := comms.DbSysHelper.GetDB().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 := comms.DbSysHelper.GetDB().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.DbSysHelper.GetDB().SqlTemplateClient("role.stpl")
|
|
//rids:=strings.Split(userRole.RoleCodes,",")
|
|
ses := comms.DbSysHelper.GetDB().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 := comms.DbSysHelper.GetDB().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 := comms.DbSysHelper.GetDB().Where("user_code=?", uid).Get(userRole)
|
|
if err != nil {
|
|
println("FindUserPermission err:" + err.Error())
|
|
} else if !ok {
|
|
userRole.UserCode = uid
|
|
comms.DbSysHelper.GetDB().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 := comms.DbSysHelper.GetDB().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.DbSysHelper.GetDB().SqlTemplateClient("role.stpl")
|
|
//rids:=strings.Split(userRole.RoleCodes,",")
|
|
ses := comms.DbSysHelper.GetDB().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 := comms.DbSysHelper.GetDB().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) map[string]bool {
|
|
key := fmt.Sprintf("user-permission:%s", uid)
|
|
perms := make(map[string]bool)
|
|
err := gocloud.CacheGets(key, &perms)
|
|
if err != nil {
|
|
perms = FindUserPermission(uid)
|
|
gocloud.CacheSets(key, perms)
|
|
}
|
|
return perms
|
|
}
|
|
|
|
func CheckPermission(uid, perm string) (rb bool) {
|
|
if uid == "" || perm == "" {
|
|
return false
|
|
}
|
|
perms := FindUserPermissionCache(uid)
|
|
return perms[perm]
|
|
}
|
|
|
|
/*func CheckUPermission(perm string, c *gin.Context) (rb bool) {
|
|
defer ruisUtil.Recovers(func(errs interface{}) {
|
|
rb = false
|
|
})
|
|
lguser := userCloud.CurrUser(c)
|
|
if lguser == nil {
|
|
return false
|
|
}
|
|
|
|
return CheckPermission(lguser.Xid, perm)
|
|
}*/
|