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.config; 16 : 17 : import static com.google.common.collect.ImmutableMap.toImmutableMap; 18 : 19 : import com.google.common.collect.ImmutableMap; 20 : import com.google.gerrit.common.data.GlobalCapability; 21 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 22 : import com.google.gerrit.extensions.restapi.Response; 23 : import com.google.gerrit.extensions.restapi.RestReadView; 24 : import com.google.gerrit.server.config.CapabilityConstants; 25 : import com.google.gerrit.server.config.ConfigResource; 26 : import com.google.gerrit.server.permissions.PermissionBackend; 27 : import com.google.gerrit.server.permissions.PluginPermissionsUtil; 28 : import com.google.inject.Inject; 29 : import com.google.inject.Singleton; 30 : import java.util.HashMap; 31 : import java.util.Map; 32 : 33 : /** List capabilities visible to the calling user. */ 34 : @Singleton 35 : public class ListCapabilities implements RestReadView<ConfigResource> { 36 : private final PermissionBackend permissionBackend; 37 : private final PluginPermissionsUtil pluginPermissionsUtil; 38 : 39 : @Inject 40 : public ListCapabilities( 41 139 : PermissionBackend permissionBackend, PluginPermissionsUtil pluginPermissionsUtil) { 42 139 : this.permissionBackend = permissionBackend; 43 139 : this.pluginPermissionsUtil = pluginPermissionsUtil; 44 139 : } 45 : 46 : @Override 47 : public Response<Map<String, CapabilityInfo>> apply(ConfigResource resource) 48 : throws ResourceNotFoundException, IllegalAccessException, NoSuchFieldException { 49 2 : permissionBackend.checkUsesDefaultCapabilities(); 50 2 : return Response.ok( 51 2 : ImmutableMap.<String, CapabilityInfo>builder() 52 2 : .putAll(collectCoreCapabilities()) 53 2 : .putAll(collectPluginCapabilities()) 54 2 : .build()); 55 : } 56 : 57 : public Map<String, CapabilityInfo> collectPluginCapabilities() { 58 2 : return convertToPermissionInfos(pluginPermissionsUtil.collectPluginCapabilities()); 59 : } 60 : 61 : private static ImmutableMap<String, CapabilityInfo> convertToPermissionInfos( 62 : ImmutableMap<String, String> permissionIdNames) { 63 2 : return permissionIdNames.entrySet().stream() 64 2 : .collect( 65 2 : toImmutableMap(Map.Entry::getKey, e -> new CapabilityInfo(e.getKey(), e.getValue()))); 66 : } 67 : 68 : private Map<String, CapabilityInfo> collectCoreCapabilities() 69 : throws IllegalAccessException, NoSuchFieldException { 70 2 : Map<String, CapabilityInfo> output = new HashMap<>(); 71 2 : Class<? extends CapabilityConstants> bundleClass = CapabilityConstants.get().getClass(); 72 2 : CapabilityConstants c = CapabilityConstants.get(); 73 2 : for (String id : GlobalCapability.getAllNames()) { 74 2 : String name = (String) bundleClass.getField(id).get(c); 75 2 : output.put(id, new CapabilityInfo(id, name)); 76 2 : } 77 2 : return output; 78 : } 79 : 80 : public static class CapabilityInfo { 81 : public String id; 82 : public String name; 83 : 84 2 : public CapabilityInfo(String id, String name) { 85 2 : this.id = id; 86 2 : this.name = name; 87 2 : } 88 : } 89 : }