- 重构authz结构
- 角色管理初版完成
This commit is contained in:
97
business/authz/autils/authz_utils.go
Normal file
97
business/authz/autils/authz_utils.go
Normal file
@@ -0,0 +1,97 @@
|
||||
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 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
|
||||
}
|
||||
Reference in New Issue
Block a user