1 package org.apache.fulcrum.security.impl; 2 3 56 57 import java.util.Map ; 58 import java.util.Iterator ; 59 60 import org.apache.fulcrum.security.TurbineSecurity; 61 62 import org.apache.fulcrum.security.entity.Group; 63 import org.apache.fulcrum.security.entity.Permission; 64 import org.apache.fulcrum.security.entity.Role; 65 66 import org.apache.fulcrum.security.util.AccessControlList; 67 import org.apache.fulcrum.security.util.GroupSet; 68 import org.apache.fulcrum.security.util.PermissionSet; 69 import org.apache.fulcrum.security.util.RoleSet; 70 import org.apache.fulcrum.security.util.TurbineSecurityException; 71 72 85 public class TurbineAccessControlList 86 implements AccessControlList 87 { 88 89 private Map roleSets; 90 91 92 private Map permissionSets; 93 94 95 private String name; 96 97 112 public TurbineAccessControlList(Map roleSets, Map permissionSets) 113 { 114 this.roleSets = roleSets; 115 this.permissionSets = permissionSets; 116 } 117 118 124 public String getName() 125 { 126 return this.name; 127 } 128 129 135 public void setName(String name) 136 { 137 this.name = name; 138 } 139 140 146 public RoleSet getRoles(Group group) 147 { 148 if (group == null) 149 { 150 return null; 151 } 152 return (RoleSet) roleSets.get(group); 153 } 154 155 160 public RoleSet getRoles() 161 { 162 return getRoles(TurbineSecurity.getGlobalGroup()); 163 } 164 165 171 public PermissionSet getPermissions(Group group) 172 { 173 if (group == null) 174 { 175 return null; 176 } 177 return (PermissionSet) permissionSets.get(group); 178 } 179 180 185 public PermissionSet getPermissions() 186 { 187 return getPermissions(TurbineSecurity.getGlobalGroup()); 188 } 189 190 197 public boolean hasRole(Role role, Group group) 198 { 199 RoleSet set = getRoles(group); 200 if (set == null || role == null) 201 { 202 return false; 203 } 204 return set.contains(role); 205 } 206 207 216 public boolean hasRole(Role role, GroupSet groupset) 217 { 218 if (role == null) 219 { 220 return false; 221 } 222 Iterator groups = groupset.elements(); 223 while (groups.hasNext()) 224 { 225 Group group = (Group) groups.next(); 226 RoleSet roles = getRoles(group); 227 if (roles != null) 228 { 229 if (roles.contains(role)) 230 { 231 return true; 232 } 233 } 234 } 235 return false; 236 } 237 238 245 public boolean hasRole(String role, String group) 246 { 247 try 248 { 249 return hasRole(TurbineSecurity.getRole(role), TurbineSecurity.getGroup(group)); 250 } 251 catch (Exception e) 252 { 253 return false; 254 } 255 } 256 257 266 public boolean hasRole(String rolename, GroupSet groupset) 267 { 268 Role role; 269 try 270 { 271 role = TurbineSecurity.getRole(rolename); 272 } 273 catch (TurbineSecurityException e) 274 { 275 return false; 276 } 277 if (role == null) 278 { 279 return false; 280 } 281 Iterator groups = groupset.elements(); 282 while (groups.hasNext()) 283 { 284 Group group = (Group) groups.next(); 285 RoleSet roles = getRoles(group); 286 if (roles != null) 287 { 288 if (roles.contains(role)) 289 { 290 return true; 291 } 292 } 293 } 294 return false; 295 } 296 297 303 public boolean hasRole(Role role) 304 { 305 return hasRole(role, TurbineSecurity.getGlobalGroup()); 306 } 307 308 314 public boolean hasRole(String role) 315 { 316 try 317 { 318 return hasRole(TurbineSecurity.getRole(role)); 319 } 320 catch (Exception e) 321 { 322 return false; 323 } 324 } 325 326 333 public boolean hasPermission(Permission permission, Group group) 334 { 335 PermissionSet set = getPermissions(group); 336 if (set == null || permission == null) 337 { 338 return false; 339 } 340 return set.contains(permission); 341 } 342 343 352 public boolean hasPermission(Permission permission, GroupSet groupset) 353 { 354 if (permission == null) 355 { 356 return false; 357 } 358 Iterator groups = groupset.elements(); 359 while (groups.hasNext()) 360 { 361 Group group = (Group) groups.next(); 362 PermissionSet permissions = getPermissions(group); 363 if (permissions != null) 364 { 365 if (permissions.contains(permission)) 366 { 367 return true; 368 } 369 } 370 } 371 return false; 372 } 373 374 381 public boolean hasPermission(String permission, String group) 382 { 383 try 384 { 385 return hasPermission(TurbineSecurity.getPermission(permission), 386 TurbineSecurity.getGroup(group)); 387 } 388 catch (Exception e) 389 { 390 return false; 391 } 392 } 393 394 401 public boolean hasPermission(String permission, Group group) 402 { 403 try 404 { 405 return hasPermission( 406 TurbineSecurity.getPermission(permission), group); 407 } 408 catch (Exception e) 409 { 410 return false; 411 } 412 } 413 414 423 public boolean hasPermission(String permissionName, GroupSet groupset) 424 { 425 Permission permission; 426 try 427 { 428 permission = TurbineSecurity.getPermission(permissionName); 429 } 430 catch (TurbineSecurityException e) 431 { 432 return false; 433 } 434 if (permission == null) 435 { 436 return false; 437 } 438 Iterator groups = groupset.elements(); 439 while (groups.hasNext()) 440 { 441 Group group = (Group) groups.next(); 442 PermissionSet permissions = getPermissions(group); 443 if (permissions != null) 444 { 445 if (permissions.contains(permission)) 446 { 447 return true; 448 } 449 } 450 } 451 return false; 452 } 453 454 460 public boolean hasPermission(Permission permission) 461 { 462 return hasPermission(permission, TurbineSecurity.getGlobalGroup()); 463 } 464 465 471 public boolean hasPermission(String permission) 472 { 473 try 474 { 475 return hasPermission(TurbineSecurity.getPermission(permission)); 476 } 477 catch (Exception e) 478 { 479 return false; 480 } 481 } 482 483 493 public Group[] getAllGroups() 494 { 495 try 496 { 497 return TurbineSecurity.getAllGroups().getGroupsArray(); 498 } 499 catch (TurbineSecurityException e) 500 { 501 return new Group[0]; 502 } 503 } 504 } 505 | Popular Tags |