Line data Source code
1 : // Copyright (C) 2009 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.args4j; 16 : 17 : import static com.google.gerrit.util.cli.Localizable.localizable; 18 : 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.extensions.restapi.AuthException; 22 : import com.google.gerrit.server.ProjectUtil; 23 : import com.google.gerrit.server.permissions.PermissionBackend; 24 : import com.google.gerrit.server.permissions.PermissionBackendException; 25 : import com.google.gerrit.server.permissions.ProjectPermission; 26 : import com.google.gerrit.server.project.NoSuchProjectException; 27 : import com.google.gerrit.server.project.ProjectCache; 28 : import com.google.gerrit.server.project.ProjectState; 29 : import com.google.inject.Inject; 30 : import com.google.inject.assistedinject.Assisted; 31 : import java.util.Optional; 32 : import org.kohsuke.args4j.CmdLineException; 33 : import org.kohsuke.args4j.CmdLineParser; 34 : import org.kohsuke.args4j.OptionDef; 35 : import org.kohsuke.args4j.spi.OptionHandler; 36 : import org.kohsuke.args4j.spi.Parameters; 37 : import org.kohsuke.args4j.spi.Setter; 38 : 39 : public class ProjectHandler extends OptionHandler<ProjectState> { 40 9 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 41 : 42 : private final ProjectCache projectCache; 43 : private final PermissionBackend permissionBackend; 44 : 45 : @Inject 46 : public ProjectHandler( 47 : ProjectCache projectCache, 48 : PermissionBackend permissionBackend, 49 : @Assisted final CmdLineParser parser, 50 : @Assisted final OptionDef option, 51 : @Assisted final Setter<ProjectState> setter) { 52 9 : super(parser, option, setter); 53 9 : this.projectCache = projectCache; 54 9 : this.permissionBackend = permissionBackend; 55 9 : } 56 : 57 : @Override 58 : public final int parseArguments(Parameters params) throws CmdLineException { 59 6 : String projectName = params.getParameter(0); 60 : 61 6 : while (projectName.endsWith("/")) { 62 0 : projectName = projectName.substring(0, projectName.length() - 1); 63 : } 64 : 65 6 : while (projectName.startsWith("/")) { 66 : // Be nice and drop the leading "/" if supplied by an absolute path. 67 : // We don't have a file system hierarchy, just a flat namespace in 68 : // the database's Project entities. We never encode these with a 69 : // leading '/' but users might accidentally include them in Git URLs. 70 : // 71 4 : projectName = projectName.substring(1); 72 : } 73 : 74 6 : String nameWithoutSuffix = ProjectUtil.stripGitSuffix(projectName); 75 6 : Project.NameKey nameKey = Project.nameKey(nameWithoutSuffix); 76 : 77 : Optional<ProjectState> state; 78 : try { 79 6 : state = projectCache.get(nameKey); 80 6 : if (!state.isPresent()) { 81 0 : throw new CmdLineException(owner, localizable("project %s not found"), nameWithoutSuffix); 82 : } 83 : // Hidden projects(permitsRead = false) should only be accessible by the project owners. 84 : // READ_CONFIG is checked here because it's only allowed to project owners(ACCESS may also 85 : // be allowed for other users). Allowing project owners to access here will help them to view 86 : // and update the config of hidden projects easily. 87 : ProjectPermission permissionToCheck = 88 6 : state.get().statePermitsRead() ? ProjectPermission.ACCESS : ProjectPermission.READ_CONFIG; 89 6 : permissionBackend.currentUser().project(nameKey).check(permissionToCheck); 90 0 : } catch (AuthException e) { 91 0 : throw new CmdLineException( 92 0 : owner, localizable(new NoSuchProjectException(nameKey, e).getMessage())); 93 0 : } catch (PermissionBackendException e) { 94 0 : logger.atWarning().withCause(e).log("Cannot load project %s", nameWithoutSuffix); 95 0 : throw new CmdLineException( 96 0 : owner, localizable(new NoSuchProjectException(nameKey).getMessage())); 97 6 : } 98 : 99 6 : setter.addValue(state.get()); 100 6 : return 1; 101 : } 102 : 103 : @Override 104 : public final String getDefaultMetaVariable() { 105 1 : return "PROJECT"; 106 : } 107 : }