KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > security > RolePermissionUpdateAction


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.modules.actions.portlets.security;
18
19 // java util
20
import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.apache.jetspeed.modules.actions.portlets.SecureVelocityPortletAction;
25 import org.apache.jetspeed.om.security.Permission;
26 import org.apache.jetspeed.om.security.Role;
27 import org.apache.jetspeed.portal.portlets.VelocityPortlet;
28 import org.apache.jetspeed.services.JetspeedSecurity;
29 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30 import org.apache.jetspeed.services.logging.JetspeedLogger;
31 import org.apache.jetspeed.services.resources.JetspeedResources;
32 import org.apache.jetspeed.services.security.JetspeedSecurityException;
33 import org.apache.turbine.util.DynamicURI;
34 import org.apache.turbine.util.RunData;
35 import org.apache.turbine.util.StringUtils;
36 import org.apache.velocity.context.Context;
37
38 /**
39  * This action sets up the template context for editing security permissions in the Turbine database
40  * for a given role.
41  *
42  * @author <a HREF="mailto:taylor@apache.org">David Sean Taylor</a>
43  * @version $Id: RolePermissionUpdateAction.java,v 1.9 2004/03/31 04:49:10 morciuch Exp $
44  */

45 public class RolePermissionUpdateAction extends SecureVelocityPortletAction
46 {
47     private static final String JavaDoc TEMP_ROLE = "tempRole";
48
49     /**
50      * Static initialization of the logger for this class
51      */

52     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(RolePermissionUpdateAction.class.getName());
53     
54     /**
55      * Build the maximized state content for this portlet. (Same as normal state).
56      *
57      * @param portlet The velocity-based portlet that is being built.
58      * @param context The velocity context for this request.
59      * @param rundata The turbine rundata context for this request.
60      */

61     protected void buildMaximizedContext( VelocityPortlet portlet,
62                                           Context context,
63                                           RunData rundata )
64     {
65         buildNormalContext( portlet, context, rundata);
66     }
67
68     /**
69      * Build the configure state content for this portlet.
70      * TODO: we could configure this portlet with configurable skins, etc..
71      *
72      * @param portlet The velocity-based portlet that is being built.
73      * @param context The velocity context for this request.
74      * @param rundata The turbine rundata context for this request.
75      */

76     protected void buildConfigureContext( VelocityPortlet portlet,
77                                           Context context,
78                                           RunData rundata )
79     {
80
81         buildNormalContext( portlet, context, rundata);
82     }
83
84     /**
85      * Build the normal state content for this portlet.
86      *
87      * @param portlet The velocity-based portlet that is being built.
88      * @param context The velocity context for this request.
89      * @param rundata The turbine rundata context for this request.
90      */

91     protected void buildNormalContext( VelocityPortlet portlet,
92                                        Context context,
93                                        RunData rundata )
94     {
95         try
96         {
97             Role role = null;
98             /*
99              * Grab the mode for the user form.
100              */

101             String JavaDoc mode = rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
102
103             //
104
// check to see if we are adding a role for a single user
105
//
106
String JavaDoc entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID);
107             if (entityid == null || entityid.trim().length() == 0)
108             {
109                 return;
110             }
111
112             buildRolePermissionContext(portlet, context, rundata, entityid);
113
114             //
115
// if there was an error, display the message
116
//
117
String JavaDoc msgid = rundata.getParameters().getString(SecurityConstants.PARAM_MSGID);
118             if (msgid != null)
119             {
120                 int id = Integer.parseInt(msgid);
121                 if (id < SecurityConstants.MESSAGES.length)
122                     context.put(SecurityConstants.PARAM_MSG, SecurityConstants.MESSAGES[id]);
123             }
124
125         }
126         catch (Exception JavaDoc e)
127         {
128             logger.error("Error in Jetspeed Role Permission Security", e);
129             rundata.setMessage("Error in Jetspeed Role Permission Security: " + e.toString());
130             rundata.setStackTrace(StringUtils.stackTrace(e), e);
131             rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error"));
132         }
133     }
134
135
136     /**
137      * Database Update Action for Security Role Permissions. Performs updates into security database.
138      *
139      * @param rundata The turbine rundata context for this request.
140      * @param context The velocity context for this request.
141      */

