1 package org.apache.turbine.util.security; 2 3 18 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.turbine.om.security.Group; 23 import org.apache.turbine.om.security.Permission; 24 import org.apache.turbine.om.security.Role; 25 import org.apache.turbine.services.security.TurbineSecurity; 26 27 40 public class TurbineAccessControlList 41 implements AccessControlList 42 { 43 44 private Map roleSets; 45 46 47 private Map permissionSets; 48 49 50 private String name; 51 52 67 public TurbineAccessControlList(Map roleSets, Map permissionSets) 68 { 69 this.roleSets = roleSets; 70 this.permissionSets = permissionSets; 71 } 72 73 79 public String getName() 80 { 81 return this.name; 82 } 83 84 90 public void setName(String name) 91 { 92 this.name = name; 93 } 94 95 101 public RoleSet getRoles(Group group) 102 { 103 if (group == null) 104 { 105 return null; 106 } 107 return (RoleSet) roleSets.get(group); 108 } 109 110 115 public RoleSet getRoles() 116 { 117 return getRoles(TurbineSecurity.getGlobalGroup()); 118 } 119 120 126 public PermissionSet getPermissions(Group group) 127 { 128 if (group == null) 129 { 130 return null; 131 } 132 return (PermissionSet) permissionSets.get(group); 133 } 134 135 140 public PermissionSet getPermissions() 141 { 142 return getPermissions(TurbineSecurity.getGlobalGroup()); 143 } 144 145 152 public boolean hasRole(Role role, Group group) 153 { 154 RoleSet set = getRoles(group); 155 if (set == null || role == null) 156 { 157 return false; 158 } 159 return set.contains(role); 160 } 161 162 171 public boolean hasRole(Role role, GroupSet groupset) 172 { 173 if (role == null) 174 { 175 return false; 176 } 177 for (Iterator groups = groupset.iterator(); groups.hasNext();) 178 { 179 Group group = (Group) groups.next(); 180 RoleSet roles = getRoles(group); 181 if (roles != null) 182 { 183 if (roles.contains(role)) 184 { 185 return true; 186 } 187 } 188 } 189 return false; 190 } 191 192 199 public boolean hasRole(String role, String group) 200 { 201 try 202 { 203 return hasRole(TurbineSecurity.getRoleByName(role), 204 TurbineSecurity.getGroupByName(group)); 205 } 206 catch (Exception e) 207 { 208 return false; 209 } 210 } 211 212 221 public boolean hasRole(String rolename, GroupSet groupset) 222 { 223 Role role; 224 try 225 { 226 role = TurbineSecurity.getRoleByName(rolename); 227 } 228 catch (TurbineSecurityException e) 229 { 230 return false; 231 } 232 if (role == null) 233 { 234 return false; 235 } 236 for (Iterator groups = groupset.iterator(); groups.hasNext();) 237 { 238 Group group = (Group) groups.next(); 239 RoleSet roles = getRoles(group); 240 if (roles != null) 241 { 242 if (roles.contains(role)) 243 { 244 return true; 245 } 246 } 247 } 248 return false; 249 } 250 251 257 public boolean hasRole(Role role) 258 { 259 return hasRole(role, TurbineSecurity.getGlobalGroup()); 260 } 261 262 268 public boolean hasRole(String role) 269 { 270 try 271 { 272 return hasRole(TurbineSecurity.getRoleByName(role)); 273 } 274 catch (Exception e) 275 { 276 return false; 277 } 278 } 279 280 287 public boolean hasPermission(Permission permission, Group group) 288 { 289 PermissionSet set = getPermissions(group); 290 if (set == null || permission == null) 291 { 292 return false; 293 } 294 return set.contains(permission); 295 } 296 297 306 public boolean hasPermission(Permission permission, GroupSet groupset) 307 { 308 if (permission == null) 309 { 310 return false; 311 } 312 for (Iterator groups = groupset.iterator(); groups.hasNext();) 313 { 314 Group group = (Group) groups.next(); 315 PermissionSet permissions = getPermissions(group); 316 if (permissions != null) 317 { 318 if (permissions.contains(permission)) 319 { 320 return true; 321 } 322 } 323 } 324 return false; 325 } 326 327 334 public boolean hasPermission(String permission, String group) 335 { 336 try 337 { 338 return hasPermission(TurbineSecurity.getPermissionByName(permission), 339 TurbineSecurity.getGroupByName(group)); 340 } 341 catch (Exception e) 342 { 343 return false; 344 } 345 } 346 347 354 public boolean hasPermission(String permission, Group group) 355 { 356 try 357 { 358 return hasPermission( 359 TurbineSecurity.getPermissionByName(permission), group); 360 } 361 catch (Exception e) 362 { 363 return false; 364 } 365 } 366 367 376 public boolean hasPermission(String permissionName, GroupSet groupset) 377 { 378 Permission permission; 379 try 380 { 381 permission = TurbineSecurity.getPermissionByName(permissionName); 382 } 383 catch (TurbineSecurityException e) 384 { 385 return false; 386 } 387 if (permission == null) 388 { 389 return false; 390 } 391 for (Iterator groups = groupset.iterator(); groups.hasNext();) 392 { 393 Group group = (Group) groups.next(); 394 PermissionSet permissions = getPermissions(group); 395 if (permissions != null) 396 { 397 if (permissions.contains(permission)) 398 { 399 return true; 400 } 401 } 402 } 403 return false; 404 } 405 406 412 public boolean hasPermission(Permission permission) 413 { 414 return hasPermission(permission, TurbineSecurity.getGlobalGroup()); 415 } 416 417 423 public boolean hasPermission(String permission) 424 { 425 try 426 { 427 return hasPermission(TurbineSecurity.getPermissionByName(permission)); 428 } 429 catch (Exception e) 430 { 431 return false; 432 } 433 } 434 435 445 public Group[] getAllGroups() 446 { 447 try 448 { 449 return TurbineSecurity.getAllGroups().getGroupsArray(); 450 } 451 catch (TurbineSecurityException e) 452 { 453 return new Group[0]; 454 } 455 } 456 } 457 | Popular Tags |