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.DuplicateKeyException; 21 : import com.google.gerrit.exceptions.NoSuchGroupException; 22 : import com.google.gerrit.extensions.common.NameInput; 23 : import com.google.gerrit.extensions.restapi.AuthException; 24 : import com.google.gerrit.extensions.restapi.BadRequestException; 25 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 26 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 27 : import com.google.gerrit.extensions.restapi.Response; 28 : import com.google.gerrit.extensions.restapi.RestModifyView; 29 : import com.google.gerrit.server.UserInitiated; 30 : import com.google.gerrit.server.group.GroupResource; 31 : import com.google.gerrit.server.group.db.GroupDelta; 32 : import com.google.gerrit.server.group.db.GroupsUpdate; 33 : import com.google.inject.Inject; 34 : import com.google.inject.Provider; 35 : import com.google.inject.Singleton; 36 : import java.io.IOException; 37 : import org.eclipse.jgit.errors.ConfigInvalidException; 38 : 39 : @Singleton 40 : public class PutName implements RestModifyView<GroupResource, NameInput> { 41 : private final Provider<GroupsUpdate> groupsUpdateProvider; 42 : 43 : @Inject 44 146 : PutName(@UserInitiated Provider<GroupsUpdate> groupsUpdateProvider) { 45 146 : this.groupsUpdateProvider = groupsUpdateProvider; 46 146 : } 47 : 48 : @Override 49 : public Response<String> apply(GroupResource rsrc, NameInput input) 50 : throws NotInternalGroupException, AuthException, BadRequestException, 51 : ResourceConflictException, ResourceNotFoundException, IOException, 52 : ConfigInvalidException { 53 2 : GroupDescription.Internal internalGroup = 54 2 : rsrc.asInternalGroup().orElseThrow(NotInternalGroupException::new); 55 2 : if (!rsrc.getControl().isOwner()) { 56 0 : throw new AuthException("Not group owner"); 57 2 : } else if (input == null || Strings.isNullOrEmpty(input.name)) { 58 1 : throw new BadRequestException("name is required"); 59 : } 60 1 : String newName = input.name.trim(); 61 1 : if (newName.isEmpty()) { 62 0 : throw new BadRequestException("name is required"); 63 : } 64 : 65 1 : if (internalGroup.getName().equals(newName)) { 66 1 : return Response.ok(newName); 67 : } 68 : 69 1 : renameGroup(internalGroup, newName); 70 1 : return Response.ok(newName); 71 : } 72 : 73 : private void renameGroup(GroupDescription.Internal group, String newName) 74 : throws ResourceConflictException, ResourceNotFoundException, IOException, 75 : ConfigInvalidException { 76 1 : AccountGroup.UUID groupUuid = group.getGroupUUID(); 77 1 : GroupDelta groupDelta = GroupDelta.builder().setName(AccountGroup.nameKey(newName)).build(); 78 : try { 79 1 : groupsUpdateProvider.get().updateGroup(groupUuid, groupDelta); 80 0 : } catch (NoSuchGroupException e) { 81 0 : throw new ResourceNotFoundException(String.format("Group %s not found", groupUuid), e); 82 1 : } catch (DuplicateKeyException e) { 83 1 : throw new ResourceConflictException("group with name " + newName + " already exists", e); 84 1 : } 85 1 : } 86 : }