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.common.collect.ImmutableList; 18 : import com.google.common.collect.Sets; 19 : import com.google.gerrit.entities.AccountGroup; 20 : import com.google.gerrit.entities.GroupDescription; 21 : import com.google.gerrit.exceptions.NoSuchGroupException; 22 : import com.google.gerrit.extensions.restapi.AuthException; 23 : import com.google.gerrit.extensions.restapi.MethodNotAllowedException; 24 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 25 : import com.google.gerrit.extensions.restapi.Response; 26 : import com.google.gerrit.extensions.restapi.RestModifyView; 27 : import com.google.gerrit.extensions.restapi.UnprocessableEntityException; 28 : import com.google.gerrit.server.UserInitiated; 29 : import com.google.gerrit.server.account.GroupControl; 30 : import com.google.gerrit.server.group.GroupResolver; 31 : import com.google.gerrit.server.group.GroupResource; 32 : import com.google.gerrit.server.group.SubgroupResource; 33 : import com.google.gerrit.server.group.db.GroupDelta; 34 : import com.google.gerrit.server.group.db.GroupsUpdate; 35 : import com.google.gerrit.server.restapi.group.AddSubgroups.Input; 36 : import com.google.inject.Inject; 37 : import com.google.inject.Provider; 38 : import com.google.inject.Singleton; 39 : import java.io.IOException; 40 : import java.util.HashSet; 41 : import java.util.Set; 42 : import org.eclipse.jgit.errors.ConfigInvalidException; 43 : 44 : @Singleton 45 : public class DeleteSubgroups implements RestModifyView<GroupResource, Input> { 46 : private final GroupResolver groupResolver; 47 : private final Provider<GroupsUpdate> groupsUpdateProvider; 48 : 49 : @Inject 50 : DeleteSubgroups( 51 146 : GroupResolver groupResolver, @UserInitiated Provider<GroupsUpdate> groupsUpdateProvider) { 52 146 : this.groupResolver = groupResolver; 53 146 : this.groupsUpdateProvider = groupsUpdateProvider; 54 146 : } 55 : 56 : @Override 57 : public Response<?> apply(GroupResource resource, Input input) 58 : throws AuthException, NotInternalGroupException, UnprocessableEntityException, 59 : ResourceNotFoundException, IOException, ConfigInvalidException { 60 4 : GroupDescription.Internal internalGroup = 61 4 : resource.asInternalGroup().orElseThrow(NotInternalGroupException::new); 62 4 : input = Input.init(input); 63 : 64 4 : final GroupControl control = resource.getControl(); 65 4 : if (!control.canRemoveGroup()) { 66 0 : throw new AuthException( 67 0 : String.format("Cannot delete groups from group %s", internalGroup.getName())); 68 : } 69 : 70 4 : Set<AccountGroup.UUID> subgroupsToRemove = new HashSet<>(); 71 4 : for (String subgroupIdentifier : input.groups) { 72 4 : GroupDescription.Basic subgroup = groupResolver.parse(subgroupIdentifier); 73 4 : subgroupsToRemove.add(subgroup.getGroupUUID()); 74 4 : } 75 : 76 4 : AccountGroup.UUID groupUuid = internalGroup.getGroupUUID(); 77 : try { 78 4 : removeSubgroups(groupUuid, subgroupsToRemove); 79 0 : } catch (NoSuchGroupException e) { 80 0 : throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e); 81 4 : } 82 : 83 4 : return Response.none(); 84 : } 85 : 86 : private void removeSubgroups( 87 : AccountGroup.UUID parentGroupUuid, Set<AccountGroup.UUID> removedSubgroupUuids) 88 : throws NoSuchGroupException, IOException, ConfigInvalidException { 89 : GroupDelta groupDelta = 90 4 : GroupDelta.builder() 91 4 : .setSubgroupModification( 92 4 : subgroupUuids -> Sets.difference(subgroupUuids, removedSubgroupUuids)) 93 4 : .build(); 94 4 : groupsUpdateProvider.get().updateGroup(parentGroupUuid, groupDelta); 95 4 : } 96 : 97 : @Singleton 98 : public static class DeleteSubgroup implements RestModifyView<SubgroupResource, Input> { 99 : 100 : private final Provider<DeleteSubgroups> delete; 101 : 102 : @Inject 103 138 : public DeleteSubgroup(Provider<DeleteSubgroups> delete) { 104 138 : this.delete = delete; 105 138 : } 106 : 107 : @Override 108 : public Response<?> apply(SubgroupResource resource, Input input) 109 : throws AuthException, MethodNotAllowedException, UnprocessableEntityException, 110 : ResourceNotFoundException, IOException, ConfigInvalidException { 111 1 : AddSubgroups.Input in = new AddSubgroups.Input(); 112 1 : in.groups = ImmutableList.of(resource.getMember().get()); 113 1 : return delete.get().apply(resource, in); 114 : } 115 : } 116 : }