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// java util20import java.util.ArrayList;
21import java.util.Iterator;
22import java.util.List;
2324import org.apache.jetspeed.modules.actions.portlets.SecureVelocityPortletAction;
25import org.apache.jetspeed.om.security.Group;
26import org.apache.jetspeed.om.security.JetspeedUser;
27import org.apache.jetspeed.portal.portlets.VelocityPortlet;
28import org.apache.jetspeed.services.JetspeedSecurity;
29import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30import org.apache.jetspeed.services.logging.JetspeedLogger;
31import org.apache.jetspeed.services.resources.JetspeedResources;
32import org.apache.turbine.util.DynamicURI;
33import org.apache.turbine.util.RunData;
34import org.apache.turbine.util.StringUtils;
35import org.apache.velocity.context.Context;
363738/***39 * This action sets up the template context for editing security roles in the Turbine database40 * for a given user.41 *42 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>43 * @version $Id: UserGroupUpdateAction.java,v 1.5 2004/03/31 04:49:10 morciuch Exp $44 */45publicclassUserGroupUpdateActionextendsSecureVelocityPortletAction46 {
47/***48 * Static initialization of the logger for this class49 */50privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(UserGroupUpdateAction.class.getName());
5152/***53 * Build the maximized state content for this portlet. (Same as normal state).54 *55 * @param portlet The velocity-based portlet that is being built.56 * @param context The velocity context for this request.57 * @param rundata The turbine rundata context for this request.58 */59protectedvoid buildMaximizedContext( VelocityPortlet portlet,
60 Context context,
61 RunData rundata )
62 {
63 buildNormalContext( portlet, context, rundata);
64 }
6566/***67 * Build the configure state content for this portlet.68 * TODO: we could configure this portlet with configurable skins, etc..69 *70 * @param portlet The velocity-based portlet that is being built.71 * @param context The velocity context for this request.72 * @param rundata The turbine rundata context for this request.73 */74protectedvoid buildConfigureContext( VelocityPortlet portlet,
75 Context context,
76 RunData rundata )
77 {
7879 buildNormalContext( portlet, context, rundata);
80 }
8182/***83 * Build the normal state content for this portlet.84 *85 * @param portlet The velocity-based portlet that is being built.86 * @param context The velocity context for this request.87 * @param rundata The turbine rundata context for this request.88 */89protectedvoid buildNormalContext( VelocityPortlet portlet,
90 Context context,
91 RunData rundata )
92 {
93try94 {
95Group group = null;
96/*97 * Grab the mode for the user form.98 */99 String mode = rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
100101//102// check to see if we are adding a role for a single user103//104 String entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID);
105if (entityid == null || entityid.trim().length() == 0)
106 {
107return;
108 }
109110 buildUserGroupsContext(portlet, context, rundata, entityid);
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]);
121 }
122123 }
124catch (Exception e)
125 {
126 logger.error("Error in Jetspeed User Group Security", e);
127 rundata.setMessage("Error in Jetspeed User Group Security: " + e.toString());
128 rundata.setStackTrace(StringUtils.stackTrace(e), e);
129 rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error"));
130 }
131 }
132133134/***135 * Database Update Action for Security Roles. Performs updates into security database.136 *137 * @param rundata The turbine rundata context for this request.138 * @param context The velocity context for this request.139 */140publicvoid doUpdate(RunData rundata, Context context)
141 throws Exception
142 {
143 String entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID);
144if (entityid == null || entityid.trim().length() == 0)
145 {
146 logger.error("UserGroupBrowser: Failed to get entity: " + entityid );
147 DynamicURI duri = new DynamicURI (rundata);
148 duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm");
149 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER);
150 rundata.setRedirectURI(duri.toString());
151return;
152 }
153154JetspeedUser user = JetspeedSecurity.getUser(entityid);
155if (null == user)
156 {
157 logger.error("UserGroupBrowser: Failed to get user: " + entityid );
158 DynamicURI duri = new DynamicURI (rundata);
159 duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm");
160 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER);
161 rundata.setRedirectURI(duri.toString());
162return;
163 }
164165166try167 {
168 List groups = (List)rundata.getUser().getTemp(SecurityConstants.CONTEXT_GROUPS);
169 List selected = (List)rundata.getUser().getTemp(SecurityConstants.CONTEXT_SELECTED);
170171if (groups == null || selected == null)
172 {
173 DynamicURI duri = new DynamicURI (rundata);
174 duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm");
175 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_MISSING_PARAMETER);
176 rundata.setRedirectURI(duri.toString());
177return;
178 }
179180//181// walk thru all the roles, see if anything changed182// if changed, update the database183//184for (int ix = 0; ix < groups.size(); ix++)
185 {
186boolean newValue = rundata.getParameters().getBoolean("box_" + ((Group)groups.get(ix)).getName(), false);
187boolean oldValue = ((Boolean)selected.get(ix + 1)).booleanValue();
188if (newValue != oldValue)
189 {
190if (newValue == true)
191 {
192// grant a role to a user193 JetspeedSecurity.joinGroup( user.getUserName(),
194 ((Group)groups.get(ix)).getName() );
195 }
196else197 {
198// revoke a role from a user199 JetspeedSecurity.unjoinGroup( user.getUserName(),
200 ((Group)groups.get(ix)).getName() );
201 }
202 }
203 }
204205// clear the temp values206 rundata.getUser().setTemp(SecurityConstants.CONTEXT_GROUPS, null);
207 rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, null);
208209 }
210catch (Exception e)
211 {
212// log the error msg213 logger.error("Failed update role+permission: ", e);
214215//216// error on update - display error message217//218 DynamicURI duri = new DynamicURI (rundata);
219 duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm");
220 duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED);
221if (user != null)
222 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, user.getUserName());
223 rundata.setRedirectURI(duri.toString());
224225 }
226 }
227228/***229 * Build the context for a role browser for a specific user.230 *231 * @param portlet The velocity-based portlet that is being built.232 * @param context The velocity context for this request.233 * @param rundata The turbine rundata context for this request.234 * @param userid The userid of the user that we are building a role context for.235 */236privatevoid buildUserGroupsContext(VelocityPortlet portlet,
237 Context context,
238 RunData rundata,
239 String userid)
240 throws Exception
241 {
242// get the user object243JetspeedUser user = JetspeedSecurity.getUser(userid);
244if (null == user)
245 {
246// no User found247 logger.error("UserGroupBrowser: Failed to get user: " + userid );
248return;
249 }
250// get master list of roles251 Iterator groups = JetspeedSecurity.getGroups();
252 ArrayList masterGroups = new ArrayList();
253 ArrayList selected = new ArrayList();
254int ix = 0;
255boolean sel = false;
256 selected.add(ix, new Boolean(sel));
257while(groups.hasNext())
258 {
259Group group = (Group)groups.next();
260 masterGroups.add(group);
261 sel = JetspeedSecurity.inGroup(user.getUserName(), group.getName());
262 ix = ix + 1;
263 selected.add(ix, new Boolean(sel));
264 }
265 masterGroups.trimToSize();
266 selected.trimToSize();
267268 rundata.getUser().setTemp(SecurityConstants.CONTEXT_GROUPS, masterGroups);
269 rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, selected);
270 context.put(SecurityConstants.CONTEXT_USER, user);
271 context.put(SecurityConstants.CONTEXT_GROUPS, masterGroups);
272 context.put(SecurityConstants.CONTEXT_SELECTED, selected);
273274 }
275276277 }