Line data Source code
1 : // Copyright (C) 2011 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.common.data; 16 : 17 : import com.google.gerrit.common.Nullable; 18 : import com.google.gerrit.entities.Permission; 19 : import com.google.gerrit.entities.PermissionRange; 20 : import java.util.ArrayList; 21 : import java.util.Arrays; 22 : import java.util.Collection; 23 : import java.util.Collections; 24 : import java.util.List; 25 : 26 : /** 27 : * Server wide capabilities. Represented as {@link Permission} objects. 28 : * 29 : * <p>Contrary to {@link Permission}, global capabilities do not need a resource to check 30 : * permissions on. 31 : */ 32 : public class GlobalCapability { 33 : /** Ability to view code review metadata refs in repositories. */ 34 : public static final String ACCESS_DATABASE = "accessDatabase"; 35 : 36 : /** 37 : * Denotes the server's administrators. 38 : * 39 : * <p>This is similar to UNIX root, or Windows SYSTEM account. Any user that has this capability 40 : * can perform almost any other action, or can grant themselves the power to perform any other 41 : * action on the site. Most of the other capabilities and permissions fall-back to the predicate 42 : * "OR user has capability ADMINISTRATE_SERVER". 43 : */ 44 : public static final String ADMINISTRATE_SERVER = "administrateServer"; 45 : 46 : /** Maximum number of changes that may be pushed in a batch. */ 47 : public static final String BATCH_CHANGES_LIMIT = "batchChangesLimit"; 48 : 49 : /** 50 : * Default maximum number of changes that may be pushed in a batch, 0 means no limit. This is just 51 : * used as a suggestion for prepopulating the field in the access UI. 52 : */ 53 : public static final int DEFAULT_MAX_BATCH_CHANGES_LIMIT = 0; 54 : 55 : /** Can create any account on the server. */ 56 : public static final String CREATE_ACCOUNT = "createAccount"; 57 : 58 : /** Can create any group on the server. */ 59 : public static final String CREATE_GROUP = "createGroup"; 60 : 61 : /** Can create any project on the server. */ 62 : public static final String CREATE_PROJECT = "createProject"; 63 : 64 : /** 65 : * Denotes who may email change reviewers and watchers. 66 : * 67 : * <p>This can be used to deny build bots from emailing reviewers and people who watch the change. 68 : * Instead, only the authors of the change and those who starred it will be emailed. The allow 69 : * rules are evaluated before deny rules, however the default is to allow emailing, if no explicit 70 : * rule is matched. 71 : */ 72 : public static final String EMAIL_REVIEWERS = "emailReviewers"; 73 : 74 : /** Can flush any cache except the active web_sessions cache. */ 75 : public static final String FLUSH_CACHES = "flushCaches"; 76 : 77 : /** Can terminate any task using the kill command. */ 78 : public static final String KILL_TASK = "killTask"; 79 : 80 : /** 81 : * Can perform limited server maintenance. 82 : * 83 : * <p>Includes tasks such as reindexing changes and flushing caches that may need to be performed 84 : * regularly. Does <strong>not</strong> grant arbitrary read/write/ACL management permissions as 85 : * does {@link #ADMINISTRATE_SERVER}. 86 : */ 87 : public static final String MAINTAIN_SERVER = "maintainServer"; 88 : 89 : /** Can modify any account on the server. */ 90 : public static final String MODIFY_ACCOUNT = "modifyAccount"; 91 : 92 : /** Queue a user can access to submit their tasks to. */ 93 : public static final String PRIORITY = "priority"; 94 : 95 : /** Maximum result limit per executed query. */ 96 : public static final String QUERY_LIMIT = "queryLimit"; 97 : 98 : /** Default result limit per executed query. */ 99 : public static final int DEFAULT_MAX_QUERY_LIMIT = 500; 100 : 101 : /** Can impersonate any user to see which refs they can read. */ 102 : public static final String READ_AS = "readAs"; 103 : 104 : /** Ability to impersonate another user. */ 105 : public static final String RUN_AS = "runAs"; 106 : 107 : /** Can run the Git garbage collection. */ 108 : public static final String RUN_GC = "runGC"; 109 : 110 : /** Can perform streaming of Gerrit events. */ 111 : public static final String STREAM_EVENTS = "streamEvents"; 112 : 113 : /** Can query permissions for any (project, user) pair */ 114 : public static final String VIEW_ACCESS = "viewAccess"; 115 : 116 : /** Can view all accounts, regardless of {@code accounts.visibility}. */ 117 : public static final String VIEW_ALL_ACCOUNTS = "viewAllAccounts"; 118 : 119 : /** Can view the server's current cache states. */ 120 : public static final String VIEW_CACHES = "viewCaches"; 121 : 122 : /** Can view open connections to the server's SSH port. */ 123 : public static final String VIEW_CONNECTIONS = "viewConnections"; 124 : 125 : /** Can view all installed plugins. */ 126 : public static final String VIEW_PLUGINS = "viewPlugins"; 127 : 128 : /** Can view all pending tasks in the queue (not just the filtered set). */ 129 : public static final String VIEW_QUEUE = "viewQueue"; 130 : 131 : private static final List<String> NAMES_ALL; 132 : private static final List<String> NAMES_LC; 133 151 : private static final String[] RANGE_NAMES = { 134 : QUERY_LIMIT, BATCH_CHANGES_LIMIT, 135 : }; 136 : 137 : static { 138 151 : NAMES_ALL = new ArrayList<>(); 139 151 : NAMES_ALL.add(ACCESS_DATABASE); 140 151 : NAMES_ALL.add(ADMINISTRATE_SERVER); 141 151 : NAMES_ALL.add(BATCH_CHANGES_LIMIT); 142 151 : NAMES_ALL.add(CREATE_ACCOUNT); 143 151 : NAMES_ALL.add(CREATE_GROUP); 144 151 : NAMES_ALL.add(CREATE_PROJECT); 145 151 : NAMES_ALL.add(EMAIL_REVIEWERS); 146 151 : NAMES_ALL.add(FLUSH_CACHES); 147 151 : NAMES_ALL.add(KILL_TASK); 148 151 : NAMES_ALL.add(MAINTAIN_SERVER); 149 151 : NAMES_ALL.add(MODIFY_ACCOUNT); 150 151 : NAMES_ALL.add(PRIORITY); 151 151 : NAMES_ALL.add(QUERY_LIMIT); 152 151 : NAMES_ALL.add(READ_AS); 153 151 : NAMES_ALL.add(RUN_AS); 154 151 : NAMES_ALL.add(RUN_GC); 155 151 : NAMES_ALL.add(STREAM_EVENTS); 156 151 : NAMES_ALL.add(VIEW_ACCESS); 157 151 : NAMES_ALL.add(VIEW_ALL_ACCOUNTS); 158 151 : NAMES_ALL.add(VIEW_CACHES); 159 151 : NAMES_ALL.add(VIEW_CONNECTIONS); 160 151 : NAMES_ALL.add(VIEW_PLUGINS); 161 151 : NAMES_ALL.add(VIEW_QUEUE); 162 : 163 151 : NAMES_LC = new ArrayList<>(NAMES_ALL.size()); 164 151 : for (String name : NAMES_ALL) { 165 151 : NAMES_LC.add(name.toLowerCase()); 166 151 : } 167 151 : } 168 : 169 : /** Returns all valid capability names. */ 170 : public static Collection<String> getAllNames() { 171 3 : return Collections.unmodifiableList(NAMES_ALL); 172 : } 173 : 174 : /** Returns true if the name is recognized as a capability name. */ 175 : public static boolean isGlobalCapability(String varName) { 176 3 : return NAMES_LC.contains(varName.toLowerCase()); 177 : } 178 : 179 : /** Returns true if the capability should have a range attached. */ 180 : public static boolean hasRange(String varName) { 181 151 : for (String n : RANGE_NAMES) { 182 151 : if (n.equalsIgnoreCase(varName)) { 183 122 : return true; 184 : } 185 : } 186 151 : return false; 187 : } 188 : 189 : public static List<String> getRangeNames() { 190 2 : return Collections.unmodifiableList(Arrays.asList(RANGE_NAMES)); 191 : } 192 : 193 : /** Returns the valid range for the capability if it has one, otherwise null. */ 194 : @Nullable 195 : public static PermissionRange.WithDefaults getRange(String varName) { 196 151 : if (QUERY_LIMIT.equalsIgnoreCase(varName)) { 197 151 : return new PermissionRange.WithDefaults( 198 : varName, 0, Integer.MAX_VALUE, 0, DEFAULT_MAX_QUERY_LIMIT); 199 : } 200 16 : if (BATCH_CHANGES_LIMIT.equalsIgnoreCase(varName)) { 201 2 : return new PermissionRange.WithDefaults( 202 : varName, 0, Integer.MAX_VALUE, 0, DEFAULT_MAX_BATCH_CHANGES_LIMIT); 203 : } 204 16 : return null; 205 : } 206 : 207 : private GlobalCapability() { 208 : // Utility class, do not create instances. 209 : } 210 : }