##### Coverage Report - org.apache.turbine.modules.actions.LoginUser

Classes in this File | Line Coverage | Branch Coverage | Complexity
LoginUser |
57% | 15/26
25% | 2/8
| 8.0;8

1 |  |
     package org.apache.turbine.modules.actions;
2 |  |
3 |  |
     /*
4 |  |
      * Licensed to the Apache Software Foundation (ASF) under one
5 |  |
      * or more contributor license agreements.  See the NOTICE file
6 |  |
      * distributed with this work for additional information
7 |  |
      * regarding copyright ownership.  The ASF licenses this file
8 |  |
      * to you under the Apache License, Version 2.0 (the
9 |  |
      * "License"); you may not use this file except in compliance
10 |  |
      * with the License.  You may obtain a copy of the License at
11 |  |
      *
12 |  |
      *   http://www.apache.org/licenses/LICENSE-2.0
13 |  |
      *
14 |  |
      * Unless required by applicable law or agreed to in writing,
15 |  |
      * software distributed under the License is distributed on an
16 |  |
      * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 |  |
      * KIND, either express or implied.  See the License for the
18 |  |
      * specific language governing permissions and limitations
19 |  |
      * under the License.
20 |  |
      */
21 |  |
22 |  |
     import org.apache.commons.lang.StringUtils;
23 |  |
     import org.apache.commons.logging.Log;
24 |  |
     import org.apache.commons.logging.LogFactory;
25 |  |
     import org.apache.fulcrum.security.util.DataBackendException;
26 |  |
     import org.apache.fulcrum.security.util.FulcrumSecurityException;
27 |  |
     import org.apache.turbine.TurbineConstants;
28 |  |
     import org.apache.turbine.annotation.TurbineConfiguration;
29 |  |
     import org.apache.turbine.annotation.TurbineService;
30 |  |
     import org.apache.turbine.modules.Action;
31 |  |
     import org.apache.turbine.om.security.User;
32 |  |
     import org.apache.turbine.pipeline.PipelineData;
33 |  |
     import org.apache.turbine.services.security.SecurityService;
34 |  |
     import org.apache.turbine.util.RunData;
35 |  |
     import org.apache.turbine.util.TurbineException;
36 |  |
37 |  |
     /**
38 |  |
      * This is where we authenticate the user logging into the system
39 |  |
      * against a user in the database. If the user exists in the database
40 |  |
      * that users last login time will be updated.
41 |  |
      *
42 |  |
      * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
43 |  |
      * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44 |  |
      * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
45 |  |
      * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
46 |  |
      * @version $Id: LoginUser.java 1812628 2017-10-19 12:34:25Z gk $
47 |  |
      */
48 | 1 |
     public class LoginUser
49 |  |
             extends Action
50 |  |
     {
51 |  |
         /** CGI Parameter for the user name */
52 |  |
         public static final String CGI_USERNAME = "username";
53 |  |
54 |  |
         /** CGI Parameter for the password */
55 |  |
         public static final String CGI_PASSWORD = "password";
56 |  |
57 |  |
         /** Logging */
58 | 1 |
         private static Log log = LogFactory.getLog(LoginUser.class);
59 |  |
60 |  |
         /** Injected service instance */
61 |  |
         @TurbineService
62 |  |
         private SecurityService security;
63 |  |
64 | 1 |
         @TurbineConfiguration( TurbineConstants.LOGIN_ERROR )
65 |  |
         private String loginError = "";
66 |  |
67 |  |
         @TurbineConfiguration( TurbineConstants.TEMPLATE_LOGIN )
68 |  |
         private String templateLogin;
69 |  |
70 |  |
         @TurbineConfiguration( TurbineConstants.SCREEN_LOGIN )
71 |  |
         private String screenLogin;
72 |  |
73 |  |
         /**
74 |  |
          * Updates the user's LastLogin timestamp, sets their state to
75 |  |
          * "logged in" and calls RunData.setUser() .  If the user cannot
76 |  |
          * be authenticated (database error?) the user is assigned
77 |  |
          * anonymous status and, if tr.props contains a TEMPLATE_LOGIN,
78 |  |
          * the screenTemplate is set to this, otherwise the screen is set
79 |  |
          * to SCREEN_LOGIN
80 |  |
          *
81 |  |
          * @param     pipelineData Turbine information.
82 |  |
          * @throws FulcrumSecurityException could not get instance of the
83 |  |
          *            anonymous user
84 |  |
          */
85 |  |
         @Override
86 |  |
         public void doPerform(PipelineData pipelineData)
87 |  |
                 throws FulcrumSecurityException
88 |  |
         {
89 | 1 |
             RunData data = getRunData(pipelineData);
90 | 1 |
             String username = data.getParameters().getString(CGI_USERNAME, "");
91 | 1 |
             String password = data.getParameters().getString(CGI_PASSWORD, "");
92 |  |
93 | 1 |
             if (StringUtils.isEmpty(username))
94 |  |
             {
95 | 0 |
                 return;
96 |  |
             }
97 |  |
98 |  |
             try
99 |  |
             {
100 | 1 |
                     if (username.equals(security.getAnonymousUser().getName()))
101 |  |
                 {
102 | 0 |
                     throw new TurbineException("Anonymous user cannot login");
103 |  |
                 }
104 |  |
105 |  |
                 // Authenticate the user and get the object.
106 | 1 |
                 User user = security.getAuthenticatedUser(username, password);
107 |  |
108 |  |
                 // Store the user object.
109 | 1 |
                 data.setUser(user);
110 |  |
111 |  |
                 // Mark the user as being logged in.
112 | 1 |
                 user.setHasLoggedIn(Boolean.TRUE);
113 |  |
114 |  |
                 // Set the last_login date in the database.
115 | 1 |
                 user.updateLastLogin();
116 |  |
117 |  |
                 // This only happens if the user is valid; otherwise, we
118 |  |
                 // will get a valueBound in the User object when we don't
119 |  |
                 // want to because the username is not set yet.  Save the
120 |  |
                 // User object into the session.
121 | 1 |
                 data.save();
122 |  |
123 |  |
                 /*
124 |  |
                  * If the setPage("template.vm") method has not
125 |  |
                  * been used in the template to authenticate the
126 |  |
                  * user (usually Login.vm), then the user will
127 |  |
                  * be forwarded to the template that is specified
128 |  |
                  * by the "template.home" property as listed in
129 |  |
                  * TR.props for the webapp.
130 |  |
                  */
131 |  |
132 |  |
             }
133 | 0 |
             catch (Exception e)
134 |  |
             {
135 | 0 |
                 if (e instanceof DataBackendException)
136 |  |
                 {
137 | 0 |
                     log.error(e);
138 |  |
                 }
139 |  |
140 |  |
                 // Set Error Message and clean out the user.
141 | 0 |
                 data.setMessage(loginError);
142 | 0 |
                 User anonymousUser = security.getAnonymousUser();
143 | 0 |
                 data.setUser(anonymousUser);
144 |  |
145 | 0 |
                 if (StringUtils.isNotEmpty(templateLogin))
146 |  |
                 {
147 |  |
                     // We're running in a templating solution
148 | 0 |
                     data.setScreenTemplate(templateLogin);
149 |  |
                 }
150 |  |
                 else
151 |  |
                 {
152 | 0 |
                     data.setScreen(screenLogin);
153 |  |
                 }
154 | 1 |
             }
155 | 1 |
         }
156 |  |
157 |  |
     }