142     public void doUpdate(RunData rundata, Context context)
143         throws Exception JavaDoc
144     {
145         String JavaDoc entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID);
146         if (entityid == null || entityid.trim().length() == 0)
147         {
148             logger.error("RolePermissionBrowser: Failed to get entity: " + entityid );
149             DynamicURI duri = new DynamicURI (rundata);
150             duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLEPERMISSION_UPDATE);
151             duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER);
152             rundata.setRedirectURI(duri.toString());
153             return;
154         }
155         Role role = JetspeedSecurity.getRole(entityid);
156         if (null == role)
157         {
158             logger.error("RolePermissionBrowser: Failed to get role: " + entityid );
159             DynamicURI duri = new DynamicURI (rundata);
160             duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLEPERMISSION_UPDATE);
161             duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER);
162             rundata.setRedirectURI(duri.toString());
163             return;
164         }
165
166
167         try
168         {
169             List JavaDoc permissions = (List JavaDoc)rundata.getUser().getTemp(SecurityConstants.CONTEXT_PERMISSIONS);
170             List JavaDoc selected = (List JavaDoc)rundata.getUser().getTemp(SecurityConstants.CONTEXT_SELECTED);
171
172             if (permissions == null || selected == null)
173             {
174                 DynamicURI duri = new DynamicURI (rundata);
175                 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLEPERMISSION_UPDATE);
176                 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER);
177                 rundata.setRedirectURI(duri.toString());
178                 return;
179             }
180
181             //
182
// walk thru all the permissions, see if anything changed
183
// if changed, update the database
184
//
185
for (int ix = 0; ix < permissions.size(); ix++)
186             {
187                 String JavaDoc permissionName = ((Permission)permissions.get(ix)).getName();
188                 boolean newValue = rundata.getParameters().getBoolean("box_" + permissionName, false);
189                 boolean oldValue = ((Boolean JavaDoc)selected.get(ix + 1)).booleanValue();
190                 if (newValue != oldValue)
191                 {
192                     if (newValue == true)
193                     {
194                         // grant a permission to a role
195
JetspeedSecurity.grantPermission(role.getName(), permissionName);
196                     }
197                     else
198                     {
199                         // revoke a permission from a role
200
JetspeedSecurity.revokePermission(role.getName(), permissionName);
201                     }
202                 }
203             }
204
205             // clear the temp values
206
rundata.getUser().setTemp(SecurityConstants.CONTEXT_PERMISSIONS, null);
207             rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, null);
208
209
210         }
211         catch (Exception JavaDoc e)
212         {
213            // log the error msg
214
logger.error("Failed update role+permission", e);
215
216             //
217
// error on update - display error message
218
//
219
DynamicURI duri = new DynamicURI (rundata);
220             duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLEPERMISSION_UPDATE);
221             duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED);
222             if (role != null)
223                 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, role.getName());
224             rundata.setRedirectURI(duri.toString());
225
226         }
227     }
228
229     /**
230      * Build the context for a role browser for a specific user.
231      *
232      * @param portlet The velocity-based portlet that is being built.
233      * @param context The velocity context for this request.
234      * @param rundata The turbine rundata context for this request.
235      * @param roleName The roleName of the role that we are building a role context for.
236      */

237     private void buildRolePermissionContext(VelocityPortlet portlet,
238                                             Context context,
239                                             RunData rundata,
240                                             String JavaDoc roleName)
241         throws JetspeedSecurityException
242     {
243         // get master list of permissions
244
Iterator JavaDoc master = JetspeedSecurity.getPermissions();
245
246         // get the user object
247
Role role = JetspeedSecurity.getRole(roleName);
248         if (null == role)
249         {
250             // no ACL found
251
logger.error("RolePermissionBrowser: Failed to get role: " + roleName);
252             return;
253         }
254
255         // get the permissions for this particular role
256
Iterator JavaDoc subset = JetspeedSecurity.getPermissions(roleName);
257
258         Vector JavaDoc permissions = new Vector JavaDoc();
259         Vector JavaDoc selected = new Vector JavaDoc();
260         boolean sel = false;
261         int ix = 0;
262         selected.add(0, new Boolean JavaDoc(sel));
263         while(master.hasNext())
264         {
265             Permission permission = (Permission) master.next();
266             permissions.add(permission);
267             sel = JetspeedSecurity.hasPermission(roleName, permission.getName());
268             ix = ix + 1;
269             selected.add(ix, new Boolean JavaDoc(sel));
270         }
271         selected.trimToSize();
272         permissions.trimToSize();
273
274         rundata.getUser().setTemp(SecurityConstants.CONTEXT_PERMISSIONS, permissions);
275         rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, selected);
276         context.put(SecurityConstants.CONTEXT_PERMISSIONS, permissions);
277         context.put(SecurityConstants.CONTEXT_SELECTED, selected);
278         context.put(SecurityConstants.CONTEXT_ROLE, role);
279     }
280
281
282 }
Popular Tags