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.gerrit.entities.AccountGroup; 19 : import com.google.gerrit.entities.GroupDescription; 20 : import com.google.gerrit.exceptions.NoSuchGroupException; 21 : import com.google.gerrit.extensions.api.groups.OwnerInput; 22 : import com.google.gerrit.extensions.common.GroupInfo; 23 : import com.google.gerrit.extensions.restapi.AuthException; 24 : import com.google.gerrit.extensions.restapi.BadRequestException; 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.group.GroupResolver; 31 : import com.google.gerrit.server.group.GroupResource; 32 : import com.google.gerrit.server.group.db.GroupDelta; 33 : import com.google.gerrit.server.group.db.GroupsUpdate; 34 : import com.google.gerrit.server.permissions.PermissionBackendException; 35 : import com.google.inject.Inject; 36 : import com.google.inject.Provider; 37 : import com.google.inject.Singleton; 38 : import java.io.IOException; 39 : import org.eclipse.jgit.errors.ConfigInvalidException; 40 : 41 : @Singleton 42 : public class PutOwner implements RestModifyView<GroupResource, OwnerInput> { 43 : private final GroupResolver groupResolver; 44 : private final Provider<GroupsUpdate> groupsUpdateProvider; 45 : private final GroupJson json; 46 : 47 : @Inject 48 : PutOwner( 49 : GroupResolver groupResolver, 50 : @UserInitiated Provider<GroupsUpdate> groupsUpdateProvider, 51 146 : GroupJson json) { 52 146 : this.groupResolver = groupResolver; 53 146 : this.groupsUpdateProvider = groupsUpdateProvider; 54 146 : this.json = json; 55 146 : } 56 : 57 : @Override 58 : public Response<GroupInfo> apply(GroupResource resource, OwnerInput input) 59 : throws ResourceNotFoundException, NotInternalGroupException, AuthException, 60 : BadRequestException, UnprocessableEntityException, IOException, ConfigInvalidException, 61 : PermissionBackendException { 62 2 : GroupDescription.Internal internalGroup = 63 2 : resource.asInternalGroup().orElseThrow(NotInternalGroupException::new); 64 2 : if (!resource.getControl().isOwner()) { 65 0 : throw new AuthException("Not group owner"); 66 : } 67 : 68 2 : if (input == null || Strings.isNullOrEmpty(input.owner)) { 69 1 : throw new BadRequestException("owner is required"); 70 : } 71 : 72 1 : GroupDescription.Basic owner = groupResolver.parse(input.owner); 73 1 : if (!internalGroup.getOwnerGroupUUID().equals(owner.getGroupUUID())) { 74 1 : AccountGroup.UUID groupUuid = internalGroup.getGroupUUID(); 75 1 : GroupDelta groupDelta = GroupDelta.builder().setOwnerGroupUUID(owner.getGroupUUID()).build(); 76 : try { 77 1 : groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta); 78 0 : } catch (NoSuchGroupException e) { 79 0 : throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e); 80 1 : } 81 : } 82 1 : return Response.ok(json.format(owner)); 83 : } 84 : }