KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > website > actions > UserAdminAction


1
2 package org.roller.presentation.website.actions;
3
4 import java.io.IOException JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.servlet.ServletException JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.struts.action.ActionForm;
14 import org.apache.struts.action.ActionForward;
15 import org.apache.struts.action.ActionMapping;
16 import org.apache.struts.action.ActionMessage;
17 import org.apache.struts.action.ActionMessages;
18 import org.roller.RollerException;
19 import org.roller.business.search.operations.RebuildUserIndexOperation;
20 import org.roller.business.search.operations.RemoveUserIndexOperation;
21 import org.roller.model.IndexManager;
22 import org.roller.model.UserManager;
23 import org.roller.pojos.UserData;
24 import org.roller.pojos.WebsiteData;
25 import org.roller.presentation.RollerRequest;
26 import org.roller.presentation.RollerSession;
27 import org.roller.presentation.pagecache.PageCacheFilter;
28 import org.roller.presentation.website.formbeans.UserAdminForm;
29 import org.roller.util.StringUtils;
30
31
32 /////////////////////////////////////////////////////////////////////////////
33
/**
34  * Actions for administering a user allow user query, edit, and delete of users.
35  *
36  * @struts.action name="userAdminForm" path="/admin/user"
37  * scope="request" parameter="method"
38  *
39  * @struts.action-forward name="adminUser.page" path="/website/UserAdmin.jsp"
40  */

