Line data Source code
1 : // Copyright (C) 2014 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.config; 16 : 17 : import com.google.gerrit.entities.Project; 18 : import com.google.gerrit.extensions.registration.DynamicMap; 19 : import com.google.gerrit.extensions.restapi.AuthException; 20 : import com.google.gerrit.extensions.restapi.ChildCollection; 21 : import com.google.gerrit.extensions.restapi.IdString; 22 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 23 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 24 : import com.google.gerrit.extensions.restapi.RestView; 25 : import com.google.gerrit.server.CurrentUser; 26 : import com.google.gerrit.server.config.ConfigResource; 27 : import com.google.gerrit.server.config.TaskResource; 28 : import com.google.gerrit.server.git.WorkQueue; 29 : import com.google.gerrit.server.git.WorkQueue.ProjectTask; 30 : import com.google.gerrit.server.git.WorkQueue.Task; 31 : import com.google.gerrit.server.permissions.GlobalPermission; 32 : import com.google.gerrit.server.permissions.PermissionBackend; 33 : import com.google.gerrit.server.permissions.PermissionBackendException; 34 : import com.google.gerrit.server.permissions.ProjectPermission; 35 : import com.google.gerrit.server.project.ProjectCache; 36 : import com.google.gerrit.server.project.ProjectState; 37 : import com.google.inject.Inject; 38 : import com.google.inject.Provider; 39 : import com.google.inject.Singleton; 40 : import java.util.Optional; 41 : 42 : @Singleton 43 : public class TasksCollection implements ChildCollection<ConfigResource, TaskResource> { 44 : private final DynamicMap<RestView<TaskResource>> views; 45 : private final ListTasks list; 46 : private final WorkQueue workQueue; 47 : private final Provider<CurrentUser> self; 48 : private final PermissionBackend permissionBackend; 49 : private final ProjectCache projectCache; 50 : 51 : @Inject 52 : TasksCollection( 53 : DynamicMap<RestView<TaskResource>> views, 54 : ListTasks list, 55 : WorkQueue workQueue, 56 : Provider<CurrentUser> self, 57 : PermissionBackend permissionBackend, 58 138 : ProjectCache projectCache) { 59 138 : this.views = views; 60 138 : this.list = list; 61 138 : this.workQueue = workQueue; 62 138 : this.self = self; 63 138 : this.permissionBackend = permissionBackend; 64 138 : this.projectCache = projectCache; 65 138 : } 66 : 67 : @Override 68 : public RestView<ConfigResource> list() { 69 2 : return list; 70 : } 71 : 72 : @Override 73 : public TaskResource parse(ConfigResource parent, IdString id) 74 : throws ResourceNotFoundException, AuthException, PermissionBackendException, 75 : ResourceConflictException { 76 2 : CurrentUser user = self.get(); 77 2 : if (!user.isIdentifiedUser()) { 78 0 : throw new AuthException("Authentication required"); 79 : } 80 : 81 : int taskId; 82 : try { 83 2 : taskId = (int) Long.parseLong(id.get(), 16); 84 0 : } catch (NumberFormatException e) { 85 0 : throw new ResourceNotFoundException(id, e); 86 2 : } 87 : 88 2 : Task<?> task = workQueue.getTask(taskId); 89 2 : if (task instanceof ProjectTask) { 90 0 : Project.NameKey nameKey = ((ProjectTask<?>) task).getProjectNameKey(); 91 0 : Optional<ProjectState> state = projectCache.get(nameKey); 92 0 : if (!state.isPresent()) { 93 0 : throw new ResourceNotFoundException(String.format("project %s not found", nameKey)); 94 : } 95 : 96 0 : state.get().checkStatePermitsRead(); 97 0 : if (permissionBackend.user(user).project(nameKey).test(ProjectPermission.ACCESS)) { 98 0 : return new TaskResource(task); 99 : } 100 : } 101 : 102 2 : if (task != null) { 103 2 : if (permissionBackend.user(user).test(GlobalPermission.VIEW_QUEUE)) { 104 2 : return new TaskResource(task); 105 : } 106 : } 107 : 108 1 : throw new ResourceNotFoundException(id); 109 : } 110 : 111 : @Override 112 : public DynamicMap<RestView<TaskResource>> views() { 113 2 : return views; 114 : } 115 : }