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.Group;
22import org.apache.jetspeed.om.security.JetspeedGroupFactory;
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.GroupException;
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 groups in the Turbine database.37 *38 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>39 * @version $Id: GroupUpdateAction.java,v 1.10 2004/03/31 04:49:10 morciuch Exp $40 */4142publicclassGroupUpdateActionextendsSecureVelocityPortletAction43 {
44privatestaticfinal String TEMP_GROUP = "tempGroup";
4546/***47 * Static initialization of the logger for this class48 */49privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(GroupUpdateAction.class.getName());
5051/***52 * Build the maximized state content for this portlet. (Same as normal state).53 *54 * @param portlet The velocity-based portlet that is being built.55 * @param context The velocity context for this request.56 * @param rundata The turbine rundata context for this request.57 */58protectedvoid buildMaximizedContext( VelocityPortlet portlet,
59 Context context,
60 RunData rundata )
61 {
62 buildNormalContext( portlet, context, rundata);
63 }
6465/***66 * Build the configure state content for this portlet.67 * TODO: we could configure this portlet with configurable skins, etc..68 *69 * @param portlet The velocity-based portlet that is being built.70 * @param context The velocity context for this request.71 * @param rundata The turbine rundata context for this request.72 */73protectedvoid buildConfigureContext( VelocityPortlet portlet,
74 Context context,
75 RunData rundata )
76 {
7778 buildNormalContext( portlet, context, rundata);
79 }
8081/***82 * Build the normal state content for this portlet.83 *84 * @param portlet The velocity-based portlet that is being built.85 * @param context The velocity context for this request.86 * @param rundata The turbine rundata context for this request.87 */88protectedvoid buildNormalContext( VelocityPortlet portlet,
89 Context context,
90 RunData rundata )
91 {
92try93 {
94Group group = null;
9596/*97 * Grab the mode for the form.98 */99 String mode = rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
100101// if we are updating or deleting - put the name in the context102//103if (mode != null && (mode.equals(SecurityConstants.PARAM_MODE_UPDATE) ||
104 mode.equals(SecurityConstants.PARAM_MODE_DELETE)))
105 {
106// get the primary key and put the object in the context107 String groupname = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID);
108 group = JetspeedSecurity.getGroup(groupname);
109 context.put(SecurityConstants.CONTEXT_GROUP, group);
110 }
111112//113// if there was an error, display the message114//115 String msgid = rundata.getParameters().getString(SecurityConstants.PARAM_MSGID);
116if (msgid != null)
117 {
118int id = Integer.parseInt(msgid);
119if (id < SecurityConstants.MESSAGES.length)
120 context.put(SecurityConstants.PARAM_MSG, SecurityConstants.MESSAGES[id]);
121122// get the bad entered data and put it back for convenient update123Group tempGroup = (Group)rundata.getUser().getTemp(TEMP_GROUP);
124if (tempGroup != null)
125 context.put(SecurityConstants.CONTEXT_GROUP, tempGroup);
126 }
127 context.put(SecurityConstants.PARAM_MODE, mode);
128129 }
130catch (Exception e)
131 {
132 logger.error("Error in Jetspeed User Security", e);
133 rundata.setMessage("Error in Jetspeed User Security: " + e.toString());
134 rundata.setStackTrace(StringUtils.stackTrace(e), e);
135 rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error"));
136 }
137 }
138139/***140 * Database Insert Action for Security Groups. Performs inserts into security database.141 *142 * @param rundata The turbine rundata context for this request.143 * @param context The velocity context for this request.144 */145publicvoid doInsert(RunData rundata, Context context)
146 throws Exception
147 {
148Group group = null;
149try150 {
151//152// validate that its not an 'blank' groupname -- not allowed153//154 String name = rundata.getParameters().getString("name");
155if (name == null || name.trim().length() == 0)
156 {
157 DynamicURI duri = new DynamicURI (rundata);
158 duri.addPathInfo(SecurityConstants.PANE_NAME, SecurityConstants.PANEID_GROUP_UPDATE);
159 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_INVALID_ENTITY_NAME);
160 rundata.setRedirectURI(duri.toString());
161 rundata.getUser().setTemp(TEMP_GROUP, null);
162return;
163 }
164165//166// generate a new group167//168 group = JetspeedGroupFactory.getInstance();
169 group.setName(name);
170171//172// add the group173///174 JetspeedSecurity.addGroup(group);
175176 }
177catch (GroupException 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_GROUP_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 (group != null)
192 rundata.getUser().setTemp(TEMP_GROUP, group);
193 }
194 }
195196/***197 * Database Update Action for Security Groups. 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 {
205Group group = null;
206try207 {
208//209// get the group object from the selected group entry in the browser210//211 group = JetspeedSecurity.getGroup(
212 rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID));
213214//215// update the group in the database216//217 JetspeedSecurity.saveGroup(group);
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_GROUP_UPDATE);
230 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED);
231if (group != null)
232 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, group.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 (group != null)
238 rundata.getUser().setTemp(TEMP_GROUP, group);
239240 }
241 }
242243/***244 * Database Delete Action for Security Groups. 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 {
252Group group = null;
253254try255 {
256//257// get the group object from the selected group entry in the browser258//259 group = JetspeedSecurity.getGroup(
260 rundata.getParameters().getString( SecurityConstants.PARAM_ENTITY_ID) );
261262//263// remove the group264//265 JetspeedSecurity.removeGroup(group.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_GROUP_UPDATE);
278 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_DELETE_FAILED);
279if (group != null)
280 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, group.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 (group != null)
286 rundata.getUser().setTemp(TEMP_GROUP, group);
287288 }
289290 }
291292293294 }