Configure a custom URL target resolver in order to define the proper URL for bound users

Bound users will be redirected to user dashboard after login.

FEA: ItEr76S28UserDashboard
This commit is contained in:
Manuel Rego Casasnovas 2012-05-16 10:36:05 +02:00
parent ce34595afd
commit db692ab010
2 changed files with 75 additions and 1 deletions

View file

@ -0,0 +1,65 @@
/*
* This file is part of LibrePlan
*
* Copyright (C) 2012 Igalia, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.libreplan.web.users.services;
import javax.servlet.http.HttpServletRequest;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.Authentication;
import org.springframework.security.ui.TargetUrlResolverImpl;
import org.springframework.security.ui.savedrequest.SavedRequest;
import org.springframework.security.userdetails.UserDetails;
/**
* Determines the URL for authenticated users depending on if user is bound or
* not to any resource.<br />
*
* If the user is bound to a resource then the target URL will be the user
* dashboard.
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
public class CustomTargetUrlResolver extends TargetUrlResolverImpl {
private final static String USER_DASHBOARD_URL = "/myaccount/userDashboard.zul";
@Autowired
private IUserDAO userDAO;
@Override
public String determineTargetUrl(SavedRequest savedRequest,
HttpServletRequest currentRequest, Authentication auth) {
UserDetails userDetails = (UserDetails) auth.getPrincipal();
try {
User user = userDAO.findByLoginName(userDetails.getUsername());
if (user.isBound()) {
return USER_DASHBOARD_URL;
}
} catch (InstanceNotFoundException e) {
throw new RuntimeException(e);
}
return super.determineTargetUrl(savedRequest, currentRequest, auth);
}
}

View file

@ -57,6 +57,9 @@
<intercept-url pattern="/expensesheet/**" access="ROLE_ADMINISTRATION,ROLE_EXPENSE_TRACKING"/>
<intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" />
<!-- These have been added because of auto-config is false now in order
to use a custom authentication filter.
See: http://static.springsource.org/spring-security/site/docs/2.0.x/reference/ns-config.html#ns-auto-config -->
<anonymous />
<http-basic />
<logout />
@ -121,7 +124,9 @@
<beans:bean id="ldapUserDetailsService"
class="org.libreplan.web.users.services.LDAPUserDetailsService" />
<!-- Configured a custom authentication filter -->
<!-- Configured a custom authentication filter:
* This needs a custom authentication entry point
* Also a custom target URL resolver is used to determine the URL depending on the user -->
<authentication-manager alias="authenticationManager" />
<beans:bean id="customAuthenticationFilter"
@ -131,6 +136,7 @@
<beans:property name="defaultTargetUrl" value="/planner/index.zul" />
<beans:property name="authenticationFailureUrl" value="/common/layout/login.zul?login_error=true" />
<beans:property name="allowSessionCreation" value="true" />
<beans:property name="targetUrlResolver" ref="customTargetUrlResolver" />
</beans:bean>
<beans:bean id="customAuthenticationEntryPoint"
@ -138,4 +144,7 @@
<beans:property name="loginFormUrl" value="/common/layout/login.zul"/>
</beans:bean>
<beans:bean id="customTargetUrlResolver"
class="org.libreplan.web.users.services.CustomTargetUrlResolver" />
</beans:beans>