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.security.SecurityAssociation;
026: import org.jboss.security.SimpleGroup;
027: import org.jboss.security.SimplePrincipal;
028: import org.jboss.security.jacc.DelegatingPolicy;
029: import org.jboss.security.jacc.SubjectPolicyContextHandler;
030:
031: import javax.security.auth.Subject;
032: import javax.security.jacc.PolicyContext;
033: import java.security.Policy;
034: import java.security.Principal;
035: import java.security.acl.Group;
036:
037: /**
038: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
039: * @version $Revision: 8784 $
040: */
041: public class Server {
042:
043: private String contextID = "ctxid";
044:
045: public String getContextID() {
046: return contextID;
047: }
048:
049: public void start() throws Exception {
050: // Set up the mandatory context handler
051: SubjectPolicyContextHandler handler = new SubjectPolicyContextHandler();
052: PolicyContext.registerHandler(
053: SubjectPolicyContextHandler.SUBJECT_CONTEXT_KEY,
054: handler, true);
055:
056: // Setup custom policy
057: DelegatingPolicy p = DelegatingPolicy.getInstance();
058: p
059: .setExternalPermissionTypes(new Class[] { PortalPermission.class });
060: Policy.setPolicy(p);
061: p.refresh();
062: }
063:
064: public void stop() {
065:
066: }
067:
068: /**
069: * Simulate a request executed in the scope of the server.
070: *
071: * @param task the task to execute
072: * @throws Exception any exception
073: */
074: public void execute(Task task) throws Exception {
075: try {
076: //Set up the contextid
077: PolicyContext.setContextID(contextID);
078:
079: // Execute task
080: task.execute();
081: } finally {
082: PolicyContext.setContextID(null);
083: }
084: }
085:
086: public interface Task {
087: void execute() throws Exception;
088: }
089:
090: public void associateRoles(String[] roleNames) throws Exception {
091: associateRoles(null, roleNames);
092: }
093:
094: public void associateRoles(Principal userPrincipal,
095: String[] roleNames) throws Exception {
096: Subject subject = new Subject();
097:
098: //
099: Group roleGroup = new SimpleGroup("Roles");
100: for (int i = 0; i < roleNames.length; i++) {
101: String roleName = roleNames[i];
102: Principal rolePrincipal = new SimplePrincipal(roleName);
103: roleGroup.addMember(rolePrincipal);
104: }
105: subject.getPrincipals().add(roleGroup);
106:
107: //
108: if (userPrincipal != null) {
109: subject.getPrincipals().add(userPrincipal);
110: }
111:
112: //
113: SecurityAssociation.setSubject(subject);
114: }
115: }
|