Line data Source code
1 : // Copyright (C) 2018 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 static java.util.Objects.requireNonNull; 18 : 19 : import com.google.gerrit.extensions.api.access.GerritPermission; 20 : import com.google.gerrit.extensions.restapi.AuthException; 21 : import java.util.Optional; 22 : 23 : /** 24 : * This signals that some permission check failed. The message is short so it can print on a 25 : * single-line in the Git output. 26 : */ 27 : public class PermissionDeniedException extends AuthException { 28 : private static final long serialVersionUID = 1L; 29 : 30 : public static final String MESSAGE_PREFIX = "not permitted: "; 31 : 32 : private final GerritPermission permission; 33 : private final Optional<String> resource; 34 : 35 : public PermissionDeniedException(GerritPermission permission) { 36 0 : super(MESSAGE_PREFIX + requireNonNull(permission).describeForException()); 37 0 : this.permission = permission; 38 0 : this.resource = Optional.empty(); 39 0 : } 40 : 41 : public PermissionDeniedException(GerritPermission permission, String resource) { 42 25 : super( 43 : MESSAGE_PREFIX 44 25 : + requireNonNull(permission).describeForException() 45 : + " on " 46 25 : + requireNonNull(resource)); 47 25 : this.permission = permission; 48 25 : this.resource = Optional.of(resource); 49 25 : } 50 : 51 : public String describePermission() { 52 10 : return permission.describeForException(); 53 : } 54 : 55 : public Optional<String> getResource() { 56 12 : return resource; 57 : } 58 : }