| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- package agentserver
- import "strings"
- // RoleAllowed 判断用户是否可以使用新助手:allowedCSV 为空表示所有登录用户
- func RoleAllowed(userRoles []string, allowedCSV string) bool {
- allowed := splitCSV(allowedCSV)
- if len(allowed) == 0 {
- return true
- }
- for _, a := range allowed {
- for _, r := range userRoles {
- if r == a {
- return true
- }
- }
- }
- return false
- }
- // SelectProfile 按角色选择 Profile:byRole 形如 "ops:prf_a,admin:prf_b",
- // 按配置顺序取第一个用户拥有的角色;都不命中时返回 def(可为空,表示 API Key 的默认 Profile)
- func SelectProfile(userRoles []string, byRole, def string) string {
- for _, item := range splitCSV(byRole) {
- role, profile, ok := strings.Cut(item, ":")
- role, profile = strings.TrimSpace(role), strings.TrimSpace(profile)
- if !ok || role == "" || profile == "" {
- continue
- }
- for _, r := range userRoles {
- if r == role {
- return profile
- }
- }
- }
- return strings.TrimSpace(def)
- }
- func splitCSV(s string) []string {
- var out []string
- for _, p := range strings.Split(s, ",") {
- if p = strings.TrimSpace(p); p != "" {
- out = append(out, p)
- }
- }
- return out
- }
|