|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | + /* |
| 21 | + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 22 | + */ |
| 23 | +package org.opensolaris.opengrok.authorization; |
| 24 | + |
| 25 | +import java.io.Serializable; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +/** |
| 30 | + * |
| 31 | + * @author Krystof Tulinger |
| 32 | + */ |
| 33 | +public class AuthorizationCheck implements Serializable { |
| 34 | + |
| 35 | + private static final long serialVersionUID = 1L; |
| 36 | + |
| 37 | + /** |
| 38 | + * Enum for avaliable authorization roles. |
| 39 | + */ |
| 40 | + public enum AuthControlFlag { |
| 41 | + /** |
| 42 | + * Failure of such a plugin will ultimately lead to the authorization |
| 43 | + * framework returning failure but only after the remaining plugins have |
| 44 | + * been invoked. |
| 45 | + * |
| 46 | + */ |
| 47 | + REQUIRED("required"), |
| 48 | + /** |
| 49 | + * Like required, however, in the case that such a plugin returns a |
| 50 | + * failure, control is directly returned to the application. The return |
| 51 | + * value is that associated with the first required or requisite plugin |
| 52 | + * to fail. |
| 53 | + * |
| 54 | + */ |
| 55 | + REQUISITE("requisite"), |
| 56 | + /** |
| 57 | + * If such a plugin succeeds and no prior required plugin has failed the |
| 58 | + * authorization framework returns success to the application |
| 59 | + * immediately without calling any further plugins in the stack. A |
| 60 | + * failure of a sufficient plugin is ignored and processing of the |
| 61 | + * plugin list continues unaffected. |
| 62 | + */ |
| 63 | + SUFFICIENT("sufficient"); |
| 64 | + |
| 65 | + private final String role; |
| 66 | + |
| 67 | + private AuthControlFlag(String role) { |
| 68 | + this.role = role; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String toString() { |
| 73 | + return this.role; |
| 74 | + } |
| 75 | + |
| 76 | + public static AuthControlFlag get(String role) { |
| 77 | + try { |
| 78 | + return AuthControlFlag.valueOf(role.toUpperCase()); |
| 79 | + } catch (IllegalArgumentException ex) { |
| 80 | + // role does not exist -> add some more info about which roles do exist |
| 81 | + throw new IllegalArgumentException( |
| 82 | + String.format("No authorization role \"%s\", available roles are [%s]. %s", |
| 83 | + role, |
| 84 | + Arrays.asList(AuthControlFlag.values()) |
| 85 | + .stream() |
| 86 | + .map(AuthControlFlag::toString) |
| 87 | + .collect(Collectors.joining(", ")), |
| 88 | + ex.getLocalizedMessage()), ex); |
| 89 | + } |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + /** |
| 94 | + * One of "required", "requisite", "sufficient". |
| 95 | + */ |
| 96 | + private AuthControlFlag role; |
| 97 | + |
| 98 | + /** |
| 99 | + * Canonical name of a java class. |
| 100 | + */ |
| 101 | + private String classname; |
| 102 | + |
| 103 | + public AuthorizationCheck() { |
| 104 | + } |
| 105 | + |
| 106 | + public AuthorizationCheck(AuthControlFlag role, String classname) { |
| 107 | + this.role = role; |
| 108 | + this.classname = classname; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Get the value of classname |
| 113 | + * |
| 114 | + * @return the value of classname |
| 115 | + */ |
| 116 | + public String getClassname() { |
| 117 | + return classname; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Set the value of classname |
| 122 | + * |
| 123 | + * @param classname new value of classname |
| 124 | + */ |
| 125 | + public void setClassname(String classname) { |
| 126 | + this.classname = classname; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Get the value of role |
| 131 | + * |
| 132 | + * @return the value of role |
| 133 | + */ |
| 134 | + public AuthControlFlag getRole() { |
| 135 | + return role; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Set the value of role |
| 140 | + * |
| 141 | + * @param role new value of role |
| 142 | + */ |
| 143 | + public void setRole(AuthControlFlag role) { |
| 144 | + this.role = role; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Set the value of role |
| 149 | + * |
| 150 | + * @param role new value of role |
| 151 | + */ |
| 152 | + public void setRole(String role) { |
| 153 | + this.role = AuthControlFlag.get(role); |
| 154 | + } |
| 155 | +} |
0 commit comments