KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > admin > struts > actions > UserAdminAction


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * 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. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.admin.struts.actions;
20
21 import java.io.IOException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.ServletException JavaDoc;
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.roller.ui.core.struts.actions.UserBaseAction;
33 import org.apache.struts.action.ActionError;
34 import org.apache.struts.action.ActionErrors;
35 import org.apache.struts.action.ActionForm;
36 import org.apache.struts.action.ActionForward;
37 import org.apache.struts.action.ActionMapping;
38 import org.apache.struts.action.ActionMessage;
39 import org.apache.struts.action.ActionMessages;
40 import org.apache.roller.RollerException;
41 import org.apache.roller.model.IndexManager;
42 import org.apache.roller.model.Roller;
43 import org.apache.roller.model.RollerFactory;
44 import org.apache.roller.model.UserManager;
45 import org.apache.roller.pojos.UserData;
46 import org.apache.roller.ui.core.BasePageModel;
47 import org.apache.roller.ui.core.RollerRequest;
48 import org.apache.roller.ui.core.RollerSession;
49 import org.apache.roller.ui.admin.struts.formbeans.UserAdminForm;
50 import org.apache.commons.lang.StringUtils;
51
52
53 /////////////////////////////////////////////////////////////////////////////
54
/**
55  * Actions for administering a user allow user query, edit, and delete of users.
56  *
57  * @struts.action name="userAdminForm" path="/roller-ui/admin/user"
58  * scope="request" parameter="method"
59  *
60  * @struts.action-forward name="adminUser.page" path=".UserAdmin"
61  */

