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