ItEr53S11RFInfraestucturaEscenariosItEr52S11: CustomUser references the Scenario instead of the scenario's name.

This commit is contained in:
Óscar González Fernández 2010-04-08 00:33:57 +02:00
parent 4836b6d57a
commit 22cb58647d
5 changed files with 44 additions and 17 deletions

View file

@ -23,6 +23,7 @@ package org.navalplanner.web.common;
import java.util.Collections;
import java.util.List;
import org.navalplanner.business.scenarios.bootstrap.PredefinedScenarios;
import org.navalplanner.business.scenarios.entities.Scenario;
import org.navalplanner.web.common.components.bandboxsearch.BandboxSearch;
import org.navalplanner.web.security.SecurityUtils;
@ -58,10 +59,10 @@ public class TemplateController extends GenericForwardComposer {
window = (Window) comp.getFellow("changeScenarioWindow");
}
public String getScenario() {
public Scenario getScenario() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
return "";
return PredefinedScenarios.MASTER.getScenario();
}
CustomUser customUser = (CustomUser) authentication.getPrincipal();
return customUser.getScenario();
@ -83,7 +84,7 @@ public class TemplateController extends GenericForwardComposer {
if (templateModel == null) {
return null;
}
return templateModel.getScenarioByName(getScenario());
return getScenario();
}
public void accept() {
@ -97,7 +98,7 @@ public class TemplateController extends GenericForwardComposer {
CustomUser customUser = (CustomUser) SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
customUser.setScenario(scenario.getName());
customUser.setScenario(scenario);
window.setVisible(false);
Executions.sendRedirect("/");

View file

@ -20,6 +20,7 @@
package org.navalplanner.web.users.services;
import org.apache.commons.lang.Validate;
import org.navalplanner.business.scenarios.entities.Scenario;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.User;
@ -32,22 +33,25 @@ import org.springframework.security.userdetails.User;
*/
public class CustomUser extends User {
private String scenario;
private Scenario scenario;
public CustomUser(String username, String password, boolean enabled,
boolean accountNonExpired, boolean credentialsNonExpired,
boolean accountNonLocked, GrantedAuthority[] authorities,
String scenario) throws IllegalArgumentException {
Scenario scenario) throws IllegalArgumentException {
super(username, password, enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, authorities);
Validate.notNull(scenario);
this.scenario = scenario;
// force name load
scenario.getName();
}
public String getScenario() {
public Scenario getScenario() {
return scenario;
}
public void setScenario(String scenario) {
public void setScenario(Scenario scenario) {
this.scenario = scenario;
}

View file

@ -75,12 +75,9 @@ public class DBUserDetailsService implements UserDetailsService {
allRoles.addAll(eachProfile.getRoles());
}
String scenarioName;
Scenario scenario = user.getLastConnectedScenario();
if (scenario == null) {
scenarioName = PredefinedScenarios.MASTER.getName();
} else {
scenarioName = scenario.getName();
scenario = PredefinedScenarios.MASTER.getScenario();
}
return new CustomUser(
@ -91,8 +88,7 @@ public class DBUserDetailsService implements UserDetailsService {
true, // credentialsNonExpired
true, // accountNonLocked
getGrantedAuthorities(allRoles),
scenarioName);
scenario);
}
private GrantedAuthority[] getGrantedAuthorities(Set<UserRole> roles) {

View file

@ -67,7 +67,7 @@
<n:td class="usuario">
${i18n:_('scenario')}:
<button onClick="templateCtrl.changeScenario();"
label="${templateCtrl.scenario}" />
label="${templateCtrl.scenario.name}" />
<window id="changeScenarioWindow" visible="false"
title="${i18n:_('Change scenario')}">
<grid>

View file

@ -29,8 +29,12 @@ import static org.navalplanner.web.test.WebappGlobalNames.WEBAPP_SPRING_SECURITY
import java.util.HashSet;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.navalplanner.business.common.IAdHocTransactionService;
import org.navalplanner.business.common.IOnTransaction;
import org.navalplanner.business.scenarios.bootstrap.IScenariosBootstrap;
import org.navalplanner.business.users.entities.UserRole;
import org.navalplanner.web.users.bootstrap.IUsersBootstrapInDB;
import org.navalplanner.web.users.bootstrap.MandatoryUser;
@ -44,7 +48,6 @@ import org.springframework.transaction.annotation.Transactional;
/**
* Tests for <code>DBUserDetailsService</code>.
*
* @author Fernando Bellas Permuy <fbellas@udc.es>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ -60,9 +63,32 @@ public class DBUserDetailsServiceTest {
@Autowired
private IUsersBootstrapInDB usersBootstrap;
@Autowired
private IScenariosBootstrap scenariosBootstrap;
@Autowired
private IAdHocTransactionService transactionService;
@Before
public void loadScenariosBootsrap() {
/*
* the required data is loaded in another transaction because if it's
* loaded on the same transaction the added scenario could not be
* retrieved from PredefinedScenario. This happened when executing all
* tests. If you execute this test in isolation this problem doesn't
* happen
*/
transactionService.runOnAnotherTransaction(new IOnTransaction<Void>() {
@Override
public Void execute() {
scenariosBootstrap.loadRequiredData();
return null;
}
});
}
@Test
public void testLoadUserByUsername() {
usersBootstrap.loadRequiredData();
for (MandatoryUser u : MandatoryUser.values()) {