View Javadoc

1   // Created on 24.11.2003
2   package com.pnpconsult.zeiterfassung.actions;
3   
4   import javax.servlet.http.HttpServletRequest;
5   import javax.servlet.http.HttpServletResponse;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.apache.struts.action.Action;
10  import org.apache.struts.action.ActionForm;
11  import org.apache.struts.action.ActionForward;
12  import org.apache.struts.action.ActionMapping;
13  
14  /***
15   * This {@link Action} checks the roles of the user and redirects to the first
16   * mapping role homepage:
17   * <ul>
18   *   <li><tt>user</tt>
19   *   <li><tt>manager</tt> (billing)
20   *   <li><tt>admin</tt>
21   * </ul>
22   * If the user is archived or has none of the roles mentioned above, she gets
23   * redirected to the {@link LogoutAction}.
24   * 
25   * 
26   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a>
27   * @version $Id: LoginAction.java,v 1.5 2004/06/09 19:26:40 powerpete Exp $
28   * 
29   * @struts.action path="/secure/login"
30   * @struts.action-forward name="user" path="/secure/user/input.do"
31   * @struts.action-forward name="manager" path="/secure/manager/managerMenu.do"
32   * @struts.action-forward name="admin" path="/secure/admin/adminMenu.do"
33   * @struts.action-forward name="logout" path="/logout.do"
34   */
35  public class LoginAction extends Action
36  {
37      private static final Log LOG = LogFactory.getLog(LoginAction.class);
38  
39      public ActionForward execute(
40          ActionMapping mapping,
41          ActionForm form,
42          HttpServletRequest request,
43          HttpServletResponse response)
44          throws Exception
45      {
46          String login = request.getUserPrincipal().getName();
47          ActionForward forward =
48              findForward(
49                  new String[] { "user", "manager", "admin" },
50                  mapping,
51                  request);
52          if (forward == null)
53          {
54              LOG.error("User " + login + " is in invalid role.");
55              return mapping.findForward("logout");
56          }
57          return forward;
58      }
59  
60      private ActionForward findForward(
61          String[] roles,
62          ActionMapping mapping,
63          HttpServletRequest request)
64      {
65          for (int i = 0; i < roles.length; i++)
66          {
67              String role = roles[i];
68              if (request.isUserInRole(role))
69              {
70                  return mapping.findForward(role);
71              }
72          }
73          return null;
74      }
75  }