Files
jx-callback/business/authz/autils/authz_utils.go
2019-09-09 12:03:07 +08:00

102 lines
2.5 KiB
Go

package autils
import (
"fmt"
"strings"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/authz"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
)
func GetRoleDescription(name string, storeID int) (description string) {
if storeID != 0 {
description = authz.StoreRoleDescriptionMap[name]
} else {
if confList, err := dao.QueryConfigs(dao.GetDB(), name, model.ConfigTypeRole, ""); err == nil && len(confList) > 0 {
description = confList[0].Value
}
}
return description
}
func ValidateRole(name string, storeID int) (err error) {
if GetRoleDescription(name, storeID) == "" {
err = fmt.Errorf("角色%s不合法", name)
}
return err
}
func NewRole(name string, storeID int) (r *authz.RoleInfo) {
r = &authz.RoleInfo{
Name: name,
StoreID: storeID,
Description: GetRoleDescription(name, storeID),
}
return r
}
func NewStoreBossRole(storeID int) (r *authz.RoleInfo) {
return NewRole(authz.StoreRoleBoss, storeID)
}
func NewRoleByModel(conf *model.NewConfig) (r *authz.RoleInfo) {
if conf.Type != model.ConfigTypeRole {
r = NewRole(conf.Key, 0)
}
return r
}
func NewRoleByFullName(fullRoleName string) (r *authz.RoleInfo) {
strList := strings.Split(fullRoleName, authz.RoleNameSep)
if len(strList) == 2 {
r = NewRole(strList[1], 0)
} else if len(strList) == 4 {
r = NewRole(strList[1], int(utils.Str2Int64(strList[3])))
}
return r
}
func RoleName2StoreID(roleName string) (storeID int) {
if list := strings.Split(roleName, authz.RoleNameSep); len(list) == 4 {
storeID = int(utils.Str2Int64(list[2]))
}
return storeID
}
func GetStoreIDFromRole(roleName string) (storeID int) {
list := strings.Split(roleName, authz.RoleNameSep)
if len(list) == 3 {
storeID = int(utils.Str2Int64WithDefault(list[2], 0))
}
return storeID
}
func RoleList2StoreIDList(roleList []*authz.RoleInfo) (storeIDs []int) {
for _, v := range roleList {
if v.StoreID > 0 {
storeIDs = append(storeIDs, v.StoreID)
}
}
return storeIDs
}
func FullRoleName2RoleList(fullRoleNameList []string) (roleList []*authz.RoleInfo) {
if len(fullRoleNameList) > 0 {
roleList = make([]*authz.RoleInfo, len(fullRoleNameList))
for k, v := range fullRoleNameList {
roleList[k] = NewRoleByFullName(v)
}
}
return roleList
}
func RoleList2Map(roleList []*authz.RoleInfo) (roleMap map[string]*authz.RoleInfo) {
roleMap = make(map[string]*authz.RoleInfo)
for _, v := range roleList {
roleMap[v.Name] = v
}
return roleMap
}