KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.struts.action.ActionForm;
24 import org.apache.struts.action.ActionForward;
25 import org.apache.struts.action.ActionMapping;
26 import org.apache.struts.action.ActionMessage;
27 import org.apache.struts.action.ActionMessages;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 /**
33  * <p>Exposes bean to request to view its properties on JSP page (user is to be
34  * viewed)
35  * </p>
36  * <p><a HREF="ViewUserAction.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.19 $ $Date: 2006/03/10 17:10:30 $
41  * @struts.action path="/core/user/view"
42  * name="userForm"
43  * scope="request"
44  * validate="false"
45  * roles="core-user-view, core-user-viewOneself"
46  * @struts.action-forward name="viewUser"
47  * path=".core.user.view"
48  * @struts.action-forward name="listUsers"
49  * path="/core/user/list.do"
50  * redirect="true"
51  */

52 public final class ViewUserAction 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         UserForm userForm = (UserForm) form;
66         String JavaDoc userName = null;
67         if ( userForm.getName() != null ) {
68             userName = userForm.getName();
69         } else if ( request.getRemoteUser() != null ) {
70             userName = request.getRemoteUser();
71         } else {
72             if ( log.isWarnEnabled() ) {
73                 log.warn("Missing user name. Returning to list...");
74             }
75             if ( request.isUserInRole("core-user-list") ) {
76                 return mapping.findForward("listUsers");
77             } else {
78                 return mapping.findForward("admin");
79             }
80
81         }
82
83         if ( !userName.equals(request.getRemoteUser()) && !request.isUserInRole("core-user-view") ) {
84             response.sendError(HttpServletResponse.SC_FORBIDDEN);
85             return null;
86         }
87
88         UserManager userManager = (UserManager) getBean(Constants.USER_MANAGER_BEAN);
89         User user = userManager.retrieveUser(userName);
90         if ( user == null ) {
91             // user not found. it might be deleted by someone else
92
ActionMessages errors = new ActionMessages();
93             errors.add("userNotFound", new ActionMessage("core.user.errors.notFound"));
94             saveErrors(request, errors);
95             return mapping.findForward("listUsers");
96         }
97
98         request.setAttribute("viewingOneself", new Boolean JavaDoc(userName.equals(request.getRemoteUser())));
99
100         request.setAttribute("user", user);
101         return mapping.findForward("viewUser");
102     }
103 }
Popular Tags