KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.roller.presentation.website.actions;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.apache.struts.action.ActionErrors;
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionForward;
8 import org.apache.struts.action.ActionMapping;
9 import org.apache.struts.action.ActionMessage;
10 import org.apache.struts.action.ActionMessages;
11 import org.roller.RollerException;
12 import org.roller.model.UserManager;
13 import org.roller.pojos.UserData;
14 import org.roller.pojos.WebsiteData;
15 import org.roller.presentation.RollerRequest;
16 import org.roller.presentation.pagecache.PageCacheFilter;
17 import org.roller.presentation.website.formbeans.UserFormEx;
18 import org.roller.util.StringUtils;
19
20 import java.io.IOException JavaDoc;
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25
26 /////////////////////////////////////////////////////////////////////////////
27
/**
28  * Actions for editing your user information.
29  *
30  * @struts.action name="userFormEx" path="/editor/user"
31  * scope="session" parameter="method"
32  *
33  * @struts.action-forward name="editUser.page" path="/website/UserEdit.jsp"
34  */

35 public class UserEditAction extends UserBaseAction
36 {
37     private static Log mLogger =
38         LogFactory.getFactory().getInstance(UserEditAction.class);
39
40     //-----------------------------------------------------------------------
41
/** Handle GET for user edit page */
42     public ActionForward edit(
43         ActionMapping mapping,
44         ActionForm actionForm,
45         HttpServletRequest JavaDoc request,
46         HttpServletResponse JavaDoc response)
47         throws IOException JavaDoc, ServletException JavaDoc
48     {
49         ActionForward forward = mapping.findForward("editUser.page");
50         try
51         {
52             RollerRequest rreq = RollerRequest.getRollerRequest(request);
53             if ( rreq.isUserAuthorizedToEdit() )
54             {
55                 UserData ud = rreq.getUser();
56                 request.setAttribute("user",ud);
57
58                 UserFormEx form = (UserFormEx)actionForm;
59                 form.copyFrom(ud, request.getLocale());
60                 
61                 // User must set new password twice
62
form.setPasswordText(null);
63                 form.setPasswordConfirm(null);
64
65                 loadRequestObjects(request, rreq, ud, form);
66             }
67             else
68             {
69                 forward = mapping.findForward("access-denied");
70             }
71         }
72         catch (Exception JavaDoc e)
73         {
74             mLogger.error("ERROR in action",e);
75             throw new ServletException JavaDoc(e);
76         }
77         
78         // if user logged in with a cookie, display a warning that they
79
// can't change passwords
80
if (mLogger.isDebugEnabled())
81         {
82             log.debug("checking for cookieLogin...");
83         }
84
85         if (request.getSession().getAttribute("cookieLogin") != null) {
86             ActionMessages messages = new ActionMessages();
87
88             // add warning messages
89
messages.add(ActionMessages.GLOBAL_MESSAGE,
90                          new ActionMessage("userSettings.cookieLogin"));
91             saveMessages(request, messages);
92         }
93         
94         return forward;
95     }
96
97     //-----------------------------------------------------------------------
98
/** Handle POST from user edit form */
99     public ActionForward update(
100         ActionMapping mapping,
101         ActionForm actionForm,
102         HttpServletRequest JavaDoc request,
103         HttpServletResponse JavaDoc response)
104         throws IOException JavaDoc, ServletException JavaDoc
105     {
106         UserFormEx form = (UserFormEx)actionForm;
107         ActionForward forward = mapping.findForward("editUser.page");
108         ActionMessages msgs = new ActionMessages();
109         try
110         {
111             RollerRequest rreq = RollerRequest.getRollerRequest(request);
112             if (rreq.isUserAuthorizedToEdit())
113             {
114                 ActionMessages errors = validate(form, new ActionErrors());
115                 if (errors.size() == 0)
116                 {
117                     UserManager mgr = rreq.getRoller().getUserManager();
118                     UserData data = mgr.getUser( form.getUserName() );
119                     
120                     // Need system user to update new user
121
rreq.getRoller().setUser(UserData.SYSTEM_USER);
122                     
123                     // Copy data from form to persistent object (won't copy over password)
124
form.copyTo(data, request.getLocale());
125                     
126                     // If user set both password and passwordConfirm then reset password
127
if ( !StringUtils.isEmpty(form.getPasswordText())
128                          && !StringUtils.isEmpty(form.getPasswordConfirm()))
129                     {
130                         try
131                         {
132                             data.resetPassword(rreq.getRoller(),
133                                form.getPasswordText(),
134                                form.getPasswordConfirm());
135                         }
136                         catch (RollerException e)
137                         {
138                             msgs.add(ActionMessages.GLOBAL_MESSAGE,
139                                 new ActionMessage("userSettings.passwordResetError"));
140                         }
141                     }
142                 
143                     // Set website fields
144
WebsiteData website = mgr.getWebsite(data.getUserName());
145                     website.setEditorTheme(form.getTheme());
146                     website.setLocale(form.getLocale());
147                     website.setTimezone(form.getTimezone());
148     
149                     // Persist changes
150
mgr.storeUser( data );
151                     mgr.storeWebsite( website );
152                     rreq.getRoller().commit();
153                     PageCacheFilter.removeFromCache( request,data );
154     
155                     msgs.add(null, new ActionMessage("userSettings.saved"));
156                     saveMessages(request, msgs);
157                 }
158                 else
159                 {
160                     saveErrors(request, errors);
161                 }
162                 return edit(mapping, actionForm, request, response);
163             }
164             return mapping.findForward("access-denied");
165         }
166         catch (Exception JavaDoc e)
167         {
168             mLogger.error("ERROR in action",e);
169             throw new ServletException JavaDoc(e);
170         }
171     }
172     
173 }
174
175
176
177
178
179
180
Popular Tags