KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.jetspeed.modules.actions.portlets.SecureVelocityPortletAction;
25 import org.apache.jetspeed.om.security.Group;
26 import org.apache.jetspeed.om.security.JetspeedUser;
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.turbine.util.DynamicURI;
33 import org.apache.turbine.util.RunData;
34 import org.apache.turbine.util.StringUtils;
35 import org.apache.velocity.context.Context;
36
37
38 /**
39  * This action sets up the template context for editing security roles in the Turbine database
40  * 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  */

45 public class UserGroupUpdateAction extends SecureVelocityPortletAction
46 {
47     /**
48      * Static initialization of the logger for this class
49      */

50     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(UserGroupUpdateAction.class.getName());
51     
52     /**
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      */

59     protected void buildMaximizedContext( VelocityPortlet portlet,
60                                           Context context,
61                                           RunData rundata )
62     {
63         buildNormalContext( portlet, context, rundata);
64     }
65
66     /**
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      */

74     protected void buildConfigureContext( VelocityPortlet portlet,
75                                           Context context,
76                                           RunData rundata )
77     {
78
79         buildNormalContext( portlet, context, rundata);
80     }
81
82     /**
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      */

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

99             String JavaDoc mode = rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
100
101             //
102
// check to see if we are adding a role for a single user
103
//
104
String JavaDoc entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID);
105             if (entityid == null || entityid.trim().length() == 0)
106             {
107                 return;
108             }
109
110             buildUserGroupsContext(portlet, context, rundata, entityid);
111
112             //
113
// if there was an error, display the message
114
//
115
String JavaDoc msgid = rundata.getParameters().getString(SecurityConstants.PARAM_MSGID);
116             if (msgid != null)
117             {
118                 int id = Integer.parseInt(msgid);
119                 if (id < SecurityConstants.MESSAGES.length)
120                     context.put(SecurityConstants.PARAM_MSG, SecurityConstants.MESSAGES[id]);
121             }
122
123         }
124         catch (Exception JavaDoc 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     }
132
133
134     /**
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      */

140     public void doUpdate(RunData rundata, Context context)
141         throws Exception JavaDoc
142     {
143         String JavaDoc entityid = rundata.getParameters().getString(SecurityConstants.PARAM_ENTITY_ID);
144         if (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());
151             return;
152         }
153
154         JetspeedUser user = JetspeedSecurity.getUser(entityid);
155         if (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());
162             return;
163         }
164
165
166         try
167         {
168             List JavaDoc groups = (List JavaDoc)rundata.getUser().getTemp(SecurityConstants.CONTEXT_GROUPS);
169             List JavaDoc selected = (List JavaDoc)rundata.getUser().getTemp(SecurityConstants.CONTEXT_SELECTED);
170
171             if (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());
177                 return;
178             }
179
180             //
181
// walk thru all the roles, see if anything changed
182
// if changed, update the database
183
//
184
for (int ix = 0; ix < groups.size(); ix++)
185             {
186                 boolean newValue = rundata.getParameters().getBoolean("box_" + ((Group)groups.get(ix)).getName(), false);
187                 boolean oldValue = ((Boolean JavaDoc)selected.get(ix + 1)).booleanValue();
188                 if (newValue != oldValue)
189                 {
190                     if (newValue == true)
191                     {
192                         // grant a role to a user
193
JetspeedSecurity.joinGroup( user.getUserName(),
194                                                 ((Group)groups.get(ix)).getName() );
195                     }
196                     else
197                     {
198                         // revoke a role from a user
199
JetspeedSecurity.unjoinGroup( user.getUserName(),
200                                                     ((Group)groups.get(ix)).getName() );
201                     }
202                 }
203             }
204
205             // clear the temp values
206
rundata.getUser().setTemp(SecurityConstants.CONTEXT_GROUPS, null);
207             rundata.getUser().setTemp(SecurityConstants.CONTEXT_SELECTED, null);
208
209         }
210         catch (Exception JavaDoc e)
211         {
212            // log the error msg
213
logger.error("Failed update role+permission: ", e);
214
215             //
216
// error on update - display error message
217
//
218
DynamicURI duri = new DynamicURI (rundata);
219             duri.addPathInfo(SecurityConstants.PANE_NAME, "UserGroupForm");
220             duri.addPathInfo(SecurityConstants.PARAM_MSGID, SecurityConstants.MID_UPDATE_FAILED);
221             if (user != null)
222                 duri.addPathInfo(SecurityConstants.PARAM_ENTITY_ID, user.getUserName());
223             rundata.setRedirectURI(duri.toString());
224
225         }
226     }
227
228     /**
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      */

236     private void buildUserGroupsContext(VelocityPortlet portlet,
237                                        Context context,
238                                        RunData rundata,
239                                        String JavaDoc userid)
240         throws Exception JavaDoc
241     {
242         // get the user object
243
JetspeedUser user = JetspeedSecurity.getUser(userid);
244         if (null == user)
245         {
246             // no User found
247
logger.error("UserGroupBrowser: Failed to get user: " + userid );
248             return;
249         }
250         // get master list of roles
251
Iterator JavaDoc groups = JetspeedSecurity.getGroups();
252         ArrayList JavaDoc masterGroups = new ArrayList JavaDoc();
253         ArrayList JavaDoc selected = new ArrayList JavaDoc();
254         int ix = 0;
255         boolean sel = false;
256         selected.add(ix, new Boolean JavaDoc(sel));
257         while(groups.hasNext())
258         {
259             Group 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 JavaDoc(sel));
264         }
265         masterGroups.trimToSize();
266         selected.trimToSize();
267
268         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);
273
274     }
275
276
277 }
Popular Tags