KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > user > CallUpdateUserAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.webapp.action.core.user;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.User;
20 import com.blandware.atleap.service.core.UserManager;
21 import com.blandware.atleap.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.UserForm;
23 import com.blandware.atleap.webapp.util.core.WebappUtil;
24 import org.apache.struts.action.ActionForm;
25 import org.apache.struts.action.ActionForward;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionMessage;
28 import org.apache.struts.action.ActionMessages;
29
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 /**
34  * <p>Prepares form bean to update user's data
35  * </p>
36  * <p><a HREF="CallUpdateUserAction.java.htm"><i>View Source</i></a></p>
37  * <p/>
38  *
39  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
40  * @version $Revision: 1.17 $ $Date: 2006/03/10 17:10:30 $
41  * @struts.action path="/core/user/callUpdate"
42  * name="userForm"
43  * scope="request"
44  * validate="false"
45  * roles="core-user-update, core-user-updateOneself, core-user-view, core-user-viewOneself"
46  * @struts.action-forward name="updateUser"
47  * path=".core.user.update"
48  * @struts.action-forward name="listUsers"
49  * path="/core/user/list.do"
50  * redirect="true"
51  */

52 public final class CallUpdateUserAction extends BaseAction {
53     /**
54      * @param mapping The ActionMapping used to select this instance
55      * @param form The optional ActionForm bean for this request (if any)
56      * @param request The HTTP request we are proceeding
57      * @param response The HTTP response we are creating
58      * @return an ActionForward instance describing where and how
59      * control should be forwarded, or null if response
60      * has already been completed
61      */

62     public ActionForward execute(ActionMapping mapping, ActionForm form,
63                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
64
65         if ( isCancelled(request) ) {
66             if ( request.isUserInRole("core-user-list") ) {
67                 return mapping.findForward("listUsers");
68             } else {
69                 return mapping.findForward("admin");
70             }
71         }
72
73         if ( !request.isUserInRole("core-user-update") && !request.isUserInRole("core-user-updateOneself") ) {
74             response.sendError(HttpServletResponse.SC_FORBIDDEN);
75             return null;
76         }
77
78         UserForm userForm = (UserForm) form;
79         String JavaDoc userName = null;
80         if ( userForm.getName() != null ) {
81             userName = userForm.getName();
82         } else {
83             if ( log.isWarnEnabled() ) {
84                 log.warn("Missing user name. Returning to list...");
85             }
86             return mapping.findForward("listUsers");
87         }
88
89         if ( !userName.equals(request.getRemoteUser()) && !request.isUserInRole("core-user-update") ) {
90             response.sendError(HttpServletResponse.SC_FORBIDDEN);
91             return null;
92         }
93
94         UserManager userManager = (UserManager) getBean(Constants.USER_MANAGER_BEAN);
95         User user = userManager.retrieveUser(userName);
96         if ( user == null ) {
97             // user not found. it might be deleted by someone else
98
ActionMessages errors = new ActionMessages();
99             errors.add("userNotFound", new ActionMessage("core.user.errors.notFound"));
100             saveErrors(request, errors);
101             return mapping.findForward("listUsers");
102         }
103
104         WebappUtil.copyProperties(userForm, user, request);
105
106         userForm.setPassword("");
107
108         // save transaction token in request
109
saveToken(request);
110         return mapping.findForward("updateUser");
111     }
112
113 }
Popular Tags