001: /*
002: * Copyright (c) 2006 Your Corporation. All Rights Reserved.
003: */
004:
005: package com.technoetic.xplanner.security.auth;
006:
007: import java.util.ArrayList;
008: import java.util.Collection;
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.Map;
013:
014: import org.apache.commons.collections.CollectionUtils;
015: import org.apache.commons.collections.Predicate;
016:
017: /**
018: * User: mprokopowicz
019: * Date: Mar 29, 2006
020: * Time: 6:47:12 PM
021: */
022: public class PrincipalSpecificPermissionHelper {
023: private AuthorizerQueryHelper authorizerQueryHelper;
024:
025: public void setAuthorizerQueryHelper(
026: AuthorizerQueryHelper authorizerQueryHelper) {
027: this .authorizerQueryHelper = authorizerQueryHelper;
028: }
029:
030: public Map getPermissionsForPrincipal(int principalId) {
031: Map map = new HashMap();
032: addProjectsPermissions(principalId, map);
033: addPersonPermissions(principalId, map);
034: return map;
035: }
036:
037: public Collection getPermissionsSpecificToPerson(int principalId) {
038: Collection personPermissionsCol = authorizerQueryHelper
039: .getAllPermissionsToPerson();
040: return CollectionUtils.select(personPermissionsCol,
041: new PersonFilter(principalId) {
042: protected boolean isEqual(Integer actualId) {
043: return actualId.intValue() == 0
044: || actualId.intValue() == id;
045: }
046: });
047: }
048:
049: public Collection getPermissionsBasedOnPersonRoles(int principalId) {
050: return CollectionUtils.select(authorizerQueryHelper
051: .getAllPermissions(), new PersonFilter(principalId));
052: }
053:
054: void addPersonPermissions(int principalId, Map map) {
055: Collection permissionsLists = new ArrayList(map.values());
056: for (Iterator it = permissionsLists.iterator(); it.hasNext();) {
057: List list = new ArrayList((List) it.next());
058: for (Iterator itPerm = list.iterator(); itPerm.hasNext();) {
059: Permission p = (Permission) itPerm.next();
060: if (p.getResourceType().equals("%")
061: || p.getResourceType().equals("system.person")) {
062: Permission personPermission = new Permission(
063: "system.person", p.getResourceId(), p
064: .getPrincipalId(), p.getName());
065: addPermissionForProject(map, 0, personPermission);
066: }
067: }
068: }
069: List permissions = (List) getPermissionsSpecificToPerson(principalId);
070: for (int i = 0; i < permissions.size(); i++) {
071: Object permission = ((Object[]) permissions.get(i))[1];
072: addPermissionForProject(map, 0, (Permission) permission);
073: }
074: }
075:
076: void addPermissionForProject(Map permissionMap, int projectId,
077: Permission permission) {
078: Integer projectKey = new Integer(projectId);
079: List permissions = (List) permissionMap.get(projectKey);
080: if (permissions == null) {
081: permissions = new ArrayList();
082: permissionMap.put(projectKey, permissions);
083: }
084: permissions.add(permission);
085: }
086:
087: private void addProjectsPermissions(int principalId, Map map) {
088:
089: List permissions = (List) getPermissionsBasedOnPersonRoles(principalId);
090: for (int i = 0; i < permissions.size(); i++) {
091: Object[] result = (Object[]) permissions.get(i);
092: addPermissionForProject(map, ((Integer) result[1])
093: .intValue(), (Permission) result[2]);
094: }
095: }
096:
097: protected class PersonFilter implements Predicate {
098: int id;
099:
100: public PersonFilter(int id) {
101: this .id = id;
102: }
103:
104: public boolean evaluate(Object obj) {
105: Object[] row = (Object[]) obj;
106: final Integer actualId = (Integer) row[0];
107: return isEqual(actualId);
108: }
109:
110: protected boolean isEqual(Integer actualId) {
111: return actualId.intValue() == id;
112: }
113: }
114: }
|