Corrigé UserRegister
Ajouté les fonctions correspondantes a SessionService
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<h2><fmt:message key="register.title" /></h2>
|
<h2><fmt:message key="register.title" /></h2>
|
||||||
<form:form method="post" commandName="registerUser">
|
<form:form method="post" commandName="registerUser">
|
||||||
|
<form:hidden path="isNew"/>
|
||||||
|
|
||||||
<form:label path="login"><fmt:message key="form.register.user.login" /></form:label> <form:input path="login"/> <br/><br />
|
<form:label path="login"><fmt:message key="form.register.user.login" /></form:label> <form:input path="login"/> <br/><br />
|
||||||
<form:label path="email"><fmt:message key="form.register.user.email" /></form:label> <form:input path="email"/> <br/><br />
|
<form:label path="email"><fmt:message key="form.register.user.email" /></form:label> <form:input path="email"/> <br/><br />
|
||||||
|
|||||||
@@ -92,9 +92,10 @@
|
|||||||
<property name="commandName" value="registerUser" />
|
<property name="commandName" value="registerUser" />
|
||||||
<property name="commandClass"
|
<property name="commandClass"
|
||||||
value="org.yacos.web.system.form.FormUser" />
|
value="org.yacos.web.system.form.FormUser" />
|
||||||
<property name="formView" value="registerUser" />
|
<property name="formView" value="registerUser.htm" />
|
||||||
<property name="successView" value="listArticle.htm" />
|
<property name="successView" value="listArticle.htm" />
|
||||||
<property name="usersManager" ref="usersManager" />
|
<property name="usersManager" ref="usersManager" />
|
||||||
|
<property name="sessionService" ref="sessionService"/>
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<bean id="SArticleController"
|
<bean id="SArticleController"
|
||||||
|
|||||||
@@ -23,8 +23,18 @@ import org.yacos.web.system.form.FormUser;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class SUserRegistrationController extends SimpleFormController {
|
public class SUserRegistrationController extends SimpleFormController {
|
||||||
|
|
||||||
private IUserManager usersManager;
|
private IUserManager usersManager;
|
||||||
|
|
||||||
|
private SessionService sessionService;
|
||||||
|
|
||||||
|
public SessionService getSessionService() {
|
||||||
|
return sessionService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSessionService(SessionService sessionService) {
|
||||||
|
this.sessionService = sessionService;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
|
* @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
|
||||||
*/
|
*/
|
||||||
@@ -37,35 +47,30 @@ public class SUserRegistrationController extends SimpleFormController {
|
|||||||
// Tries to retrieve the current user : case of user modification
|
// Tries to retrieve the current user : case of user modification
|
||||||
User user = (User) request.getSession().getAttribute("currentUser");
|
User user = (User) request.getSession().getAttribute("currentUser");
|
||||||
|
|
||||||
if((user == null) && (usersManager.getUser(userCommand.getLogin()) != null)){
|
if( (userCommand.getIsNew()) && (usersManager.getUser(userCommand.getLogin()) != null)){
|
||||||
errors.rejectValue("login", "form.register.error.alreadyExists");
|
errors.rejectValue("login", "form.register.error.alreadyExists");
|
||||||
return new ModelAndView(new RedirectView(getFormView()));
|
return new ModelAndView(new RedirectView(getFormView()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(user == null){
|
if( userCommand.getIsNew() ){
|
||||||
user = new User();
|
usersManager.addUser(
|
||||||
|
userCommand.getLogin(),
|
||||||
|
userCommand.getPassword(),
|
||||||
|
userCommand.getFirstName(),
|
||||||
|
userCommand.getLastName(),
|
||||||
|
userCommand.getOrganization(),
|
||||||
|
userCommand.getEmail());
|
||||||
|
} else {
|
||||||
|
user.setEmail(userCommand.getEmail());
|
||||||
|
user.setFirstName(userCommand.getFirstName());
|
||||||
|
user.setLastName(userCommand.getLastName());
|
||||||
|
user.setOrganization(userCommand.getOrganization());
|
||||||
}
|
}
|
||||||
|
|
||||||
// We should never be able to change the login of an existing user
|
|
||||||
if(user.getLogin() == null){
|
|
||||||
user.setLogin(userCommand.getLogin());
|
|
||||||
}
|
|
||||||
user.setEmail(userCommand.getEmail());
|
|
||||||
user.setFirstName(userCommand.getFirstName());
|
|
||||||
user.setLastName(userCommand.getLastName());
|
|
||||||
user.setOrganization(userCommand.getOrganization());
|
|
||||||
|
|
||||||
if(userCommand.passwordWasModified()){
|
if(userCommand.passwordWasModified()){
|
||||||
user.setPassword(userCommand.getPassword());
|
user.setPassword(userCommand.getPassword());
|
||||||
}
|
}
|
||||||
|
|
||||||
usersManager.addUser(
|
|
||||||
userCommand.getLogin(),
|
|
||||||
userCommand.getPassword(),
|
|
||||||
userCommand.getFirstName(),
|
|
||||||
userCommand.getLastName(),
|
|
||||||
userCommand.getOrganization(),
|
|
||||||
userCommand.getEmail());
|
|
||||||
|
|
||||||
return new ModelAndView(new RedirectView(getSuccessView()));
|
return new ModelAndView(new RedirectView(getSuccessView()));
|
||||||
}
|
}
|
||||||
@@ -80,10 +85,12 @@ public class SUserRegistrationController extends SimpleFormController {
|
|||||||
|
|
||||||
// We try to see if the user is already registered and logged
|
// We try to see if the user is already registered and logged
|
||||||
User currentUser = new User();
|
User currentUser = new User();
|
||||||
|
user.setIsNew(true);
|
||||||
// TODO : see how JAAS could change the way of getting the current User
|
// TODO : see how JAAS could change the way of getting the current User
|
||||||
if(request.getSession().getAttribute("currentUser") != null){
|
if(sessionService.isUserLogged(request)){
|
||||||
// If he is logged, we use his profile information to fill the form
|
// If he is logged, we use his profile information to fill the form
|
||||||
currentUser = (User) request.getSession().getAttribute("currentUser");
|
currentUser = (User) sessionService.getCurrentUser(request);
|
||||||
|
user.setIsNew(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
user.setLogin(currentUser.getLogin());
|
user.setLogin(currentUser.getLogin());
|
||||||
@@ -92,7 +99,7 @@ public class SUserRegistrationController extends SimpleFormController {
|
|||||||
user.setEmail(currentUser.getEmail());
|
user.setEmail(currentUser.getEmail());
|
||||||
user.setOrganization(currentUser.getOrganization());
|
user.setOrganization(currentUser.getOrganization());
|
||||||
|
|
||||||
return super.formBackingObject(request);
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUsersManager(IUserManager usersManager) {
|
public void setUsersManager(IUserManager usersManager) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.springframework.web.util.WebUtils;
|
|||||||
import org.yacos.core.conferences.Conference;
|
import org.yacos.core.conferences.Conference;
|
||||||
import org.yacos.core.conferences.IConferenceManager;
|
import org.yacos.core.conferences.IConferenceManager;
|
||||||
import org.yacos.core.users.IUserManager;
|
import org.yacos.core.users.IUserManager;
|
||||||
|
import org.yacos.core.users.User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides Session related Services
|
* Provides Session related Services
|
||||||
@@ -15,6 +16,9 @@ public class SessionService {
|
|||||||
private IConferenceManager conferenceManager;
|
private IConferenceManager conferenceManager;
|
||||||
private IUserManager userManager;
|
private IUserManager userManager;
|
||||||
|
|
||||||
|
private User currentUser;
|
||||||
|
private Conference currentConference;
|
||||||
|
|
||||||
public SessionService(){
|
public SessionService(){
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -44,10 +48,43 @@ public class SessionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setCurrentConference(HttpServletRequest request, Conference c){
|
public void setCurrentConference(HttpServletRequest request, Conference c){
|
||||||
|
currentConference = c;
|
||||||
setCurrentConferenceId(request, c.getId() );
|
setCurrentConferenceId(request, c.getId() );
|
||||||
}
|
}
|
||||||
|
|
||||||
public Conference getCurrentConference(HttpServletRequest request){
|
public Conference getCurrentConference(HttpServletRequest request){
|
||||||
return conferenceManager.getConference(getCurrentConferenceId(request));
|
if(currentConference == null){
|
||||||
|
currentConference = conferenceManager.getConference(getCurrentConferenceId(request));
|
||||||
|
}
|
||||||
|
return currentConference;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentUser(HttpServletRequest request,User currentUser) {
|
||||||
|
this.currentUser = currentUser;
|
||||||
|
this.setCurrentUserLogin(request,currentUser.getLogin());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentUserLogin(HttpServletRequest request, String login) {
|
||||||
|
WebUtils.setSessionAttribute(request, "currentUserLogin", login);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCurrentUserLogin(HttpServletRequest request){
|
||||||
|
return (String)WebUtils.getSessionAttribute(request, "currentUserLogin");
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getCurrentUser(HttpServletRequest request) {
|
||||||
|
if(currentUser == null){
|
||||||
|
String login = this.getCurrentUserLogin(request);
|
||||||
|
if(login == null || login.equals("")){
|
||||||
|
currentUser = null;
|
||||||
|
} else {
|
||||||
|
currentUser = userManager.getUser(login);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return currentUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isUserLogged(HttpServletRequest request){
|
||||||
|
return getCurrentUser(request) != null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ public class FormUser {
|
|||||||
private String organization;
|
private String organization;
|
||||||
|
|
||||||
private String passwordPlaceholderValue;
|
private String passwordPlaceholderValue;
|
||||||
|
|
||||||
|
private boolean isNew;
|
||||||
|
|
||||||
public FormUser() {
|
public FormUser() {
|
||||||
this.login = "";
|
this.login = "";
|
||||||
@@ -125,5 +127,12 @@ public class FormUser {
|
|||||||
return this.password.equals(this.passwordConfirm);
|
return this.password.equals(this.passwordConfirm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setIsNew(boolean isNew) {
|
||||||
|
this.isNew = isNew;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getIsNew() {
|
||||||
|
return isNew;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user