41 public final class UserAdminAction extends UserBaseAction
42 {
43     private static Log mLogger =
44         LogFactory.getFactory().getInstance(UserAdminAction.class);
45
46     //-----------------------------------------------------------------------
47
/**
48      * Show query for user page or, if userName specified in request, show
49      * the admin user page for the specified user.
50      */

51     public ActionForward edit(
52         ActionMapping mapping,
53         ActionForm actionForm,
54         HttpServletRequest JavaDoc request,
55         HttpServletResponse JavaDoc response)
56         throws IOException JavaDoc, ServletException JavaDoc
57     {
58         ActionForward forward = mapping.findForward("adminUser.page");
59         ActionMessages msgs = new ActionMessages();
60         try
61         {
62             RollerRequest rreq = RollerRequest.getRollerRequest(request);
63             if ( rreq.isUserAuthorizedToEdit() && rreq.isAdminUser() )
64             {
65                 UserAdminForm userForm = (UserAdminForm)actionForm;
66                 UserManager mgr = rreq.getRoller().getUserManager();
67                 
68                 if (userForm != null && userForm.getUserName() != null)
69                 {
70                     UserData user = mgr.getUser( userForm.getUserName(), false );
71                     if (user != null)
72                     {
73                         userForm.copyFrom(user, request.getLocale());
74                         
75                         // User must set new password twice
76
userForm.setPasswordText(null);
77                         userForm.setPasswordConfirm(null);
78                         
79                         // Join in the website enabled field
80
WebsiteData website =
81                             mgr.getWebsite(userForm.getUserName(), false);
82                         userForm.setUserEnabled(website.getIsEnabled());
83                     }
84                     else
85                     {
86                         msgs.add(ActionMessages.GLOBAL_MESSAGE,
87                             new ActionMessage("userAdmin.invalidNewUserName"));
88                         userForm.setUserName("");
89                     }
90                 }
91             }
92             else
93             {
94                 forward = mapping.findForward("access-denied");
95             }
96         }
97         catch (Exception JavaDoc e)
98         {
99             mLogger.error("ERROR in action",e);
100             throw new ServletException JavaDoc(e);
101         }
102         
103         if (request.getSession().getAttribute("cookieLogin") != null)
104         {
105             // TODO: make it possible to change passwords regardless of remember me
106
msgs.add(ActionMessages.GLOBAL_MESSAGE,
107                     new ActionMessage("userAdmin.cookieLogin"));
108         }
109         
110          saveMessages(request, msgs);
111          return forward;
112     }
113
114     //-----------------------------------------------------------------------
115
/**
116      * Process POST of edited user data, may cause delete of user.
117      */

118     public ActionForward update(
119         ActionMapping mapping,
120         ActionForm actionForm,
121         HttpServletRequest JavaDoc request,
122         HttpServletResponse JavaDoc response)
123         throws IOException JavaDoc, ServletException JavaDoc
124     {
125         ActionForward forward = mapping.findForward("adminUser.page");
126         ActionMessages msgs = new ActionMessages();
127         try
128         {
129             RollerRequest rreq = RollerRequest.getRollerRequest(request);
130             if ( rreq.isUserAuthorizedToEdit() && rreq.isAdminUser() )
131             {
132                 UserAdminForm userForm = (UserAdminForm)actionForm;
133                 UserManager mgr = rreq.getRoller().getUserManager();
134                 
135                 // Need system user to update user
136
rreq.getRoller().setUser(UserData.SYSTEM_USER);
137
138                UserData user = mgr.retrieveUser(userForm.getId());
139                 userForm.copyTo(user, request.getLocale()); // doesn't copy password
140

141                 if (userForm.getDelete())
142                 {
143                     // TODO: ask are you sure before deleting user
144
PageCacheFilter.removeFromCache( request, user );
145                     user = deleteUser(mapping, request, rreq, userForm, mgr, user);
146                     
147                     msgs.add(ActionMessages.GLOBAL_MESSAGE,
148                             new ActionMessage("userSettings.deleted"));
149                     saveMessages(request, msgs);
150                 }
151                 else
152                 {
153                     // If user set both password and passwordConfirm then reset
154
if ( !StringUtils.isEmpty(userForm.getPasswordText())
155                          && !StringUtils.isEmpty(userForm.getPasswordConfirm()))
156                     {
157                         try
158                         {
159                             user.resetPassword(rreq.getRoller(),
160                                userForm.getPasswordText(),
161                                userForm.getPasswordConfirm());
162                         }
163                         catch (RollerException e)
164                         {
165                             msgs.add(ActionMessages.GLOBAL_MESSAGE,
166                               new ActionMessage("userSettings.passwordResetError"));
167                         }
168                     }
169                     
170                     // Persist changes to user
171
mgr.storeUser( user );
172                     rreq.getRoller().commit();
173                     
174                     // Flush both main page and regular page caches
175
refreshIndexCache(request, rreq, userForm);
176                     PageCacheFilter.removeFromCache( request, user );
177                     
178                     msgs.add(ActionMessages.GLOBAL_MESSAGE,
179                         new ActionMessage("userSettings.saved"));
180                     saveMessages(request, msgs);
181                 }
182                 
183                 // Operation complete, return to edit action
184
userForm.setUserName(null);
185                 return edit(mapping, actionForm, request, response);
186             }
187             else
188             {
189                 forward = mapping.findForward("access-denied");
190             }
191         }
192         catch (Exception JavaDoc e)
193         {
194             mLogger.error("ERROR in action",e);
195             throw new ServletException JavaDoc(e);
196         }
197         return forward;
198     }
199
200     //-----------------------------------------------------------------------
201
/** Delete specified user and remove associated entries from the cache. */
202     private UserData deleteUser(
203             ActionMapping mapping,
204             HttpServletRequest JavaDoc request,
205             RollerRequest rreq,
206             UserAdminForm uaf,
207             UserManager mgr,
208             UserData ud) throws RollerException
209     {
210         // remove user's Entries from Lucene index
211
IndexManager indexManager = rreq.getRoller().getIndexManager();
212         indexManager.removeUserIndex(ud);
213         
214         // delete user from database
215
ud.remove();
216         rreq.getRoller().commit();
217         PageCacheFilter.removeFromCache( request, ud );
218         ud = null;
219
220         request.getSession().setAttribute(
221             RollerSession.STATUS_MESSAGE,
222                 uaf.getUserName() + " has been deleted");
223
224         uaf.reset(mapping, request);
225         
226         List JavaDoc users = mgr.getUsers(false);
227         request.setAttribute("users", users);
228         return ud;
229     }
230
231     //-----------------------------------------------------------------------
232
/**
233      * Rebuild a user's search index.
234      */

235     public ActionForward index(
236         ActionMapping mapping,
237         ActionForm actionForm,
238         HttpServletRequest JavaDoc request,
239         HttpServletResponse JavaDoc response)
240         throws IOException JavaDoc, ServletException JavaDoc
241     {
242         try
243         {
244             RollerRequest rreq = RollerRequest.getRollerRequest(request);
245             if ( rreq.isUserAuthorizedToEdit() && rreq.isAdminUser() )
246             {
247                 UserAdminForm uaf = (UserAdminForm)actionForm;
248                 
249                 // if admin requests an index be re-built, do it
250
IndexManager manager = rreq.getRoller().getIndexManager();
251                 manager.rebuildUserIndex();
252                 request.getSession().setAttribute(
253                     RollerSession.STATUS_MESSAGE,
254                         "Successfully scheduled rebuild of index for '"
255                         + uaf.getUserName() + "'");
256             }
257         }
258         catch (Exception JavaDoc e)
259         {
260             mLogger.error("ERROR in action",e);
261             throw new ServletException JavaDoc(e);
262         }
263         return edit(mapping, actionForm, request, response);
264     }
265
266 }
267
268
Popular Tags