62 public final class UserAdminAction extends UserBaseAction {
63     private static Log mLogger =
64             LogFactory.getFactory().getInstance(UserAdminAction.class);
65     
66     //-----------------------------------------------------------------------
67
/**
68      * Show query for user page or, if userName specified in request,
69      * show the admin user page for the specified user.
70      */

71     public ActionForward edit(
72             ActionMapping mapping,
73             ActionForm actionForm,
74             HttpServletRequest JavaDoc request,
75             HttpServletResponse JavaDoc response)
76             throws IOException JavaDoc, ServletException JavaDoc {
77         ActionForward forward = mapping.findForward("adminUser.page");
78         try {
79             UserData user = null;
80             RollerRequest rreq = RollerRequest.getRollerRequest(request);
81             RollerSession rollerSession = RollerSession.getRollerSession(request);
82             if (rollerSession.isGlobalAdminUser() ) {
83                 UserAdminForm userForm = (UserAdminForm)actionForm;
84                 UserManager mgr = RollerFactory.getRoller().getUserManager();
85                 if (userForm!=null
86                         && userForm.getUserName()!=null && !userForm.isNewUser()) {
87                     ActionMessages msgs = getErrors(request);
88                     msgs = (msgs == null) ? new ActionMessages() : msgs;
89                     user = mgr.getUserByUserName(userForm.getUserName(), null);
90                     if (user != null) {
91                         userForm.copyFrom(user, request.getLocale());
92                         // User must set new password twice
93
userForm.setPasswordText(null);
94                         userForm.setPasswordConfirm(null);
95                     } else {
96                         msgs.add(ActionErrors.GLOBAL_ERROR,
97                                 new ActionMessage("userAdmin.invalidNewUserName"));
98                         userForm.setUserName("");
99                     }
100                     if (request.getSession().getAttribute("cookieLogin")!=null) {
101                         // TODO: make it possible to change passwords
102
// regardless of remember me
103
msgs.add(ActionErrors.GLOBAL_ERROR,
104                                 new ActionMessage("userAdmin.cookieLogin"));
105                     }
106                     saveErrors(request, msgs);
107                 }
108                 request.setAttribute("model", new UserAdminPageModel(
109                         request, response, mapping, userForm, user));
110             } else {
111                 forward = mapping.findForward("access-denied");
112             }
113         } catch (Exception JavaDoc e) {
114             mLogger.error("ERROR in action",e);
115             throw new ServletException JavaDoc(e);
116         }
117         return forward;
118     }
119     
120     //-----------------------------------------------------------------------
121
/**
122      * Process POST of edited user data, may cause delete of user.
123      */

124     public ActionForward update(
125             ActionMapping mapping,
126             ActionForm actionForm,
127             HttpServletRequest JavaDoc request,
128             HttpServletResponse JavaDoc response)
129             throws IOException JavaDoc, ServletException JavaDoc {
130         ActionForward forward = mapping.findForward("adminUser.page");
131         ActionMessages msgs = new ActionMessages();
132         try {
133             RollerRequest rreq = RollerRequest.getRollerRequest(request);
134             RollerSession rollerSession =
135                     RollerSession.getRollerSession(request);
136             if (rollerSession.isGlobalAdminUser() ) {
137                 UserManager mgr = RollerFactory.getRoller().getUserManager();
138                 UserAdminForm userForm = (UserAdminForm)actionForm;
139                 
140                 if (userForm.isNewUser()) {
141                     UserData user = new UserData();
142                     userForm.copyTo(user, request.getLocale());
143                     user.setId(null);
144                     user.setDateCreated(new java.util.Date JavaDoc());
145                     user.setEnabled(Boolean.TRUE);
146                     
147                     // Check username and email addresses
148
msgs = validate(userForm, msgs);
149                     
150                     // Must have matching passwords and confirm passwords
151
if ( !StringUtils.isEmpty(userForm.getPasswordText())
152                     && !StringUtils.isEmpty(userForm.getPasswordConfirm())) {
153                         try {
154                             user.resetPassword(RollerFactory.getRoller(),
155                                     userForm.getPasswordText(),
156                                     userForm.getPasswordConfirm());
157                         } catch (RollerException e) {
158                             msgs.add(ActionErrors.GLOBAL_ERROR,
159                                     new ActionError("userSettings.passwordResetError"));
160                         }
161                     } else {
162                         msgs.add(ActionErrors.GLOBAL_ERROR,
163                                 new ActionError("userSettings.needPasswordTwice"));
164                     }
165                     
166                     // If no error messages, then add user
167
if (msgs.isEmpty()) {
168                         try {
169                             // Save new user to database
170
mgr.addUser(user);
171                             RollerFactory.getRoller().flush();
172                             
173                             msgs.add(ActionMessages.GLOBAL_MESSAGE,
174                                     new ActionMessage("userSettings.saved"));
175                             saveMessages(request, msgs);
176                             
177                             // Operation complete, return to edit action
178
userForm.setUserName(null);
179                             userForm.setNewUser((false));
180                             
181                         } catch (RollerException e) {
182                             // Add and commit failed, so show nice error message
183
msgs.add(ActionErrors.GLOBAL_ERROR,
184                                     new ActionError(e.getMessage()));
185                             saveErrors(request, msgs);
186                         }
187                     } else {
188                         saveErrors(request, msgs);
189                     }
190                     return edit(mapping, actionForm, request, response);
191                     
192                 } else {
193                     
194                     UserData user = mgr.getUser(userForm.getId());
195                     userForm.copyTo(user, request.getLocale());
196                     
197                     // Check username and email addresses
198
msgs = validate(userForm, msgs);
199                     
200                     // If user set both password and passwordConfirm then reset
201
if ( !StringUtils.isEmpty(userForm.getPasswordText())
202                     && !StringUtils.isEmpty(userForm.getPasswordConfirm())) {
203                         try {
204                             user.resetPassword(RollerFactory.getRoller(),
205                                     userForm.getPasswordText(),
206                                     userForm.getPasswordConfirm());
207                         } catch (RollerException e) {
208                             msgs.add(ActionErrors.GLOBAL_ERROR,
209                                     new ActionMessage(
210                                     "userSettings.passwordResetError"));
211                         }
212                     } else if (!StringUtils.isEmpty(userForm.getPasswordText())
213                     || !StringUtils.isEmpty(userForm.getPasswordConfirm())) {
214                         // But it's an error to specify only one of the two
215
msgs.add(ActionErrors.GLOBAL_ERROR,
216                                 new ActionMessage(
217                                 "userSettings.needPasswordTwice"));
218                     }
219                     
220                     if (msgs.isEmpty()) {
221                         try {
222                             // Persist changes to user
223
mgr.saveUser(user);
224                             RollerFactory.getRoller().flush();
225                             
226                             msgs.add(ActionMessages.GLOBAL_MESSAGE,
227                                     new ActionMessage("userSettings.saved"));
228                             saveMessages(request, msgs);
229                             
230                             // Operation complete, return to edit action
231
userForm.setUserName(null);
232                             
233                         } catch (RollerException e) {
234                             msgs.add(ActionErrors.GLOBAL_ERROR,
235                                     new ActionMessage(e.getMessage()));
236                             saveErrors(request, msgs);
237                         }
238                     } else {
239                         saveErrors(request, msgs);
240                     }
241                 }
242                 
243                 return edit(mapping, actionForm, request, response);
244             } else {
245                 forward = mapping.findForward("access-denied");
246             }
247         } catch (Exception JavaDoc e) {
248             mLogger.error("ERROR in action",e);
249             throw new ServletException JavaDoc(e);
250         }
251         return forward;
252     }
253     
254     //-----------------------------------------------------------------------
255
/**
256      * Cancel from edit user.
257      */

258     public ActionForward cancel(
259             ActionMapping mapping,
260             ActionForm actionForm,
261             HttpServletRequest JavaDoc request,
262             HttpServletResponse JavaDoc response)
263             throws IOException JavaDoc, ServletException JavaDoc {
264         UserAdminForm userForm = (UserAdminForm)actionForm;
265         userForm.setUserName(null);
266         userForm.setNewUser(false);
267         return edit(mapping, actionForm, request, response);
268     }
269     
270     //-----------------------------------------------------------------------
271
/**
272      * Create new user.
273      */

274     public ActionForward newUser(
275             ActionMapping mapping,
276             ActionForm actionForm,
277             HttpServletRequest JavaDoc request,
278             HttpServletResponse JavaDoc response)
279             throws IOException JavaDoc, ServletException JavaDoc {
280         UserAdminForm userForm = (UserAdminForm)actionForm;
281         userForm.setNewUser(true);
282         userForm.setEnabled(Boolean.TRUE);
283         return edit(mapping, actionForm, request, response);
284     }
285     
286     //-----------------------------------------------------------------------
287
/**
288      * Rebuild a user's search index.
289      */

290     public ActionForward index(
291             ActionMapping mapping,
292             ActionForm actionForm,
293             HttpServletRequest JavaDoc request,
294             HttpServletResponse JavaDoc response)
295             throws IOException JavaDoc, ServletException JavaDoc {
296         try {
297             RollerRequest rreq = RollerRequest.getRollerRequest(request);
298             RollerSession rollerSession = RollerSession.getRollerSession(request);
299             if (rollerSession.isGlobalAdminUser() ) {
300                 UserAdminForm uaf = (UserAdminForm)actionForm;
301                 
302                 // if admin requests an index be re-built, do it
303
IndexManager manager = RollerFactory.getRoller().getIndexManager();
304                 manager.rebuildWebsiteIndex();
305                 request.getSession().setAttribute(
306                         RollerSession.STATUS_MESSAGE,
307                         "Successfully scheduled rebuild of index for");
308             }
309         } catch (Exception JavaDoc e) {
310             mLogger.error("ERROR in action",e);
311             throw new ServletException JavaDoc(e);
312         }
313         return edit(mapping, actionForm, request, response);
314     }
315     
316     public class UserAdminPageModel extends BasePageModel {
317         private UserAdminForm userAdminForm = null;
318         private List JavaDoc permissions = new ArrayList JavaDoc();
319         
320         public UserAdminPageModel(
321                 HttpServletRequest JavaDoc request,
322                 HttpServletResponse JavaDoc response,
323                 ActionMapping mapping,
324                 UserAdminForm form,
325                 UserData user) throws RollerException {
326             super("dummy", request, response, mapping);
327             userAdminForm = form;
328             
329             if (user != null) {
330                 Roller roller = RollerFactory.getRoller();
331                 permissions = roller.getUserManager().getAllPermissions(user);
332             }
333         }
334         public String JavaDoc getTitle() {
335             if (StringUtils.isEmpty(userAdminForm.getUserName()) && userAdminForm.isNewUser()) {
336                 return bundle.getString("userAdmin.title.createNewUser");
337             } else if (StringUtils.isEmpty(userAdminForm.getUserName())) {
338                 return bundle.getString("userAdmin.title.searchUser");
339             }
340             return MessageFormat.format(
341                     bundle.getString("userAdmin.title.editUser"),
342                     new Object JavaDoc[] { userAdminForm.getUserName() } );
343         }
344         public List JavaDoc getPermissions() {
345             return permissions;
346         }
347         public void setPermissions(List JavaDoc permissions) {
348             this.permissions = permissions;
349         }
350     }
351 }
352
353
Popular Tags