Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.group; 16 : 17 : import com.google.gerrit.entities.GroupDescription; 18 : import com.google.gerrit.extensions.registration.DynamicMap; 19 : import com.google.gerrit.extensions.restapi.AuthException; 20 : import com.google.gerrit.extensions.restapi.ChildCollection; 21 : import com.google.gerrit.extensions.restapi.IdString; 22 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 23 : import com.google.gerrit.extensions.restapi.RestView; 24 : import com.google.gerrit.extensions.restapi.TopLevelResource; 25 : import com.google.gerrit.server.IdentifiedUser; 26 : import com.google.gerrit.server.group.GroupResource; 27 : import com.google.gerrit.server.group.MemberResource; 28 : import com.google.gerrit.server.restapi.account.AccountsCollection; 29 : import com.google.inject.Inject; 30 : import com.google.inject.Provider; 31 : import com.google.inject.Singleton; 32 : import java.io.IOException; 33 : import org.eclipse.jgit.errors.ConfigInvalidException; 34 : 35 : @Singleton 36 : public class MembersCollection implements ChildCollection<GroupResource, MemberResource> { 37 : private final DynamicMap<RestView<MemberResource>> views; 38 : private final Provider<ListMembers> list; 39 : private final AccountsCollection accounts; 40 : 41 : @Inject 42 : MembersCollection( 43 : DynamicMap<RestView<MemberResource>> views, 44 : Provider<ListMembers> list, 45 138 : AccountsCollection accounts) { 46 138 : this.views = views; 47 138 : this.list = list; 48 138 : this.accounts = accounts; 49 138 : } 50 : 51 : @Override 52 : public RestView<GroupResource> list() throws ResourceNotFoundException, AuthException { 53 1 : return list.get(); 54 : } 55 : 56 : @Override 57 : public MemberResource parse(GroupResource parent, IdString id) 58 : throws NotInternalGroupException, AuthException, ResourceNotFoundException, IOException, 59 : ConfigInvalidException { 60 2 : GroupDescription.Internal group = 61 2 : parent.asInternalGroup().orElseThrow(NotInternalGroupException::new); 62 : 63 1 : IdentifiedUser user = accounts.parse(TopLevelResource.INSTANCE, id).getUser(); 64 1 : if (parent.getControl().canSeeMember(user.getAccountId()) && isMember(group, user)) { 65 1 : return new MemberResource(parent, user); 66 : } 67 0 : throw new ResourceNotFoundException(id); 68 : } 69 : 70 : private static boolean isMember(GroupDescription.Internal group, IdentifiedUser user) { 71 1 : return group.getMembers().contains(user.getAccountId()); 72 : } 73 : 74 : @Override 75 : public DynamicMap<RestView<MemberResource>> views() { 76 2 : return views; 77 : } 78 : }