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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.modules.actions.portlets.security;
1819// velocity20import org.apache.jetspeed.modules.actions.portlets.SecureVelocityPortletAction;
21import org.apache.jetspeed.om.security.JetspeedRoleFactory;
22import org.apache.jetspeed.om.security.Role;
23import org.apache.jetspeed.portal.portlets.VelocityPortlet;
24import org.apache.jetspeed.services.JetspeedSecurity;
25import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
26import org.apache.jetspeed.services.logging.JetspeedLogger;
27import org.apache.jetspeed.services.resources.JetspeedResources;
28import org.apache.jetspeed.services.security.RoleException;
29import org.apache.turbine.util.DynamicURI;
30import org.apache.turbine.util.RunData;
31import org.apache.turbine.util.StringUtils;
32import org.apache.velocity.context.Context;
333435/***36 * This action sets up the template context for editing security roles in the Turbine database.37 *38 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>39 * @version $Id: RoleUpdateAction.java,v 1.14 2004/03/31 04:49:10 morciuch Exp $40 */41publicclassRoleUpdateActionextendsSecureVelocityPortletAction42 {
43privatestaticfinal String TEMP_ROLE = "tempRole";
4445/***46 * Static initialization of the logger for this class47 */48privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(RoleUpdateAction.class.getName());
4950/***51 * Build the maximized state content for this portlet. (Same as normal state).52 *53 * @param portlet The velocity-based portlet that is being built.54 * @param context The velocity context for this request.55 * @param rundata The turbine rundata context for this request.56 */57protectedvoid buildMaximizedContext( VelocityPortlet portlet,
58 Context context,
59 RunData rundata )
60 {
61 buildNormalContext( portlet, context, rundata);
62 }
6364/***65 * Build the configure state content for this portlet.66 * TODO: we could configure this portlet with configurable skins, etc..67 *68 * @param portlet The velocity-based portlet that is being built.69 * @param context The velocity context for this request.70 * @param rundata The turbine rundata context for this request.71 */72protectedvoid buildConfigureContext( VelocityPortlet portlet,
73 Context context,
74 RunData rundata )
75 {
7677 buildNormalContext( portlet, context, rundata);
78 }
7980/***81 * Build the normal state content for this portlet.82 *83 * @param portlet The velocity-based portlet that is being built.84 * @param context The velocity context for this request.85 * @param rundata The turbine rundata context for this request.86 */87protectedvoid buildNormalContext( VelocityPortlet portlet,
88 Context context,
89 RunData rundata )
90 {
91try92 {
93Role role = null;
9495/*96 * Grab the mode for the user form.97 */98 String mode = rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
99100// if we are updating or deleting - put the name in the context101//102if (mode != null && (mode.equals(SecurityConstants.PARAM_MODE_UPDATE) ||
103 mode.equals(SecurityConstants.PARAM_MODE_DELETE)))
104 {
105// get the primary key and put the object in the context106 String rolename = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID);
107 role = JetspeedSecurity.getRole(rolename);
108 context.put(SecurityConstants.CONTEXT_ROLE, role);
109 }
110111//112// if there was an error, display the message113//114 String msgid = rundata.getParameters().getString(SecurityConstants.PARAM_MSGID);
115if (msgid != null)
116 {
117int id = Integer.parseInt(msgid);
118if (id < SecurityConstants.MESSAGES.length)
119 context.put(SecurityConstants.PARAM_MSG, SecurityConstants.MESSAGES[id]);
120121// get the bad entered data and put it back for convenient update122Role tempRole = (Role)rundata.getUser().getTemp(TEMP_ROLE);
123if (tempRole != null)
124 context.put(SecurityConstants.CONTEXT_ROLE, tempRole);
125 }
126 context.put(SecurityConstants.PARAM_MODE, mode);
127128 }
129catch (Exception e)
130 {
131 logger.error("Error in Jetspeed User Security", e);
132 rundata.setMessage("Error in Jetspeed User Security: " + e.toString());
133 rundata.setStackTrace(StringUtils.stackTrace(e), e);
134 rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error"));
135 }
136 }
137138/***139 * Database Insert Action for Security Roles. Performs inserts into security database.140 *141 * @param rundata The turbine rundata context for this request.142 * @param context The velocity context for this request.143 */144publicvoid doInsert(RunData rundata, Context context)
145 throws Exception
146 {
147Role role = null;
148try149 {
150//151// validate that its not an 'blank' rolename -- not allowed152//153 String name = rundata.getParameters().getString("name");
154if (name == null || name.trim().length() == 0)
155 {
156 DynamicURI duri = new DynamicURI (rundata);
157 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLE_UPDATE);
158 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_INVALID_ENTITY_NAME);
159 rundata.setRedirectURI(duri.toString());
160 rundata.getUser().setTemp(TEMP_ROLE, null);
161return;
162 }
163164//165// generate a new role166// 167168 role = JetspeedRoleFactory.getInstance();
169 role.setName(name);
170171//172// add the role173///174 JetspeedSecurity.addRole(role);
175176 }
177catch (RoleException e)
178 {
179// log the error msg180 logger.error("Exception", e);
181182//183// dup key found - display error message - bring back to same screen184//185 DynamicURI duri = new DynamicURI (rundata);
186 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLE_UPDATE);
187 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_ENTITY_ALREADY_EXISTS);
188 rundata.setRedirectURI(duri.toString());
189190// save values that user just entered so they don't have to re-enter191if (role != null)
192 rundata.getUser().setTemp(TEMP_ROLE, role);
193 }
194 }
195196/***197 * Database Update Action for Security Roles. Performs updates into security database.198 *199 * @param rundata The turbine rundata context for this request.200 * @param context The velocity context for this request.201 */202publicvoid doUpdate(RunData rundata, Context context)
203 throws Exception
204 {
205Role role = null;
206try207 {
208//209// get the role object from the selected role entry in the browser210//211 role = JetspeedSecurity.getRole(
212 rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID));
213214//215// update the role in the database216//217 JetspeedSecurity.saveRole(role);
218219 }
220catch (Exception e)
221 {
222// log the error msg223 logger.error("Exception", e);
224225//226// error on update - display error message227//228 DynamicURI duri = new DynamicURI (rundata);
229 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLE_UPDATE);
230 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED);
231if (role != null)
232 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, role.getName());
233 duri.addQueryData(SecurityConstants.PARAM_MODE, SecurityConstants.PARAM_MODE_UPDATE);
234 rundata.setRedirectURI(duri.toString());
235236// save values that user just entered so they don't have to re-enter237if (role != null)
238 rundata.getUser().setTemp(TEMP_ROLE, role);
239240 }
241 }
242243/***244 * Database Delete Action for Security Roles. Performs deletes into security database.245 *246 * @param rundata The turbine rundata context for this request.247 * @param context The velocity context for this request.248 */249publicvoid doDelete(RunData rundata, Context context)
250 throws Exception
251 {
252Role role = null;
253254try255 {
256//257// get the role object from the selected role entry in the browser258//259 role = JetspeedSecurity.getRole(
260 rundata.getParameters().getString( SecurityConstants.PARAM_ENTITY_ID) );
261262//263// remove the role264//265 JetspeedSecurity.removeRole(role.getName());
266267 }
268catch (Exception e)
269 {
270// log the error msg271 logger.error("Exception", e);
272273//274// error on delete - display error message275//276 DynamicURI duri = new DynamicURI (rundata);
277 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_ROLE_UPDATE);
278 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_DELETE_FAILED);
279if (role != null)
280 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, role.getName());
281 duri.addQueryData(SecurityConstants.PARAM_MODE, SecurityConstants.PARAM_MODE_DELETE);
282 rundata.setRedirectURI(duri.toString());
283284// save values that user just entered so they don't have to re-enter285if (role != null)
286 rundata.getUser().setTemp(TEMP_ROLE, role);
287288 }
289290 }
291292293 }