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