View Javadoc
1   /*
2    * Copyright (c) 2015, Fraunhofer FOKUS
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * 
8    * * Redistributions of source code must retain the above copyright notice, this
9    *   list of conditions and the following disclaimer.
10   * 
11   * * Redistributions in binary form must reproduce the above copyright notice,
12   *   this list of conditions and the following disclaimer in the documentation
13   *   and/or other materials provided with the distribution.
14   * 
15   * * Neither the name of particity nor the names of its
16   *   contributors may be used to endorse or promote products derived from
17   *   this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   * 
30   * 
31   * 
32   * @author sma
33   */
34  package de.fraunhofer.fokus.oefit.particity.portlet.login.form;
35  
36  import java.util.List;
37  
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  import javax.servlet.http.HttpSession;
47  import javax.validation.Valid;
48  
49  import org.springframework.stereotype.Controller;
50  import org.springframework.ui.Model;
51  import org.springframework.validation.BindingResult;
52  import org.springframework.web.bind.annotation.ModelAttribute;
53  import org.springframework.web.bind.annotation.RequestMapping;
54  import org.springframework.web.portlet.bind.annotation.ActionMapping;
55  import org.springframework.web.portlet.bind.annotation.RenderMapping;
56  
57  import com.liferay.portal.kernel.exception.SystemException;
58  import com.liferay.portal.kernel.log.Log;
59  import com.liferay.portal.kernel.log.LogFactoryUtil;
60  import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
61  import com.liferay.portal.kernel.servlet.SessionErrors;
62  import com.liferay.portal.kernel.servlet.SessionMessages;
63  import com.liferay.portal.kernel.util.JavaConstants;
64  import com.liferay.portal.model.Role;
65  import com.liferay.portal.model.User;
66  import com.liferay.portal.security.auth.Authenticator;
67  import com.liferay.portal.service.UserLocalServiceUtil;
68  import com.liferay.portal.theme.ThemeDisplay;
69  import com.liferay.portal.util.PortalUtil;
70  
71  import de.fraunhofer.fokus.oefit.adhoc.custom.E_Role;
72  import de.fraunhofer.fokus.oefit.adhoc.forms.ProfileForm;
73  import de.fraunhofer.fokus.oefit.particity.portlet.BaseController;
74  import de.fraunhofer.fokus.oefit.adhoc.custom.CustomPortalServiceHandler;
75  
76  /**
77   * Controller for the login portlet
78   * 
79   * Redirects to login/form by default
80   *
81   */
82  @Controller("loginController")
83  @RequestMapping("view")
84  public class LoginController extends BaseController {
85  
86  	private static final Log	 m_objLog	                        = LogFactoryUtil
87  	                                                                        .getLog(LoginController.class);
88  
89  	private static final boolean	_CHECK_METHOD_ON_PROCESS_ACTION	= false;
90  
91  	/**
92  	 * Gets the user data.
93  	 *
94  	 * @return the user data
95  	 */
96  	@ModelAttribute("userData")
97  	public ProfileForm getUserData() {
98  		return new ProfileForm();
99  	}
100 
101 	/**
102 	 * Checks if is check method on process action.
103 	 *
104 	 * @return true, if is check method on process action
105 	 */
106 	protected boolean isCheckMethodOnProcessAction() {
107 		return _CHECK_METHOD_ON_PROCESS_ACTION;
108 	}
109 
110 	/**
111 	 * Login.
112 	 *
113 	 * @param data the data
114 	 * @param result the result
115 	 * @param request the request
116 	 * @param response the response
117 	 * @param model the model
118 	 */
119 	@ActionMapping(params = "action=login")
120 	public void login(@Valid @ModelAttribute("data") final ProfileForm data,
121 	        final BindingResult result, final ActionRequest request,
122 	        final ActionResponse response, final Model model) {
123 		m_objLog.debug("login::start(" + data.getMail() + ")");
124 
125 		if (!result.hasErrors()) {
126 			try {
127 				/*
128 				 * ClassLoader pcl = PortalClassLoaderUtil.getClassLoader();
129 				 * Class lClass =
130 				 * pcl.loadClass("com.liferay.portlet.login.util.LoginUtil");
131 				 * Method method = lClass.getDeclaredMethod("login",
132 				 * HttpServletRequest.class, HttpServletResponse.class,
133 				 * String.class, String.class, Boolean.TYPE, String.class);
134 				 * method.invoke(null,
135 				 * PortalUtil.getHttpServletRequest(request),
136 				 * PortalUtil.getHttpServletResponse(response), data.getMail(),
137 				 * data.getPass1(), false, CompanyConstants.AUTH_TYPE_EA);
138 				 */
139 
140 				/*
141 				 * Class<?> loginUtilClass = ClassResolverUtil
142 				 * .resolveByPortalClassLoader(LOGIN_UTIL_FQCN); MethodKey
143 				 * methodKey = new MethodKey(loginUtilClass, LOGIN_METHOD,
144 				 * LOGIN_PARAM_TYPES); PortalClassInvoker.invoke(false,
145 				 * methodKey, PortalUtil.getHttpServletRequest(request),
146 				 * PortalUtil.getHttpServletResponse(response), data.getMail(),
147 				 * data.getPass1(), true, CompanyConstants.AUTH_TYPE_EA);
148 				 */
149 				final ThemeDisplay td = this.getThemeDisplay(request);
150 				final int status = UserLocalServiceUtil
151 				        .authenticateByEmailAddress(
152 				                td.getCompanyId(), data.getMail(),
153 				                data.getPass1(),
154 				                null, null, null);
155 
156 				if (status == Authenticator.SUCCESS) {
157 
158 					final User user = UserLocalServiceUtil
159 					        .getUserByEmailAddress(
160 					                td.getCompanyId(), data.getMail());
161 					// Long userId =
162 					// UserLocalServiceUtil.getUserIdByEmailAddress(td.getCompanyId(),
163 					// data.getMail());
164 					final String userIdString = String
165 					        .valueOf(user.getUserId());
166 
167 					final HttpServletRequest hrequest = PortalUtil
168 					        .getHttpServletRequest(request);
169 					final HttpSession session = hrequest.getSession();
170 
171 					session.setAttribute("j_username", userIdString);
172 					session.setAttribute("j_password", user.getPassword());
173 					session.setAttribute("j_remoteuser", userIdString);
174 
175 					E_Role erole = null;
176 
177 					if (user != null && user.getEmailAddress() != null) {
178 						List<Role> roles = null;
179 						try {
180 							roles = user.getRoles();
181 						} catch (final SystemException e) {
182 						}
183 						if (roles != null) {
184 							for (final Role role : roles) {
185 								erole = CustomPortalServiceHandler.matchesRole(role);
186 								if (erole != null) {
187 									break;
188 								}
189 							}
190 						}
191 					}
192 
193 					if (erole != null) {
194 						response.sendRedirect(erole.getHomeUrl());
195 					}
196 
197 				} else {
198 
199 					final PortletConfig portletConfig = (PortletConfig) request
200 					        .getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
201 					final LiferayPortletConfig liferayPortletConfig = (LiferayPortletConfig) portletConfig;
202 					SessionMessages
203 					        .add(request,
204 					                liferayPortletConfig.getPortletId()
205 					                        + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
206 
207 					// SessionMessages.add(request,
208 					// getThemeDisplay(request).getPortletDisplay().getPortletName()
209 					// + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
210 					m_objLog.debug("Login failed!");
211 					SessionErrors.add(request, "common.form.login.failed");
212 					// result.addError(new FieldError("data", "mail",
213 					// "common.form.login.failed"));
214 				}
215 
216 			} catch (final Throwable t) {
217 				m_objLog.warn(t);
218 			}
219 
220 			/*
221 			 * String className =
222 			 * "com.liferay.portlet.login.action.LoginAction"; PortletConfig
223 			 * portletConfig = (PortletConfig)
224 			 * request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
225 			 * NoRedirectActionResponse noRedirectActionResponse = new
226 			 * NoRedirectActionResponse(response); try {
227 			 * PortletActionInvoker.processAction( className, portletConfig,
228 			 * request, noRedirectActionResponse); } catch (Throwable e) {
229 			 * m_objLog.error(e, e); } if
230 			 * (!Validator.isNull(noRedirectActionResponse.getRedirectLocation
231 			 * ())) { String redirect = PortalUtil.getPathMain() +
232 			 * "/portal/login?login=" + data.getMail() + "&password=" +
233 			 * data.getPass1() + "&rememberMe=true"; try {
234 			 * response.sendRedirect(redirect); } catch (Throwable e) {
235 			 * m_objLog.warn(e); } }
236 			 */
237 		}
238 
239 		m_objLog.debug("login::end");
240 	}
241 
242 	/**
243 	 * Render.
244 	 *
245 	 * @param request the request
246 	 * @param response the response
247 	 * @param model the model
248 	 * @return the string
249 	 */
250 	@RequestMapping(value = "view")
251 	@RenderMapping
252 	public String render(final RenderRequest request,
253 	        final RenderResponse response,
254 	        final Model model) {
255 		m_objLog.trace("render::start");
256 		String page = request.getParameter("jspPage");
257 		if (page == null) {
258 			page = "form";
259 		}
260 		final String error = request.getParameter("error");
261 		if (error != null) {
262 			model.addAttribute("error", error);
263 			m_objLog.warn("Handing over error " + error);
264 			final String ePage = request.getParameter("errorPage");
265 			if (ePage != null) {
266 				page = ePage;
267 			}
268 		}
269 		
270 		m_objLog.trace("render::end(" + page + ")");
271 
272 		return page;
273 	}
274 
275 }