View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.portlets.security.permissions;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.LinkedList;
23  import java.util.List;
24  
25  import javax.portlet.ActionRequest;
26  import javax.portlet.ActionResponse;
27  import javax.portlet.PortletConfig;
28  import javax.portlet.PortletContext;
29  import javax.portlet.PortletException;
30  import javax.portlet.PortletSession;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.jetspeed.CommonPortletServices;
37  import org.apache.jetspeed.headerresource.HeaderResource;
38  import org.apache.jetspeed.security.PermissionManager;
39  import org.apache.jetspeed.security.RoleManager;
40  import org.apache.jetspeed.security.om.InternalPermission;
41  import org.apache.jetspeed.security.om.InternalPrincipal;
42  import org.apache.portals.gems.dojo.AbstractDojoVelocityPortlet;
43  import org.apache.velocity.context.Context;
44  
45  /***
46   * Security Permissions Portlet
47   * 
48   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
49   * @version $Id: $
50   */
51  public class SecurityPermissionsPortlet extends AbstractDojoVelocityPortlet
52  {
53      protected final Log logger = LogFactory.getLog(this.getClass());
54      protected PermissionManager pm = null;
55      protected RoleManager rm = null;
56      
57      // TODO: move to prefs
58      static final String CLASSNAMES[] = 
59      {
60          "org.apache.jetspeed.security.FolderPermission",
61          "org.apache.jetspeed.security.PagePermission",
62          "org.apache.jetspeed.security.PortletPermission"
63      };
64      static final String TITLES[] = 
65      {
66          "Folders",
67          "Pages",
68          "Portlets"
69      };
70      
71      
72      public void init(PortletConfig config) throws PortletException
73      {
74          super.init(config);
75          PortletContext context = getPortletContext();
76          pm = (PermissionManager) context
77                  .getAttribute(CommonPortletServices.CPS_PERMISSION_MANAGER);
78          if (pm == null)
79                  throw new PortletException(
80                          "Could not get instance of portal permission manager component");
81          rm = (RoleManager) context
82                  .getAttribute(CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
83          if (rm == null)
84              throw new PortletException(
85                  "Could not get instance of portal role manager component");        
86      }
87  
88      protected void includeHeaderContent( HeaderResource headerResource )
89      {
90          headerResource.dojoAddCoreLibraryRequire( "dojo.lang.*" );
91          headerResource.dojoAddCoreLibraryRequire( "dojo.event.*" );
92          headerResource.dojoAddCoreLibraryRequire( "dojo.io.*" );
93          headerResource.dojoAddCoreLibraryRequire( "dojo.widget.*" );
94          headerResource.dojoAddCoreLibraryRequire( "dojo.widget.LayoutContainer" );
95          headerResource.dojoAddCoreLibraryRequire( "dojo.widget.ContentPane" );
96          headerResource.dojoAddCoreLibraryRequire( "dojo.widget.SplitContainer" );
97          headerResource.dojoAddCoreLibraryRequire( "dojo.widget.TabContainer" );
98          headerResource.dojoAddCoreLibraryRequire( "dojo.widget.Checkbox" );
99          headerResource.dojoAddCoreLibraryRequire( "dojo.widget.Dialog" );
100         headerResource.dojoAddCoreLibraryRequire( "dojo.widget.Button" );
101         headerResource.dojoAddCoreLibraryRequire( "dojo.widget.Menu2" );
102         headerResource.dojoAddModuleLibraryRequire( "jetspeed.widget.EditorTable" );
103     }
104     
105     public void doView(RenderRequest request, RenderResponse response)
106             throws PortletException, IOException
107     {
108         retrievePermissions(request.getPortletSession(), getContext(request));
109         super.doView(request, response);
110     }
111 
112     public void retrievePermissions(PortletSession session, Context context)
113     {
114         // TODO: don't use session, since this is a client-side portlet
115         Iterator folderPermissions = (Iterator)session.getAttribute("folderPermissions", PortletSession.PORTLET_SCOPE);
116         Iterator pagePermissions = (Iterator)session.getAttribute("pagePermissions", PortletSession.PORTLET_SCOPE);
117         Iterator portletPermissions = (Iterator)session.getAttribute("portletPermissions", PortletSession.PORTLET_SCOPE);
118         Iterator roles = (Iterator)session.getAttribute("roles", PortletSession.PORTLET_SCOPE);
119         if (portletPermissions == null)
120         {
121             List folders = new LinkedList();
122             List pages = new LinkedList();
123             List portlets = new LinkedList();
124             Iterator all = pm.getPermissions().iterator();
125             while (all.hasNext())
126             {
127                 InternalPermission permission = (InternalPermission)all.next();                
128                 if (permission.getClassname().equals(CLASSNAMES[0]))
129                 {
130                     folders.add(new PermissionData(permission));                    
131                 }
132                 else if (permission.getClassname().equals(CLASSNAMES[1]))
133                 {
134                     pages.add(new PermissionData(permission));
135                 }
136                 else if (permission.getClassname().equals(CLASSNAMES[2]))
137                 {
138                     portlets.add(new PermissionData(permission));
139                 }                
140             }
141             folderPermissions = folders.iterator();
142             pagePermissions = pages.iterator();
143             portletPermissions = portlets.iterator();
144             try
145             {
146                 roles = rm.getRoles("");
147             }
148             catch(Exception e)
149             {
150                 logger.error(e);
151             }
152         }        
153         context.put("folderPermissions", folderPermissions);
154         context.put("pagePermissions", pagePermissions);
155         context.put("portletPermissions", portletPermissions);
156         ArrayList rolesList = new ArrayList();
157         if ( roles != null )
158         {
159             while( roles.hasNext() )
160             {
161                 rolesList.add( roles.next() );
162             }
163         }
164         context.put("roles", rolesList);
165     }
166     
167     public void processAction(ActionRequest request,
168             ActionResponse actionResponse) throws PortletException, IOException
169     {
170     }
171 
172     public class PermissionData
173     {
174         public PermissionData(InternalPermission permission)
175         {
176             this.permission = permission;
177             this.roles = ""; 
178             int size = permission.getPrincipals().size(); 
179             if (size == 0)
180             {
181                 return;
182             }
183             Iterator principals = permission.getPrincipals().iterator();
184             int count = 0;
185             StringBuffer result = new StringBuffer();
186             while (principals.hasNext())
187             {
188                 InternalPrincipal principal = (InternalPrincipal)principals.next();
189                 int last = principal.getFullPath().lastIndexOf("/") + 1;
190                 result.append(principal.getFullPath().substring(last));            
191                 count++;
192                 if (count < size)
193                 {
194                     result.append(",");
195                 }
196             }
197             this.roles = result.toString();
198         }
199         
200         InternalPermission permission;
201         String roles;
202         
203         public InternalPermission getPermission()
204         {
205             return permission;
206         }
207         
208         public void setPermission(InternalPermission permission)
209         {
210             this.permission = permission;
211         }
212         
213         public String getRoles()
214         {
215             return roles;
216         }
217         
218         public void setRoles(String roles)
219         {
220             this.roles = roles;
221         }
222     }
223 }