001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.test.security;
023:
024: import org.jboss.portal.security.PortalPermission;
025: import org.jboss.portal.security.PortalPermissionCollection;
026: import org.jboss.portal.security.PortalSecurityException;
027: import org.jboss.portal.security.spi.provider.PermissionRepository;
028:
029: import javax.security.auth.Subject;
030: import java.security.Permission;
031:
032: /**
033: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
034: * @version $Revision: 8784 $
035: */
036: public class HierarchyPermission extends PortalPermission {
037:
038: /** The serialVersionUID */
039: private static final long serialVersionUID = -172205970543945821L;
040:
041: /** . */
042: public static final String VIEW_ACTION = "view";
043:
044: /** . */
045: public static final String VIEW_RECCURSIVE_ACTION = "viewrecursive";
046:
047: /** . */
048: private final String action;
049:
050: public HierarchyPermission(PortalPermissionCollection collection) {
051: super ("hierarchy", collection);
052: this .action = null;
053: }
054:
055: public HierarchyPermission(String uri, String action) {
056: super ("hierarchy", uri);
057: if (uri.startsWith("/") == false) {
058: throw new IllegalArgumentException(
059: "URI must begin with a leading /");
060: }
061: if (!VIEW_ACTION.equals(action)
062: && !VIEW_RECCURSIVE_ACTION.equals(action)) {
063: throw new IllegalArgumentException("Action " + action
064: + " should be one of {" + VIEW_ACTION + ","
065: + VIEW_RECCURSIVE_ACTION + "}");
066: }
067: this .action = action;
068: }
069:
070: public boolean implies(PermissionRepository repository,
071: Subject caller, String roleName, PortalPermission permission)
072: throws PortalSecurityException {
073: if (permission instanceof HierarchyPermission) {
074: HierarchyPermission that = (HierarchyPermission) permission;
075: String uri = that.uri;
076: if (uri != null) {
077: HierarchyPermission same = (HierarchyPermission) repository
078: .getPermission(roleName, uri);
079: if (same != null && uri.equals(same.getURI())) {
080: return true;
081: }
082: while (uri.length() > 1) {
083: int index = uri.lastIndexOf('/');
084: uri = uri.substring(0, index);
085: if (uri.length() == 0) {
086: uri = "/";
087: }
088: HierarchyPermission parent = (HierarchyPermission) repository
089: .getPermission(roleName, uri);
090: if (parent != null
091: && VIEW_RECCURSIVE_ACTION
092: .equals(parent.action)) {
093: return true;
094: }
095: }
096: }
097: }
098: return false;
099: }
100:
101: public boolean implies(Permission permission) {
102: if (permission instanceof HierarchyPermission && uri != null) {
103: HierarchyPermission that = (HierarchyPermission) permission;
104: if (action.equals(VIEW_ACTION)) {
105: return uri.equals(that.uri);
106: } else {
107: return that.uri != null && that.uri.startsWith(uri);
108: }
109: }
110: return false;
111: }
112:
113: public boolean equals(Object obj) {
114: if (obj == this ) {
115: return true;
116: }
117: if (obj instanceof HierarchyPermission) {
118: HierarchyPermission that = (HierarchyPermission) obj;
119: if (uri == null) {
120: return that.uri == null;
121: } else {
122: return action.equals(that.action)
123: && uri.equals(that.uri);
124: }
125: }
126: return false;
127: }
128:
129: public int hashCode() {
130: if (uri == null) {
131: return 0;
132: } else {
133: return action.hashCode() + uri.hashCode();
134: }
135: }
136:
137: public String getActions() {
138: return action;
139: }
140:
141: public String getType() {
142: throw new UnsupportedOperationException();
143: }
144: }
|