Returns the Names of all users
(ctx context.Context)
| 1080 | |
| 1081 | // Returns the Names of all users |
| 1082 | func (db *DatabaseContext) GetUserNames(ctx context.Context) (users []string, err error) { |
| 1083 | dbUserPrefix := db.MetadataKeys.UserKeyPrefix() |
| 1084 | startKey := dbUserPrefix |
| 1085 | limit := db.Options.QueryPaginationLimit |
| 1086 | if limit == 0 { |
| 1087 | limit = DefaultQueryPaginationLimit |
| 1088 | } |
| 1089 | |
| 1090 | userRe, err := regexp.Compile(`^` + dbUserPrefix + `[^:]*$`) |
| 1091 | if err != nil { |
| 1092 | return nil, err |
| 1093 | } |
| 1094 | users = []string{} |
| 1095 | |
| 1096 | outerLoop: |
| 1097 | for { |
| 1098 | results, err := db.QueryUsers(ctx, startKey, limit) |
| 1099 | if err != nil { |
| 1100 | return nil, err |
| 1101 | } |
| 1102 | |
| 1103 | var principalName string |
| 1104 | |
| 1105 | resultCount := 0 |
| 1106 | |
| 1107 | for { |
| 1108 | // startKey is inclusive for views, so need to skip first result if using non-empty startKey, as this results in an overlapping result |
| 1109 | var skipAddition bool |
| 1110 | if resultCount == 0 && startKey != dbUserPrefix { |
| 1111 | skipAddition = true |
| 1112 | } |
| 1113 | |
| 1114 | var queryRow QueryUsersRow |
| 1115 | found := results.Next(ctx, &queryRow) |
| 1116 | if !found { |
| 1117 | break |
| 1118 | } |
| 1119 | |
| 1120 | if !strings.HasPrefix(queryRow.ID, dbUserPrefix) { |
| 1121 | break |
| 1122 | } |
| 1123 | |
| 1124 | principalName = queryRow.Name |
| 1125 | startKey = queryRow.ID |
| 1126 | resultCount++ |
| 1127 | |
| 1128 | if !userRe.MatchString(queryRow.ID) { |
| 1129 | continue |
| 1130 | } |
| 1131 | |
| 1132 | if principalName != "" && !skipAddition { |
| 1133 | users = append(users, principalName) |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | closeErr := results.Close() |
| 1138 | if closeErr != nil { |
| 1139 | return nil, closeErr |
no test coverage detected