001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.security.proxy;
023:
024: import java.rmi.RemoteException;
025: import java.security.AccessController;
026: import java.security.Principal;
027: import javax.ejb.EJBContext;
028: import javax.naming.Name;
029: import javax.naming.NamingException;
030: import javax.naming.directory.Attribute;
031: import javax.naming.directory.Attributes;
032:
033: import org.jboss.test.security.test.NamespacePermission;
034: import org.jboss.test.security.interfaces.IProjRepository;
035:
036: /** A simple stateful security proxy example for the ProjRepository bean.
037:
038: @see javax.naming.Name
039: @see javax.naming.directory.Attributes
040: @see org.jboss.test.security.test.ejbs.project.interfaces.IProjRepository
041:
042: @author Scott_Stark@displayscape.com
043: @version $Revision: 57211 $
044: */
045: public class ProjRepositorySecurityProxy2 implements IProjRepository {
046: org.apache.log4j.Category log = org.apache.log4j.Category
047: .getInstance(getClass());
048:
049: /**
050: * @label bean
051: * @clientRole state sink
052: * @supplierRole state source
053: */
054: private IProjRepository projRepository;
055: private EJBContext ctx;
056:
057: public void setEJBContext(EJBContext ctx) {
058: this .ctx = ctx;
059: log.debug("ProjRepositorySecurityProxy2.setEJBContext, ctx="
060: + ctx);
061: }
062:
063: public void setBean(Object bean) {
064: projRepository = (IProjRepository) bean;
065: log.debug("ProjRepositorySecurityProxy2.setBean, bean="
066: + projRepository);
067: }
068:
069: public void ejbCreate(Name projectName) {
070: Principal user = ctx.getCallerPrincipal();
071: String userID = user.getName();
072: log
073: .debug("ProjRepositorySecurityProxy2.ejbCreate, projectName="
074: + projectName);
075: // Only scott or starksm can create project sessions
076: if (userID.equals("scott") == false
077: && userID.equals("starksm") == false)
078: throw new SecurityException("Invalid project userID: "
079: + userID);
080: }
081:
082: // --- Begin IProjRepository interface methods
083: public void createFolder(Name folderPath) {
084: log
085: .debug("ProjRepositorySecurityProxy2.createFolder, folderPath="
086: + folderPath);
087: }
088:
089: public void deleteFolder(Name folderPath, boolean recursive) {
090: log
091: .debug("ProjRepositorySecurityProxy2.deleteFolder, folderPath="
092: + folderPath);
093: }
094:
095: public void createItem(Name itemPath, Attributes attributes) {
096: log.debug("ProjRepositorySecurityProxy2.createItem, itemPath="
097: + itemPath);
098: }
099:
100: public void updateItem(Name itemPath, Attributes attributes) {
101: log.debug("ProjRepositorySecurityProxy2.updateItem, itemPath="
102: + itemPath);
103: }
104:
105: public void deleteItem(Name itemPath) {
106: Principal user = ctx.getCallerPrincipal();
107: String userID = user.getName();
108: log.debug("ProjRepositorySecurityProxy2.deleteItem, itemPath="
109: + itemPath);
110: // Only the item owner can delete it
111: String owner = null;
112: try {
113: Attributes attributes = projRepository.getItem(itemPath);
114: if (attributes != null) {
115: Attribute attr = attributes.get("owner");
116: if (attr != null)
117: owner = (String) attr.get();
118: }
119: } catch (Exception e) {
120: log.debug("failed", e);
121: throw new SecurityException("Failed to obtain owner for: "
122: + itemPath);
123: }
124:
125: if (owner == null)
126: throw new SecurityException("No owner assigned to: "
127: + itemPath);
128: if (owner.equals(userID) == false)
129: throw new SecurityException("User: " + userID
130: + " is not the owner of: " + itemPath);
131: }
132:
133: public Attributes getItem(Name itemPath) {
134: NamespacePermission p = new NamespacePermission(itemPath,
135: "r---");
136: AccessController.checkPermission(p);
137: log.debug("ProjRepositorySecurityProxy2.getItem, itemPath="
138: + itemPath);
139: return null;
140: }
141: // --- End IProjRepository interface methods
142:
143: }
|