KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jacc > test > portal > LazyPermissionCollection


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jacc.test.portal;
23
24 import java.security.Permission JavaDoc;
25 import java.security.PermissionCollection JavaDoc;
26 import java.security.Principal JavaDoc;
27 import java.security.acl.Group JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import javax.security.auth.Subject JavaDoc;
36 import javax.security.jacc.PolicyContext JavaDoc;
37
38 import org.jboss.logging.Logger;
39
40 //$Id: LazyPermissionCollection.java 40338 2006-01-18 22:10:07Z asaldhana $
41

42 /**
43  * JBPORTAL-565: Create Testcase for JACC Usage
44  * PermissionCollection that is lazily loaded
45  * @author <a HREF="mailto:Anil.Saldhana@jboss.org">Anil Saldhana</a>
46  * @since Jan 16, 2006
47  * @version $Revision: 40338 $
48  */

49 public class LazyPermissionCollection extends PermissionCollection JavaDoc
50 {
51    private static Logger log = Logger.getLogger(LazyPermissionCollection.class);
52     
53    private List JavaDoc permissionTable = new ArrayList JavaDoc();
54    
55    public void add(Permission JavaDoc perm)
56    {
57       log.debug("Inside add with perm=" + perm);
58       permissionTable.add(perm);
59    }
60
61    
62    public boolean implies(Permission JavaDoc perm)
63    {
64       log.debug("Inside implies with perm=" + perm);
65       
66       if(perm instanceof PortalPermission == false)
67          return false;
68       
69       try
70       {
71          loadPermissionsBasedOnRole();
72       }catch(Exception JavaDoc e)
73       {
74          log.error(e);
75          return false;
76       }
77       
78       int len = permissionTable.size();
79       for(int i = 0; i < len ; i++)
80       {
81          Permission JavaDoc p = (Permission JavaDoc)permissionTable.get(i);
82          if(p.implies(perm))
83             return true;
84       }
85       
86       return false;
87    }
88
89     
90    public Enumeration JavaDoc elements()
91    {
92       return Collections.enumeration(permissionTable);
93    }
94     
95    
96    private String JavaDoc getRole() throws Exception JavaDoc
97    {
98       Subject JavaDoc subject = (Subject JavaDoc) PolicyContext.getContext("javax.security.auth.Subject.container");
99       Set JavaDoc principals = subject.getPrincipals();
100       Iterator JavaDoc iter = principals != null ? principals.iterator() : null;
101       while(iter != null && iter.hasNext())
102       {
103          Principal JavaDoc p = (Principal JavaDoc)iter.next();
104          if(p instanceof Group JavaDoc)
105          {
106             Group JavaDoc gp = (Group JavaDoc)p;
107             if("Roles".equals(gp.getName()) == false)
108                continue;
109             Enumeration JavaDoc en = gp.members();
110             while(en.hasMoreElements())
111             {
112                Principal JavaDoc role = (Principal JavaDoc)en.nextElement();
113                return role.getName();
114             }
115          }
116       }
117       return null;
118    }
119    
120    private void loadPermissionsBasedOnRole() throws Exception JavaDoc
121    {
122       String JavaDoc role = this.getRole();
123       if(role == null)
124          throw new IllegalStateException JavaDoc("role is null");
125       this.permissionTable.clear();
126       if("employee".equalsIgnoreCase(role))
127       {
128          //Permitted to access for all portal resources
129
permissionTable.add(new PortalObjectPermission("/default", "view"));
130       }
131       else
132          if("janitor".equalsIgnoreCase(role))
133          {
134             //Only permitted to view a window
135
permissionTable.add(new PortalObjectPermission("/default/default/a", "view"));
136          }
137          else
138             if("admin".equals(role))
139             {
140                //Permitted for the whole context
141
permissionTable.add(new PortalObjectPermission("/", "view"));
142             }
143    }
144 }
145
Popular Tags