AccessControlled.java 2.9 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24 25
package hudson.security;

26
import javax.annotation.Nonnull;
27
import org.acegisecurity.AccessDeniedException;
28
import org.acegisecurity.Authentication;
29

30 31 32 33 34 35 36 37 38 39 40
/**
 * Object that has an {@link ACL}
 *
 * @since 1.220
 */
public interface AccessControlled {
    /**
     * Obtains the ACL associated with this object.
     *
     * @return never null.
     */
41
    @Nonnull ACL getACL();
42 43 44 45

    /**
     * Convenient short-cut for {@code getACL().checkPermission(permission)}
     */
46 47 48
    default void checkPermission(@Nonnull Permission permission) throws AccessDeniedException {
        getACL().checkPermission(permission);
    }
49

T
Tim Jacomb 已提交
50 51 52 53
    /**
     * Convenient short-cut for {@code getACL().checkAnyPermission(permission)}
     * @see ACL#checkAnyPermission(Permission...)
     *
54
     * @since 2.222
T
Tim Jacomb 已提交
55 56 57 58 59
     */
    default void checkAnyPermission(@Nonnull Permission... permission) throws AccessDeniedException {
        getACL().checkAnyPermission(permission);
    }

60 61 62
    /**
     * Convenient short-cut for {@code getACL().hasPermission(permission)}
     */
63 64 65
    default boolean hasPermission(@Nonnull Permission permission) {
        return getACL().hasPermission(permission);
    }
66

T
Tim Jacomb 已提交
67 68 69 70
    /**
     * Convenient short-cut for {@code getACL().hasAnyPermission(permission)}
     * @see ACL#hasAnyPermission(Permission...)
     *
71
     * @since 2.222
T
Tim Jacomb 已提交
72 73 74 75 76
     */
    default boolean hasAnyPermission(@Nonnull Permission... permission) {
        return getACL().hasAnyPermission(permission);
    }

77 78
    /**
     * Convenient short-cut for {@code getACL().hasPermission(a, permission)}
79
     * @since 2.92
80 81
     */
    default boolean hasPermission(@Nonnull Authentication a, @Nonnull Permission permission) {
82 83 84
        if (a == ACL.SYSTEM) {
            return true;
        }
85 86 87
        return getACL().hasPermission(a, permission);
    }

88
}