|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.security; |
| 20 | + |
| 21 | +import org.elasticsearch.client.security.user.User; |
| 22 | +import org.elasticsearch.common.Nullable; |
| 23 | +import org.elasticsearch.common.ParseField; |
| 24 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 26 | +import org.elasticsearch.common.xcontent.XContentParser.Token; |
| 27 | +import org.elasticsearch.common.xcontent.XContentParserUtils; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Collection; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.HashSet; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Objects; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 38 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; |
| 39 | + |
| 40 | +/** |
| 41 | + * Response when requesting zero or more users. |
| 42 | + * Returns a List of {@link User} objects |
| 43 | + */ |
| 44 | +public class GetUsersResponse { |
| 45 | + private final Set<User> users; |
| 46 | + private final Set<User> enabledUsers; |
| 47 | + |
| 48 | + public GetUsersResponse(Set<User> users, Set<User> enabledUsers) { |
| 49 | + this.users = Collections.unmodifiableSet(users); |
| 50 | + this.enabledUsers = Collections.unmodifiableSet(enabledUsers); |
| 51 | + } |
| 52 | + |
| 53 | + public Set<User> getUsers() { |
| 54 | + return users; |
| 55 | + } |
| 56 | + |
| 57 | + public Set<User> getEnabledUsers() { |
| 58 | + return enabledUsers; |
| 59 | + } |
| 60 | + |
| 61 | + public static GetUsersResponse fromXContent(XContentParser parser) throws IOException { |
| 62 | + XContentParserUtils.ensureExpectedToken(Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); |
| 63 | + final Set<User> users = new HashSet<>(); |
| 64 | + final Set<User> enabledUsers = new HashSet<>(); |
| 65 | + Token token; |
| 66 | + while ((token = parser.nextToken()) != Token.END_OBJECT) { |
| 67 | + XContentParserUtils.ensureExpectedToken(Token.FIELD_NAME, token, parser::getTokenLocation); |
| 68 | + ParsedUser parsedUser = USER_PARSER.parse(parser, parser.currentName()); |
| 69 | + users.add(parsedUser.user); |
| 70 | + if (parsedUser.enabled) { |
| 71 | + enabledUsers.add(parsedUser.user); |
| 72 | + } |
| 73 | + } |
| 74 | + return new GetUsersResponse(users, enabledUsers); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean equals(Object o) { |
| 79 | + if (this == o) return true; |
| 80 | + if (!(o instanceof GetUsersResponse)) return false; |
| 81 | + GetUsersResponse that = (GetUsersResponse) o; |
| 82 | + return Objects.equals(users, that.users); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public int hashCode() { |
| 87 | + return Objects.hash(users); |
| 88 | + } |
| 89 | + |
| 90 | + public static final ParseField USERNAME = new ParseField("username"); |
| 91 | + public static final ParseField ROLES = new ParseField("roles"); |
| 92 | + public static final ParseField FULL_NAME = new ParseField("full_name"); |
| 93 | + public static final ParseField EMAIL = new ParseField("email"); |
| 94 | + public static final ParseField METADATA = new ParseField("metadata"); |
| 95 | + public static final ParseField ENABLED = new ParseField("enabled"); |
| 96 | + |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + public static final ConstructingObjectParser<ParsedUser, String> USER_PARSER = new ConstructingObjectParser<>("user_info", |
| 99 | + (constructorObjects) -> { |
| 100 | + int i = 0; |
| 101 | + final String username = (String) constructorObjects[i++]; |
| 102 | + final Collection<String> roles = (Collection<String>) constructorObjects[i++]; |
| 103 | + final Map<String, Object> metadata = (Map<String, Object>) constructorObjects[i++]; |
| 104 | + final Boolean enabled = (Boolean) constructorObjects[i++]; |
| 105 | + final String fullName = (String) constructorObjects[i++]; |
| 106 | + final String email = (String) constructorObjects[i++]; |
| 107 | + return new ParsedUser(username, roles, metadata, enabled, fullName, email); |
| 108 | + }); |
| 109 | + |
| 110 | + static { |
| 111 | + USER_PARSER.declareString(constructorArg(), USERNAME); |
| 112 | + USER_PARSER.declareStringArray(constructorArg(), ROLES); |
| 113 | + USER_PARSER.declareObject(constructorArg(), (parser, c) -> parser.map(), METADATA); |
| 114 | + USER_PARSER.declareBoolean(constructorArg(), ENABLED); |
| 115 | + USER_PARSER.declareStringOrNull(optionalConstructorArg(), FULL_NAME); |
| 116 | + USER_PARSER.declareStringOrNull(optionalConstructorArg(), EMAIL); |
| 117 | + } |
| 118 | + |
| 119 | + protected static final class ParsedUser { |
| 120 | + protected User user; |
| 121 | + protected boolean enabled; |
| 122 | + |
| 123 | + public ParsedUser(String username, Collection<String> roles, Map<String, Object> metadata, Boolean enabled, |
| 124 | + @Nullable String fullName, @Nullable String email) { |
| 125 | + String checkedUsername = username = Objects.requireNonNull(username, "`username` is required, cannot be null"); |
| 126 | + Collection<String> checkedRoles = Collections.unmodifiableSet(new HashSet<>( |
| 127 | + Objects.requireNonNull(roles, "`roles` is required, cannot be null. Pass an empty Collection instead."))); |
| 128 | + Map<String, Object> checkedMetadata = Collections |
| 129 | + .unmodifiableMap(Objects.requireNonNull(metadata, "`metadata` is required, cannot be null. Pass an empty map instead.")); |
| 130 | + this.user = new User(checkedUsername, checkedRoles, checkedMetadata, fullName, email); |
| 131 | + this.enabled = enabled; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments