1 31 32 package org.opencms.security; 33 34 import java.util.HashMap ; 35 import java.util.Set ; 36 37 53 public class CmsPermissionSet { 54 55 56 public static final CmsPermissionSet ACCESS_CONTROL = new CmsPermissionSet(CmsPermissionSet.PERMISSION_CONTROL); 57 58 59 public static final CmsPermissionSet ACCESS_DIRECT_PUBLISH = new CmsPermissionSet( 60 CmsPermissionSet.PERMISSION_DIRECT_PUBLISH); 61 62 63 public static final CmsPermissionSet ACCESS_READ = new CmsPermissionSet(CmsPermissionSet.PERMISSION_READ); 64 65 66 public static final CmsPermissionSet ACCESS_VIEW = new CmsPermissionSet(CmsPermissionSet.PERMISSION_VIEW); 67 68 69 public static final CmsPermissionSet ACCESS_WRITE = new CmsPermissionSet(CmsPermissionSet.PERMISSION_WRITE); 70 71 72 public static final int PERMISSION_CONTROL = 8; 73 74 75 public static final int PERMISSION_DIRECT_PUBLISH = 16; 76 77 78 public static final int PERMISSION_EMPTY = 0; 79 80 81 public static final int PERMISSION_FULL = CmsPermissionSet.PERMISSION_READ 82 + CmsPermissionSet.PERMISSION_WRITE 83 + CmsPermissionSet.PERMISSION_VIEW 84 + CmsPermissionSet.PERMISSION_CONTROL 85 + CmsPermissionSet.PERMISSION_DIRECT_PUBLISH; 86 87 88 public static final int PERMISSION_READ = 1; 89 90 91 public static final int PERMISSION_VIEW = 4; 92 93 94 public static final int PERMISSION_WRITE = 2; 95 96 97 private static HashMap m_permissions; 98 99 100 protected int m_allowed; 101 102 103 protected int m_denied; 104 105 111 public CmsPermissionSet(int allowedPermissions, int deniedPermissions) { 112 113 m_allowed = allowedPermissions; 114 m_denied = deniedPermissions; 115 } 116 117 120 protected CmsPermissionSet() { 121 122 } 124 125 130 protected CmsPermissionSet(int allowedPermissions) { 131 132 m_allowed = allowedPermissions; 133 m_denied = 0; 134 } 135 136 141 public static Set getPermissionKeys() { 142 143 return permissions().keySet(); 144 } 145 146 152 public static int getPermissionValue(String key) { 153 154 return ((Integer )permissions().get(key)).intValue(); 155 } 156 157 162 private static HashMap permissions() { 163 if (m_permissions == null) { 164 m_permissions = new HashMap (); 165 m_permissions.put("GUI_PERMISSION_TYPE_READ_0", new Integer (CmsPermissionSet.PERMISSION_READ)); 166 m_permissions.put("GUI_PERMISSION_TYPE_WRITE_0", new Integer (CmsPermissionSet.PERMISSION_WRITE)); 167 m_permissions.put("GUI_PERMISSION_TYPE_VIEW_0", new Integer (CmsPermissionSet.PERMISSION_VIEW)); 168 m_permissions.put("GUI_PERMISSION_TYPE_CONTROL_0", new Integer (CmsPermissionSet.PERMISSION_CONTROL)); 169 m_permissions.put("GUI_PERMISSION_TYPE_DIRECT_PUBLISH_0", new Integer (CmsPermissionSet.PERMISSION_DIRECT_PUBLISH)); 170 } 171 return m_permissions; 172 } 173 174 177 public boolean equals(Object obj) { 178 179 if (obj == this) { 180 return true; 181 } 182 if (obj instanceof CmsPermissionSet) { 183 CmsPermissionSet other = (CmsPermissionSet)obj; 184 return (other.m_allowed == m_allowed) && (other.m_denied == m_denied); 185 } 186 return false; 187 } 188 189 194 public int getAllowedPermissions() { 195 196 return m_allowed; 197 } 198 199 204 public int getDeniedPermissions() { 205 206 return m_denied; 207 } 208 209 215 public int getPermissions() { 216 217 return m_allowed & ~m_denied; 218 } 219 220 225 public String getPermissionString() { 226 227 StringBuffer p = new StringBuffer (""); 228 229 if ((m_denied & CmsPermissionSet.PERMISSION_READ) > 0) { 230 p.append("-r"); 231 } else if (requiresReadPermission()) { 232 p.append("+r"); 233 } 234 235 if ((m_denied & CmsPermissionSet.PERMISSION_WRITE) > 0) { 236 p.append("-w"); 237 } else if (requiresWritePermission()) { 238 p.append("+w"); 239 } 240 241 if ((m_denied & CmsPermissionSet.PERMISSION_VIEW) > 0) { 242 p.append("-v"); 243 } else if (requiresViewPermission()) { 244 p.append("+v"); 245 } 246 247 if ((m_denied & CmsPermissionSet.PERMISSION_CONTROL) > 0) { 248 p.append("-c"); 249 } else if (requiresControlPermission()) { 250 p.append("+c"); 251 } 252 253 if ((m_denied & CmsPermissionSet.PERMISSION_DIRECT_PUBLISH) > 0) { 254 p.append("-d"); 255 } else if (requiresDirectPublishPermission()) { 256 p.append("+d"); 257 } 258 259 return p.toString(); 260 } 261 262 265 public int hashCode() { 266 267 return m_allowed * m_denied; 268 } 269 270 275 public boolean requiresControlPermission() { 276 277 return 0 < (m_allowed & CmsPermissionSet.PERMISSION_CONTROL); 278 } 279 280 285 public boolean requiresDirectPublishPermission() { 286 287 return 0 < (m_allowed & CmsPermissionSet.PERMISSION_DIRECT_PUBLISH); 288 } 289 290 295 public boolean requiresReadPermission() { 296 297 return 0 < (m_allowed & CmsPermissionSet.PERMISSION_READ); 298 } 299 300 305 public boolean requiresViewPermission() { 306 307 return 0 < (m_allowed & CmsPermissionSet.PERMISSION_VIEW); 308 } 309 310 315 public boolean requiresWritePermission() { 316 317 return 0 < (m_allowed & CmsPermissionSet.PERMISSION_WRITE); 318 } 319 320 325 public String toString() { 326 327 return "[PermissionSet:] " + getPermissionString(); 328 } 329 } | Popular Tags |