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.group.GroupResource; 26 : import com.google.gerrit.server.group.SubgroupResource; 27 : import com.google.inject.Inject; 28 : import com.google.inject.Singleton; 29 : 30 : @Singleton 31 : public class SubgroupsCollection implements ChildCollection<GroupResource, SubgroupResource> { 32 : private final DynamicMap<RestView<SubgroupResource>> views; 33 : private final ListSubgroups list; 34 : private final GroupsCollection groupsCollection; 35 : 36 : @Inject 37 : SubgroupsCollection( 38 : DynamicMap<RestView<SubgroupResource>> views, 39 : ListSubgroups list, 40 138 : GroupsCollection groupsCollection) { 41 138 : this.views = views; 42 138 : this.list = list; 43 138 : this.groupsCollection = groupsCollection; 44 138 : } 45 : 46 : @Override 47 : public RestView<GroupResource> list() { 48 1 : return list; 49 : } 50 : 51 : @Override 52 : public SubgroupResource parse(GroupResource resource, IdString id) 53 : throws NotInternalGroupException, AuthException, ResourceNotFoundException { 54 1 : GroupDescription.Internal parent = 55 1 : resource.asInternalGroup().orElseThrow(NotInternalGroupException::new); 56 : 57 1 : GroupDescription.Basic member = 58 1 : groupsCollection.parse(TopLevelResource.INSTANCE, id).getGroup(); 59 1 : if (resource.getControl().canSeeGroup() && isSubgroup(parent, member)) { 60 1 : return new SubgroupResource(resource, member); 61 : } 62 0 : throw new ResourceNotFoundException(id); 63 : } 64 : 65 : private static boolean isSubgroup( 66 : GroupDescription.Internal parent, GroupDescription.Basic member) { 67 1 : return parent.getSubgroups().contains(member.getGroupUUID()); 68 : } 69 : 70 : @Override 71 : public DynamicMap<RestView<SubgroupResource>> views() { 72 1 : return views; 73 : } 74 : }