package authz import ( "fmt" "strings" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" ) const ( StoreRoleBoss = "StoreBoss" RoleNameSep = "/" RolePrefix = "Role" StoreRolePrefix = "Store" ) type RoleInfo struct { Name string `json:"name"` Description string `json:"description"` StoreID int `json:"storeID,omitempty"` // 如果这个值非0,表示门店角色 } var ( storeRoleDescriptionMap = map[string]string{ StoreRoleBoss: "门店老板", } StoreRoleList = []*RoleInfo{ &RoleInfo{ Name: StoreRoleBoss, Description: storeRoleDescriptionMap[StoreRoleBoss], }, } ) func GetRoleDescription(name string, storeID int) (description string) { if storeID != 0 { description = 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 *RoleInfo) { r = &RoleInfo{ Name: name, StoreID: storeID, Description: GetRoleDescription(name, storeID), } return r } func NewRoleByModel(conf *model.NewConfig) (r *RoleInfo) { if conf.Type != model.ConfigTypeRole { r = NewRole(conf.Key, 0) } return r } func NewRoleByFullName(fullRoleName string) (r *RoleInfo) { strList := strings.Split(fullRoleName, 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 (r *RoleInfo) GetFullName() (fullRoleName string) { strList := []string{ RolePrefix, r.Name, } if r.StoreID > 0 { strList = append(strList, StoreRolePrefix, utils.Int2Str(r.StoreID)) } fullRoleName = strings.Join(strList, RoleNameSep) return fullRoleName } func RoleName2StoreID(roleName string) (storeID int) { if list := strings.Split(roleName, RoleNameSep); len(list) == 4 { storeID = int(utils.Str2Int64(list[2])) } return storeID } func GetStoreIDFromRole(roleName string) (storeID int) { list := strings.Split(roleName, RoleNameSep) if len(list) == 3 { storeID = int(utils.Str2Int64WithDefault(list[2], 0)) } return storeID } func RoleList2StoreIDList(roleList []*RoleInfo) (storeIDs []int) { for _, v := range roleList { if v.StoreID > 0 { storeIDs = append(storeIDs, v.StoreID) } } return storeIDs } func FullRoleName2RoleList(fullRoleNameList []string) (roleList []*RoleInfo) { if len(fullRoleNameList) > 0 { roleList = make([]*RoleInfo, len(fullRoleNameList)) for k, v := range fullRoleNameList { roleList[k] = NewRoleByFullName(v) } } return roleList } func RoleList2Map(roleList []*RoleInfo) (roleMap map[string]*RoleInfo) { roleMap = make(map[string]*RoleInfo) for _, v := range roleList { roleMap[v.Name] = v } return roleMap }