001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.security;
024:
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028: import java.util.Collection;
029: import java.util.Iterator;
030: import java.util.Properties;
031:
032: import org.apache.xpath.CachedXPathAPI;
033: import org.w3c.dom.Element;
034:
035: import biz.hammurapi.config.ConfigurationException;
036: import biz.hammurapi.sql.DataAccessObject;
037: import biz.hammurapi.sql.RowProcessor;
038: import biz.hammurapi.sql.SQLProcessor;
039: import biz.hammurapi.web.security.sql.AssignedPermissionImpl;
040: import biz.hammurapi.web.security.sql.SecurityEngine;
041: import biz.hammurapi.xml.dom.DOMUtils;
042:
043: /**
044: * This class overrides equality - it uses class name and action name instead of "helper" permission id.
045: * @author Pavel Vlasov
046: * @revision $Revision$
047: */
048: public class Permission extends AssignedPermissionImpl implements
049: DataAccessObject {
050:
051: /**
052: *
053: */
054: public Permission() {
055: // Default constructor
056: }
057:
058: public Permission(String className, String action) {
059: setClassName(className);
060: setActionName(action);
061: }
062:
063: public int hashCode() {
064: return getClass().hashCode() ^ getActionName().hashCode();
065: }
066:
067: public boolean equals(Object obj) {
068: if (obj == this ) {
069: return true;
070: }
071:
072: if (obj instanceof Permission) {
073: Permission op = (Permission) obj;
074: return getClassName().equals(op.getClassName())
075: && getActionName().equals(op.getActionName());
076: }
077:
078: return false;
079: }
080:
081: private Collection impliedPermissions = new ArrayList();
082:
083: /**
084: * @param className
085: * @param actionName
086: * @return True if permission for given class and action is granted by this permission or permissions
087: * implied by this permission.
088: */
089: public Boolean isGranted(String className, String actionName) {
090: if (isImplied(className, actionName)) {
091: return getIsDenied() ? Boolean.FALSE : Boolean.TRUE;
092: }
093:
094: return null;
095: }
096:
097: private boolean isImplied(String className, String actionName) {
098: if (getClassName().equals(className)
099: && getActionName().equals(actionName)) {
100: return true;
101: }
102:
103: Iterator it = impliedPermissions.iterator();
104: while (it.hasNext()) {
105: if (((Permission) it.next()).isImplied(className,
106: actionName)) {
107: return true;
108: }
109: }
110:
111: return false;
112: }
113:
114: /**
115: * @param force
116: */
117: public Permission(boolean force) {
118: super (force);
119: }
120:
121: /**
122: * @param rs
123: * @throws SQLException
124: */
125: public Permission(ResultSet rs) throws SQLException {
126: super (rs);
127: }
128:
129: /**
130: * @param holder
131: * @param force
132: * @throws ConfigurationException
133: */
134: public Permission(Element holder, boolean force)
135: throws ConfigurationException {
136: super (holder, force);
137: }
138:
139: /**
140: * @param holder
141: * @param pathMap
142: * @param cxpa
143: * @param force
144: * @throws ConfigurationException
145: */
146: public Permission(Element holder, Properties pathMap,
147: CachedXPathAPI cxpa, boolean force)
148: throws ConfigurationException {
149: super (holder, pathMap, cxpa, force);
150: }
151:
152: private boolean isDerived;
153:
154: /**
155: * Sets derived indicator.
156: * @param isDerived
157: */
158: public void setDerived(boolean isDerived) {
159: this .isDerived = isDerived;
160: }
161:
162: /**
163: * Indicates whether permission was directly assigned to the object or derived from other objects.
164: * E.g. User derives permission from Role.
165: * @return
166: */
167: public boolean isDerived() {
168: return isDerived;
169: }
170:
171: /**
172: * Retrieves derived permissons
173: */
174: public void setSQLProcessor(SQLProcessor processor)
175: throws SQLException {
176: SecurityEngine engine = new SecurityEngine(processor);
177: engine.processImpliedPermissions(getId(), new RowProcessor() {
178:
179: public boolean process(ResultSet rs) throws SQLException {
180: impliedPermissions.add(new Permission(rs));
181: return true;
182: }
183:
184: });
185: }
186:
187: public void toDom(Element holder) {
188: super .toDom(holder);
189: DOMUtils.toDom(impliedPermissions, "implied-permissions",
190: holder);
191: }
192:
193: }
|