001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2008 Sun Microsystems, Inc. 025 * Portions Copyright 2013-2015 ForgeRock AS 026 */ 027package org.opends.server.authorization.dseecompat; 028 029import static org.opends.server.authorization.dseecompat.Aci.*; 030 031import java.util.EnumSet; 032import java.util.Set; 033 034/** 035 * This class provides an enumeration of the allowed rights. 036 */ 037public enum EnumRight { 038 039 /** 040 * This enumeration is returned when the result of the right is "read". 041 * 042 * @see Aci#ACI_READ 043 */ 044 READ ("read"), 045 /** 046 * This enumeration is returned when the result of the right is "write". 047 * 048 * @see Aci#ACI_WRITE 049 */ 050 WRITE ("write"), 051 /** 052 * This enumeration is returned when the result of the right is "add". 053 * 054 * @see Aci#ACI_ADD 055 */ 056 ADD ("add"), 057 /** 058 * This enumeration is returned when the result of the right is "delete". 059 * 060 * @see Aci#ACI_DELETE 061 */ 062 DELETE ("delete"), 063 /** 064 * This enumeration is returned when the result of the right is "search". 065 * 066 * @see Aci#ACI_SEARCH 067 */ 068 SEARCH ("search"), 069 /** 070 * This enumeration is returned when the result of the right is "compare". 071 * 072 * @see Aci#ACI_COMPARE 073 */ 074 COMPARE ("compare"), 075 /** 076 * This enumeration is returned when the result of the right is 077 * "selfwrite". 078 * 079 * @see Aci#ACI_SELF 080 */ 081 SELFWRITE ("selfwrite"), 082 /** 083 * This enumeration is returned when the result of the right is "proxy". 084 * 085 * @see Aci#ACI_PROXY 086 */ 087 PROXY ("proxy"), 088 /** 089 * This enumeration is returned when the result of the right is "import". 090 * 091 * @see Aci#ACI_IMPORT 092 */ 093 IMPORT ("import"), 094 /** 095 * This enumeration is returned when the result of the right is "export". 096 * 097 * @see Aci#ACI_EXPORT 098 */ 099 EXPORT ("export"), 100 /** 101 * This enumeration is returned when the result of the right is "all". 102 * 103 * @see Aci#ACI_ALL 104 */ 105 ALL ("all"); 106 107 /** 108 * The name of the right. 109 */ 110 private final String right; 111 112 /** 113 * Creates an enumeration of the right name. 114 * @param right The name of the right. 115 */ 116 EnumRight (String right) { 117 this.right = right ; 118 } 119 120 /** 121 * Returns the string representation of the right. 122 * 123 * @return the string representation of the right 124 */ 125 public String getRight() { 126 return right; 127 } 128 129 /** 130 * Checks if the enumeration is equal to the right name. 131 * @param right The name of the right to check. 132 * @return True if the right is equal to the enumeration's. 133 */ 134 public boolean isRight(String right){ 135 return right.equalsIgnoreCase(this.right); 136 } 137 138 /** 139 * Creates an enumeration of the right name. 140 * @param right The name of the right. 141 * @return An enumeration of the right or null if the name is invalid. 142 */ 143 public static EnumRight decode(String right){ 144 if (right != null){ 145 for (EnumRight t : EnumRight.values()){ 146 if (t.isRight(right)){ 147 return t; 148 } 149 } 150 } 151 return null; 152 } 153 154 /** 155 * Returns bit mask associated with the specified right. 156 * @param right The right enumeration to return the mask for. 157 * @return The bit mask associated with the right. 158 */ 159 public static int getMask(EnumRight right) { 160 int mask=ACI_NULL; 161 switch(right) { 162 case READ: 163 mask=ACI_READ; 164 break; 165 case WRITE: 166 mask=ACI_WRITE; 167 break; 168 case ADD: 169 mask=ACI_ADD; 170 break; 171 case DELETE: 172 mask=ACI_DELETE; 173 break; 174 case SEARCH: 175 mask=ACI_SEARCH; 176 break; 177 case COMPARE: 178 mask=ACI_COMPARE; 179 break; 180 case ALL: 181 mask=ACI_ALL; 182 break; 183 case EXPORT: 184 mask=ACI_EXPORT; 185 break; 186 case IMPORT: 187 mask=ACI_IMPORT; 188 break; 189 case PROXY: 190 mask=ACI_PROXY; 191 break; 192 case SELFWRITE: 193 mask=ACI_SELF; 194 break; 195 } 196 return mask; 197 } 198 199 /** 200 * Return the EnumRight corresponding to the provided rightsMask. 201 * 202 * @param rightsMask 203 * the rights mask for which to return the corresponding EnumRight 204 * @return EnumRight corresponding to the provided rightsMask. 205 */ 206 public static Set<EnumRight> getEnumRight(int rightsMask) { 207 final EnumSet<EnumRight> results = EnumSet.noneOf(EnumRight.class); 208 // Next 3 rights are not included in ALL for historical reasons. 209 // ALL already existed when they got added. For compatibility reasons 210 // with existing deployments, they were not included in ALL. 211 if (hasRights(rightsMask, ACI_EXPORT)) 212 { 213 results.add(EXPORT); 214 } 215 if (hasRights(rightsMask, ACI_IMPORT)) 216 { 217 results.add(IMPORT); 218 } 219 if (hasRights(rightsMask, ACI_PROXY)) 220 { 221 results.add(PROXY); 222 } 223 224 if (hasRights(rightsMask, ACI_ALL)) { 225 results.add(ALL); 226 return results; 227 } 228 // the remaining rights are already included in ALL 229 if (hasRights(rightsMask, ACI_READ)) 230 { 231 results.add(READ); 232 } 233 if (hasRights(rightsMask, ACI_WRITE)) 234 { 235 results.add(WRITE); 236 } 237 if (hasRights(rightsMask, ACI_ADD)) 238 { 239 results.add(ADD); 240 } 241 if (hasRights(rightsMask, ACI_DELETE)) 242 { 243 results.add(DELETE); 244 } 245 if (hasRights(rightsMask, ACI_SEARCH)) 246 { 247 results.add(SEARCH); 248 } 249 if (hasRights(rightsMask, ACI_COMPARE)) 250 { 251 results.add(COMPARE); 252 } 253 if (hasRights(rightsMask, ACI_SELF)) 254 { 255 results.add(SELFWRITE); 256 } 257 return results; 258 } 259 260 /** 261 * Checks if the provided rights mask has the specified rights. 262 * 263 * @param rightsMask 264 * The rights mask to look into. 265 * @param rights 266 * The rights to check for. 267 * @return true if the rights mask has the specified rights, false 268 * otherwise. 269 */ 270 public static boolean hasRights(int rightsMask, int rights) { 271 return (rightsMask & rights) == rights; 272 } 273}