Performance optimize on accessor.
@@ -93,31 +93,49 @@ func contains(arr []string, val string) bool {
}
func (a *access) rolesForTeam(auth atc.TeamAuth) []string {
- roleSet := map[string]bool{}
-
- groups := a.groups()
connectorID := a.connectorID()
userID := a.userID()
+ if userID != "" {
+ userID = fmt.Sprintf("%v:%v", connectorID, userID)
+ }
userName := a.userName()
+ if userName != "" {
+ userName = fmt.Sprintf("%v:%v", connectorID, userName)
+ }
+ var groups []string
+ for _, group := range a.groups() {
+ if group != "" {
+ groups = append(groups, fmt.Sprintf("%v:%v", connectorID, group))
+ }
+ }
+ var roles []string
+ for role, roleAuth := range auth {
+ if roleOnTeam(userID, userName, groups, roleAuth) {
+ roles = append(roles, role)
+ }
+ }
+
+ return roles
+}
- for role, auth := range auth {
- userAuth := auth["users"]
- groupAuth := auth["groups"]
+func roleOnTeam(userID string, userName string, groups []string, roleAuth map[string][]string) bool {
+ userAuth := roleAuth["users"]
+ groupAuth := roleAuth["groups"]
// backwards compatibility for allow-all-users
if len(userAuth) == 0 && len(groupAuth) == 0 {
- roleSet[role] = true
+ return true
}
for _, user := range userAuth {
if userID != "" {
- if strings.EqualFold(user, fmt.Sprintf("%v:%v", connectorID, userID)) {
- roleSet[role] = true
+ if strings.EqualFold(user, userID) {
+ return true
}
}
if userName != "" {
- if strings.EqualFold(user, fmt.Sprintf("%v:%v", connectorID, userName)) {
- roleSet[role] = true
+ if strings.EqualFold(user, userName) {
+ return true
}
}
}
@@ -125,19 +143,14 @@ func (a *access) rolesForTeam(auth atc.TeamAuth) []string {
for _, group := range groupAuth {
for _, claimGroup := range groups {
if claimGroup != "" {
- if strings.EqualFold(group, fmt.Sprintf("%v:%v", connectorID, claimGroup)) {
- roleSet[role] = true
- }
+ if strings.EqualFold(group, claimGroup) {
+ return true
}
}
}
}
- var roles []string
- for role := range roleSet {
- roles = append(roles, role)
- }
- return roles
+ return false
}
func (a *access) HasToken() bool {
