|
| 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.util.Arrays; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +/** |
| 29 | + * Enum for avaliable authorization roles. |
| 30 | + * |
| 31 | + * @author Krystof Tulinger |
| 32 | + */ |
| 33 | +public enum AuthControlFlag { |
| 34 | + /** |
| 35 | + * Failure of such a plugin will ultimately lead to the authorization |
| 36 | + * framework returning failure but only after the remaining plugins have |
| 37 | + * been invoked. |
| 38 | + * |
| 39 | + */ |
| 40 | + REQUIRED("required"), |
| 41 | + /** |
| 42 | + * Like required, however, in the case that such a plugin returns a failure, |
| 43 | + * control is directly returned to the application. The return value is that |
| 44 | + * associated with the first required or requisite plugin to fail. |
| 45 | + * |
| 46 | + */ |
| 47 | + REQUISITE("requisite"), |
| 48 | + /** |
| 49 | + * If such a plugin succeeds and no prior required plugin has failed the |
| 50 | + * authorization framework returns success to the application immediately |
| 51 | + * without calling any further plugins in the stack. A failure of a |
| 52 | + * sufficient plugin is ignored and processing of the plugin list continues |
| 53 | + * unaffected. |
| 54 | + */ |
| 55 | + SUFFICIENT("sufficient"); |
| 56 | + |
| 57 | + private final String flag; |
| 58 | + |
| 59 | + private AuthControlFlag(String flag) { |
| 60 | + this.flag = flag; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String toString() { |
| 65 | + return this.flag; |
| 66 | + } |
| 67 | + |
| 68 | + public boolean isRequired() { |
| 69 | + return REQUIRED.equals(this); |
| 70 | + } |
| 71 | + |
| 72 | + public boolean isRequisite() { |
| 73 | + return REQUISITE.equals(this); |
| 74 | + } |
| 75 | + |
| 76 | + public boolean isSufficient() { |
| 77 | + return SUFFICIENT.equals(this); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get the enum value for the string parameter. |
| 82 | + * |
| 83 | + * @param flag parameter describing the desired enum value |
| 84 | + * @return the flag representing the parameter value |
| 85 | + * |
| 86 | + * @throws IllegalArgumentException when there is no such value in the enum |
| 87 | + */ |
| 88 | + public static AuthControlFlag get(String flag) throws IllegalArgumentException { |
| 89 | + try { |
| 90 | + return AuthControlFlag.valueOf(flag.toUpperCase()); |
| 91 | + } catch (IllegalArgumentException ex) { |
| 92 | + // flag does not exist -> add some more info about which flags do exist |
| 93 | + throw new IllegalArgumentException( |
| 94 | + String.format("No control flag \"%s\", available flags are [%s]. %s", |
| 95 | + flag, |
| 96 | + Arrays.asList(AuthControlFlag.values()) |
| 97 | + .stream() |
| 98 | + .map(AuthControlFlag::toString) |
| 99 | + .collect(Collectors.joining(", ")), |
| 100 | + ex.getLocalizedMessage()), ex); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments