Line data Source code
1 : // Copyright (C) 2017 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.permissions; 16 : 17 : import com.google.gerrit.entities.Project; 18 : import com.google.gerrit.extensions.api.access.CoreOrPluginProjectPermission; 19 : import com.google.gerrit.extensions.api.access.GlobalOrPluginPermission; 20 : import com.google.gerrit.extensions.conditions.BooleanCondition; 21 : import com.google.gerrit.server.notedb.ChangeNotes; 22 : import com.google.gerrit.server.permissions.PermissionBackend.ForChange; 23 : import com.google.gerrit.server.permissions.PermissionBackend.ForProject; 24 : import com.google.gerrit.server.permissions.PermissionBackend.ForRef; 25 : import com.google.gerrit.server.permissions.PermissionBackend.RefFilterOptions; 26 : import com.google.gerrit.server.permissions.PermissionBackend.WithUser; 27 : import com.google.gerrit.server.query.change.ChangeData; 28 : import java.util.Collection; 29 : import java.util.Set; 30 : import org.eclipse.jgit.lib.Ref; 31 : import org.eclipse.jgit.lib.Repository; 32 : 33 : /** 34 : * Helpers for {@link PermissionBackend} that must fail. 35 : * 36 : * <p>These helpers are useful to curry failure state identified inside a non-throwing factory 37 : * method to the throwing {@code check} or {@code test} methods. 38 : */ 39 : public class FailedPermissionBackend { 40 : public static WithUser user(String message) { 41 0 : return new FailedWithUser(message, null); 42 : } 43 : 44 : public static WithUser user(String message, Throwable cause) { 45 0 : return new FailedWithUser(message, cause); 46 : } 47 : 48 : public static ForProject project(String message) { 49 0 : return project(message, null); 50 : } 51 : 52 : public static ForProject project(String message, Throwable cause) { 53 0 : return new FailedProject(message, cause); 54 : } 55 : 56 : public static ForRef ref(String message) { 57 0 : return ref(message, null); 58 : } 59 : 60 : public static ForRef ref(String message, Throwable cause) { 61 0 : return new FailedRef(message, cause); 62 : } 63 : 64 : public static ForChange change(String message) { 65 0 : return change(message, null); 66 : } 67 : 68 : public static ForChange change(String message, Throwable cause) { 69 0 : return new FailedChange(message, cause); 70 : } 71 : 72 : private FailedPermissionBackend() {} 73 : 74 : private static class FailedWithUser extends WithUser { 75 : private final String message; 76 : private final Throwable cause; 77 : 78 0 : FailedWithUser(String message, Throwable cause) { 79 0 : this.message = message; 80 0 : this.cause = cause; 81 0 : } 82 : 83 : @Override 84 : public ForProject project(Project.NameKey project) { 85 0 : return new FailedProject(message, cause); 86 : } 87 : 88 : @Override 89 : public void check(GlobalOrPluginPermission perm) throws PermissionBackendException { 90 0 : throw new PermissionBackendException(message, cause); 91 : } 92 : 93 : @Override 94 : public <T extends GlobalOrPluginPermission> Set<T> test(Collection<T> permSet) 95 : throws PermissionBackendException { 96 0 : throw new PermissionBackendException(message, cause); 97 : } 98 : 99 : @Override 100 : public BooleanCondition testCond(GlobalOrPluginPermission perm) { 101 0 : throw new UnsupportedOperationException( 102 : "FailedPermissionBackend does not support conditions"); 103 : } 104 : } 105 : 106 : private static class FailedProject extends ForProject { 107 : private final String message; 108 : private final Throwable cause; 109 : 110 0 : FailedProject(String message, Throwable cause) { 111 0 : this.message = message; 112 0 : this.cause = cause; 113 0 : } 114 : 115 : @Override 116 : public String resourcePath() { 117 0 : throw new UnsupportedOperationException( 118 : "FailedPermissionBackend is not scoped to a resource"); 119 : } 120 : 121 : @Override 122 : public ForRef ref(String ref) { 123 0 : return new FailedRef(message, cause); 124 : } 125 : 126 : @Override 127 : public void check(CoreOrPluginProjectPermission perm) throws PermissionBackendException { 128 0 : throw new PermissionBackendException(message, cause); 129 : } 130 : 131 : @Override 132 : public <T extends CoreOrPluginProjectPermission> Set<T> test(Collection<T> permSet) 133 : throws PermissionBackendException { 134 0 : throw new PermissionBackendException(message, cause); 135 : } 136 : 137 : @Override 138 : public BooleanCondition testCond(CoreOrPluginProjectPermission perm) { 139 0 : throw new UnsupportedOperationException( 140 : "FailedPermissionBackend does not support conditions"); 141 : } 142 : 143 : @Override 144 : public Collection<Ref> filter(Collection<Ref> refs, Repository repo, RefFilterOptions opts) 145 : throws PermissionBackendException { 146 0 : throw new PermissionBackendException(message, cause); 147 : } 148 : } 149 : 150 : private static class FailedRef extends ForRef { 151 : private final String message; 152 : private final Throwable cause; 153 : 154 0 : FailedRef(String message, Throwable cause) { 155 0 : this.message = message; 156 0 : this.cause = cause; 157 0 : } 158 : 159 : @Override 160 : public String resourcePath() { 161 0 : throw new UnsupportedOperationException( 162 : "FailedPermissionBackend is not scoped to a resource"); 163 : } 164 : 165 : @Override 166 : public ForChange change(ChangeData cd) { 167 0 : return new FailedChange(message, cause); 168 : } 169 : 170 : @Override 171 : public ForChange change(ChangeNotes notes) { 172 0 : return new FailedChange(message, cause); 173 : } 174 : 175 : @Override 176 : public void check(RefPermission perm) throws PermissionBackendException { 177 0 : throw new PermissionBackendException(message, cause); 178 : } 179 : 180 : @Override 181 : public Set<RefPermission> test(Collection<RefPermission> permSet) 182 : throws PermissionBackendException { 183 0 : throw new PermissionBackendException(message, cause); 184 : } 185 : 186 : @Override 187 : public BooleanCondition testCond(RefPermission perm) { 188 0 : throw new UnsupportedOperationException( 189 : "FailedPermissionBackend does not support conditions"); 190 : } 191 : } 192 : 193 : private static class FailedChange extends ForChange { 194 : private final String message; 195 : private final Throwable cause; 196 : 197 0 : FailedChange(String message, Throwable cause) { 198 0 : this.message = message; 199 0 : this.cause = cause; 200 0 : } 201 : 202 : @Override 203 : public String resourcePath() { 204 0 : throw new UnsupportedOperationException( 205 : "FailedPermissionBackend is not scoped to a resource"); 206 : } 207 : 208 : @Override 209 : public void check(ChangePermissionOrLabel perm) throws PermissionBackendException { 210 0 : throw new PermissionBackendException(message, cause); 211 : } 212 : 213 : @Override 214 : public <T extends ChangePermissionOrLabel> Set<T> test(Collection<T> permSet) 215 : throws PermissionBackendException { 216 0 : throw new PermissionBackendException(message, cause); 217 : } 218 : 219 : @Override 220 : public BooleanCondition testCond(ChangePermissionOrLabel perm) { 221 0 : throw new UnsupportedOperationException( 222 : "FailedPermissionBackend does not support conditions"); 223 : } 224 : } 225 : }