Line data Source code
1 : // Copyright (C) 2019 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.extensions.api.access; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : import static java.util.Objects.requireNonNull; 19 : 20 : import com.google.common.base.MoreObjects; 21 : import java.util.Objects; 22 : import java.util.regex.Pattern; 23 : 24 : /** Repository permissions defined by plugins. */ 25 : public final class PluginProjectPermission implements CoreOrPluginProjectPermission { 26 : public static final String PLUGIN_PERMISSION_NAME_PATTERN_STRING = "[a-zA-Z]+"; 27 0 : private static final Pattern PLUGIN_PERMISSION_PATTERN = 28 0 : Pattern.compile("^" + PLUGIN_PERMISSION_NAME_PATTERN_STRING + "$"); 29 : 30 : private final String pluginName; 31 : private final String permission; 32 : 33 0 : public PluginProjectPermission(String pluginName, String permission) { 34 0 : requireNonNull(pluginName, "pluginName"); 35 0 : requireNonNull(permission, "permission"); 36 0 : checkArgument( 37 0 : isValidPluginPermissionName(permission), "invalid plugin permission name: %s", permission); 38 : 39 0 : this.pluginName = pluginName; 40 0 : this.permission = permission; 41 0 : } 42 : 43 : public String pluginName() { 44 0 : return pluginName; 45 : } 46 : 47 : public String permission() { 48 0 : return permission; 49 : } 50 : 51 : @Override 52 : public String describeForException() { 53 0 : return permission + " for plugin " + pluginName; 54 : } 55 : 56 : @Override 57 : public int hashCode() { 58 0 : return Objects.hash(pluginName, permission); 59 : } 60 : 61 : @Override 62 : public boolean equals(Object other) { 63 0 : if (other instanceof PluginProjectPermission) { 64 0 : PluginProjectPermission b = (PluginProjectPermission) other; 65 0 : return pluginName.equals(b.pluginName) && permission.equals(b.permission); 66 : } 67 0 : return false; 68 : } 69 : 70 : @Override 71 : public String toString() { 72 0 : return MoreObjects.toStringHelper(this) 73 0 : .add("pluginName", pluginName) 74 0 : .add("permission", permission) 75 0 : .toString(); 76 : } 77 : 78 : /** 79 : * Checks if a given name is valid to be used for plugin permissions. 80 : * 81 : * @param name a name string. 82 : * @return whether the name is valid as a plugin permission. 83 : */ 84 : private static boolean isValidPluginPermissionName(String name) { 85 0 : return PLUGIN_PERMISSION_PATTERN.matcher(name).matches(); 86 : } 87 : }