Integration du Core dans Acegi : authentification via EJB fonctionnelle. \n TODO: corriger les noms de roles (ROLE_AUTHOR ou AUTHOR, il faut decider), mettre en place le role ANONYMOUS quand aucune conference n'est selectionnee, et modifier jsp/secure/userinformation.jsp pour qu'il affiche quelque chose de dynamique ...
This commit is contained in:
@@ -68,12 +68,12 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schem
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="userDetailsService" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl">
|
||||
<property name="userProperties">
|
||||
<bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
|
||||
<property name="location" value="/WEB-INF/users.properties"/>
|
||||
</bean>
|
||||
</property>
|
||||
<bean id="userDetailsService" class="org.yacos.auth.UserDetailsService">
|
||||
<!-- <property name="userProperties">-->
|
||||
<!-- <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">-->
|
||||
<!-- <property name="location" value="/WEB-INF/users.properties"/>-->
|
||||
<!-- </bean>-->
|
||||
<!-- </property>-->
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -137,4 +137,9 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schem
|
||||
|
||||
<bean id="loggerListener" class="org.acegisecurity.event.authentication.LoggerListener"/>
|
||||
|
||||
|
||||
<!--<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">-->
|
||||
<!-- <property name="persistenceUnitName" value="YACOSCore"/>-->
|
||||
<!--</bean>-->
|
||||
|
||||
</beans>
|
||||
@@ -42,6 +42,11 @@
|
||||
<filter-name>lazyLoadingFilter</filter-name>
|
||||
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>lazyLoadingFilter</filter-name>
|
||||
<url-pattern>/onverracaplustard</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
|
||||
<!-- Acegi Security declaration Start -->
|
||||
<filter>
|
||||
@@ -88,7 +93,7 @@
|
||||
<filter>
|
||||
<filter-name>SessionService</filter-name>
|
||||
<filter-class>
|
||||
org.yacos.web.system.controller.SessionService
|
||||
org.yacos.web.system.session.SessionServiceFilter
|
||||
</filter-class>
|
||||
</filter>
|
||||
|
||||
|
||||
@@ -84,7 +84,6 @@
|
||||
<bean id="ListArticleController"
|
||||
class="org.yacos.web.PCmember.controller.ListArticleController">
|
||||
<property name="articleManager" ref="articleManager" />
|
||||
<property name="sessionService" ref="sessionService" />
|
||||
</bean>
|
||||
|
||||
<bean id="SUserRegistrationController"
|
||||
@@ -96,7 +95,6 @@
|
||||
<property name="formView" value="registerUser.htm" />
|
||||
<property name="successView" value="listArticle.htm" />
|
||||
<property name="userManager" ref="userManager" />
|
||||
<property name="sessionService" ref="sessionService" />
|
||||
</bean>
|
||||
|
||||
<bean id="SArticleController"
|
||||
@@ -121,7 +119,6 @@
|
||||
<property name="articleManager" ref="articleManager" />
|
||||
<property name="userManager" ref="userManager" />
|
||||
<property name="conferenceManager" ref="conferenceManager" />
|
||||
<property name="sessionService" ref="sessionService" />
|
||||
</bean>
|
||||
|
||||
<bean id="DispatchArticleController"
|
||||
@@ -135,7 +132,6 @@
|
||||
<property name="articleManager" ref="articleManager" />
|
||||
<property name="userManager" ref="userManager" />
|
||||
<property name="conferenceManager" ref="conferenceManager" />
|
||||
<!-- <property name="sessionService" ref="sessionService" /> -->
|
||||
</bean>
|
||||
|
||||
<bean id="AddConferenceController5"
|
||||
@@ -218,7 +214,6 @@
|
||||
<!-- Get chosen conference in session context -->
|
||||
<bean id="ChooseConferenceController"
|
||||
class="org.yacos.web.system.controller.ChooseConferenceController">
|
||||
<property name="sessionService" ref="sessionService" />
|
||||
</bean>
|
||||
|
||||
|
||||
@@ -278,7 +273,7 @@
|
||||
|
||||
|
||||
<bean id="sessionService"
|
||||
class="org.yacos.web.system.controller.SessionService">
|
||||
class="org.yacos.web.system.session.SessionService">
|
||||
<property name="conferenceManager" ref="conferenceManager" />
|
||||
<property name="userManager" ref="userManager" />
|
||||
</bean>
|
||||
@@ -290,4 +285,4 @@
|
||||
jndi-name="UserManagerBean/remote" cache="true" />
|
||||
<jee:jndi-lookup id="conferenceManager"
|
||||
jndi-name="ConferenceManagerBean/remote" cache="true" />
|
||||
</beans>
|
||||
</beans>
|
||||
|
||||
111
YACOSWeb/src/org/yacos/auth/UserDetails.java
Normal file
111
YACOSWeb/src/org/yacos/auth/UserDetails.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package org.yacos.auth;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
import org.yacos.core.users.Role;
|
||||
import org.yacos.core.users.User;
|
||||
import org.yacos.web.system.session.SessionService;
|
||||
|
||||
public class UserDetails implements org.acegisecurity.userdetails.UserDetails {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String login;
|
||||
/**
|
||||
* Password : used for authentication purpose only, is only writable
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* First Name of the user
|
||||
*/
|
||||
private String firstName;
|
||||
/**
|
||||
* Last name of the user
|
||||
*/
|
||||
private String lastName;
|
||||
/**
|
||||
* Organization or lab the user belongs to
|
||||
*/
|
||||
private String organization;
|
||||
/**
|
||||
* Active email (has to be used frequently) of the user
|
||||
*/
|
||||
private String email;
|
||||
|
||||
public UserDetails(User user) {
|
||||
this.login = user.getLogin();
|
||||
this.password = user.getPassword();
|
||||
this.firstName = user.getFirstName();
|
||||
this.lastName = user.getLastName();
|
||||
this.organization = user.getOrganization();
|
||||
this.email = user.getEmail();
|
||||
}
|
||||
|
||||
public GrantedAuthority[] getAuthorities() {
|
||||
Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
|
||||
|
||||
GrantedAuthority[] authorities = null;
|
||||
|
||||
if(currentConferenceId != null){
|
||||
List<Role> rolesList = SessionService.getInstance().getConferenceManager().getRoles(login, currentConferenceId);
|
||||
if(! rolesList.isEmpty()){
|
||||
authorities = new GrantedAuthority[rolesList.size()];
|
||||
}
|
||||
|
||||
for(int i=0;i<rolesList.size();i++){
|
||||
authorities[i] = new GrantedAuthorityImpl(rolesList.get(i).getType().name());
|
||||
}
|
||||
}
|
||||
|
||||
return (GrantedAuthority[]) authorities;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public boolean isAccountNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isAccountNonLocked() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isCredentialsNonExpired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public String getOrganization() {
|
||||
return organization;
|
||||
}
|
||||
|
||||
}
|
||||
39
YACOSWeb/src/org/yacos/auth/UserDetailsService.java
Normal file
39
YACOSWeb/src/org/yacos/auth/UserDetailsService.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.yacos.auth;
|
||||
|
||||
import org.acegisecurity.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DataRetrievalFailureException;
|
||||
import org.yacos.core.users.User;
|
||||
import org.yacos.web.system.session.SessionService;
|
||||
|
||||
/**
|
||||
* User details service
|
||||
*
|
||||
* @author christiancorsano
|
||||
*
|
||||
*/
|
||||
public class UserDetailsService implements
|
||||
org.acegisecurity.userdetails.UserDetailsService {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public UserDetails loadUserByUsername(String login)
|
||||
throws UsernameNotFoundException, DataAccessException {
|
||||
User user;
|
||||
try {
|
||||
user = SessionService.getInstance().getUserManager().getUser(login);
|
||||
} catch (Exception e){
|
||||
throw new DataRetrievalFailureException("Couldn't retrieve the User "+login,e);
|
||||
}
|
||||
|
||||
if(user == null){
|
||||
throw new UsernameNotFoundException(login);
|
||||
}
|
||||
|
||||
return new UserDetails(user);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,7 +17,7 @@ import org.yacos.core.users.IUserManager;
|
||||
import org.yacos.core.users.User;
|
||||
import org.yacos.web.PCmember.form.FormPreference;
|
||||
import org.yacos.web.system.controller.NoConferenceSelectedException;
|
||||
import org.yacos.web.system.controller.SessionService;
|
||||
import org.yacos.web.system.session.SessionService;
|
||||
|
||||
public class ChoosePreferenceController extends SimpleFormController {
|
||||
|
||||
@@ -27,13 +27,12 @@ public class ChoosePreferenceController extends SimpleFormController {
|
||||
private IArticleManager articleManager;
|
||||
private IUserManager userManager;
|
||||
private IConferenceManager conferenceManager;
|
||||
private SessionService sessionService;
|
||||
|
||||
|
||||
@Override
|
||||
protected Object formBackingObject(HttpServletRequest request)
|
||||
throws Exception {
|
||||
Integer currentConferenceId = sessionService.getCurrentConferenceId(request);
|
||||
Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
|
||||
|
||||
if(currentConferenceId == null){
|
||||
throw new NoConferenceSelectedException();
|
||||
@@ -112,12 +111,4 @@ public class ChoosePreferenceController extends SimpleFormController {
|
||||
public void setConferenceManager(IConferenceManager conferenceManager) {
|
||||
this.conferenceManager = conferenceManager;
|
||||
}
|
||||
|
||||
public SessionService getSessionService() {
|
||||
return sessionService;
|
||||
}
|
||||
|
||||
public void setSessionService(SessionService sessionService) {
|
||||
this.sessionService = sessionService;
|
||||
}
|
||||
}
|
||||
@@ -14,14 +14,13 @@ import org.yacos.core.article.IArticleManager;
|
||||
import org.yacos.core.exceptions.ConferenceDoesntExistException;
|
||||
import org.yacos.core.users.User;
|
||||
import org.yacos.web.PCmember.form.FormDispatcher;
|
||||
import org.yacos.web.system.controller.SessionService;
|
||||
import org.yacos.web.system.session.SessionService;
|
||||
|
||||
public class DispatchArticleController extends SimpleFormController{
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private IArticleManager articleManager;
|
||||
private SessionService sessionService;
|
||||
|
||||
public IArticleManager getArticleManager() {
|
||||
return articleManager;
|
||||
@@ -30,15 +29,6 @@ public class DispatchArticleController extends SimpleFormController{
|
||||
this.articleManager = articleManager;
|
||||
}
|
||||
|
||||
public SessionService getSessionService() {
|
||||
return sessionService;
|
||||
}
|
||||
|
||||
public void setSessionService(SessionService sessionService) {
|
||||
this.sessionService = sessionService;
|
||||
}
|
||||
|
||||
|
||||
protected Object formBackingObject(HttpServletRequest request) throws ConferenceDoesntExistException {
|
||||
logger.info("Returning dispatcher view");
|
||||
|
||||
@@ -49,7 +39,7 @@ public class DispatchArticleController extends SimpleFormController{
|
||||
members.add(m2);
|
||||
// TODO : use the formBackingObject to store these
|
||||
request.getSession().setAttribute("members",members);
|
||||
Integer currentConferenceId = sessionService.getCurrentConferenceId(request);
|
||||
Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
|
||||
// TODO : Fix that, we should use the form backing object instead of the session to store the articles
|
||||
request.getSession().setAttribute("articles",articleManager.getArticles(currentConferenceId));
|
||||
logger.info(articleManager.getArticles(currentConferenceId));
|
||||
@@ -71,7 +61,7 @@ public class DispatchArticleController extends SimpleFormController{
|
||||
|
||||
request.getSession().setAttribute("members",members);
|
||||
|
||||
Integer currentConferenceId = sessionService.getCurrentConferenceId(request);
|
||||
Integer currentConferenceId = SessionService.getInstance().getCurrentConferenceId();
|
||||
|
||||
return new ModelAndView("dispatchArticle", "articles", articleManager.getArticles(currentConferenceId) );
|
||||
}
|
||||
|
||||
@@ -102,13 +102,8 @@ public class EvaluationController extends SimpleFormController {
|
||||
if(toto == null){
|
||||
toto = userManager.addUser("toto", "toto", "Toto", "Toto", "Toto corp.", "toto@totocorp.com");
|
||||
}
|
||||
Report rpt =new Report();
|
||||
rpt.setArticle(article);
|
||||
rpt.setCommentAuthor(commentAuthor);
|
||||
rpt.setCommentPCMember(commentPCMember);
|
||||
rpt.setRatings(listeRating);
|
||||
rpt.setReferee(toto);
|
||||
|
||||
Report rpt =new Report(commentPCMember,commentAuthor,listeRating,toto,article);
|
||||
|
||||
request.getSession().setAttribute("report", rpt);
|
||||
//Map<String, Report> model1 = new HashMap<String, Report>();
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import org.yacos.web.system.controller.NoConferenceSelectedException;
|
||||
import org.yacos.web.system.controller.SessionService;
|
||||
import org.yacos.web.system.session.SessionService;
|
||||
import org.yacos.core.article.Article;
|
||||
import org.yacos.core.article.IArticleManager;
|
||||
import org.yacos.core.exceptions.ConferenceDoesntExistException;
|
||||
@@ -22,8 +22,6 @@ public class ListArticleController extends SimpleFormController {
|
||||
|
||||
private IArticleManager articleManager;
|
||||
|
||||
private SessionService sessionService;
|
||||
|
||||
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException, NoConferenceSelectedException {
|
||||
|
||||
@@ -31,7 +29,7 @@ public class ListArticleController extends SimpleFormController {
|
||||
|
||||
List<Article> listArticle;
|
||||
try {
|
||||
listArticle = articleManager.getArticles(sessionService.getCurrentConferenceId(request));
|
||||
listArticle = articleManager.getArticles(SessionService.getInstance().getCurrentConferenceId());
|
||||
getServletContext().setAttribute("listArticle", listArticle);
|
||||
} catch (ConferenceDoesntExistException e) {
|
||||
logger.error(e.getMessage());
|
||||
@@ -47,11 +45,4 @@ public class ListArticleController extends SimpleFormController {
|
||||
public void setArticleManager(IArticleManager articleManager) {
|
||||
this.articleManager = articleManager;
|
||||
}
|
||||
|
||||
public SessionService getSessionService() {
|
||||
return sessionService;
|
||||
}
|
||||
public void setSessionService(SessionService sessionService) {
|
||||
this.sessionService = sessionService;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.SimpleFormController;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
import org.yacos.web.system.controller.SessionService;
|
||||
import org.yacos.web.system.session.SessionService;
|
||||
import org.yacos.core.article.Article;
|
||||
import org.yacos.core.article.IArticleManager;
|
||||
import org.yacos.core.conferences.Conference;
|
||||
@@ -29,16 +29,15 @@ public class SArticleController extends SimpleFormController {
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private IArticleManager articleManager;
|
||||
private SessionService sessionService;
|
||||
|
||||
public ModelAndView onSubmit(HttpServletRequest request,
|
||||
HttpServletResponse response, Object command, BindException errors)
|
||||
throws ServletException, IllegalStateException {
|
||||
|
||||
// Fetching conference
|
||||
Conference conf = sessionService.getCurrentConference(request);
|
||||
Conference conf = SessionService.getInstance().getCurrentConference();
|
||||
// Fetching user
|
||||
User user = sessionService.getCurrentUser(request);
|
||||
User user = SessionService.getInstance().getCurrentUser();
|
||||
|
||||
String title = ((FormSubmission) command).getTitle();
|
||||
String theme = ((FormSubmission) command).getTheme();
|
||||
@@ -95,12 +94,4 @@ public class SArticleController extends SimpleFormController {
|
||||
public void setArticleManager(IArticleManager articleManager) {
|
||||
this.articleManager = articleManager;
|
||||
}
|
||||
|
||||
public SessionService getSessionService() {
|
||||
return sessionService;
|
||||
}
|
||||
|
||||
public void setSessionService(SessionService sessionService) {
|
||||
this.sessionService = sessionService;
|
||||
}
|
||||
}
|
||||
@@ -4,26 +4,18 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.mvc.Controller;
|
||||
import org.yacos.web.system.session.SessionService;
|
||||
|
||||
public class ChooseConferenceController implements Controller {
|
||||
private SessionService sessionService;
|
||||
|
||||
public SessionService getSessionService() {
|
||||
return sessionService;
|
||||
}
|
||||
|
||||
public void setSessionService(SessionService sessionService) {
|
||||
this.sessionService = sessionService;
|
||||
}
|
||||
|
||||
public ModelAndView handleRequest(HttpServletRequest request,
|
||||
HttpServletResponse response) throws Exception {
|
||||
|
||||
// get IDConf
|
||||
// get IDConf
|
||||
int idConf = Integer.parseInt(request.getParameter("idConf"));
|
||||
|
||||
// Set idConf in session context
|
||||
sessionService.setCurrentConferenceId(request, idConf);
|
||||
SessionService.getInstance().setCurrentConferenceId(idConf);
|
||||
|
||||
return new ModelAndView("main");
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.springframework.web.servlet.view.RedirectView;
|
||||
import org.yacos.core.users.IUserManager;
|
||||
import org.yacos.core.users.User;
|
||||
import org.yacos.web.system.form.FormUser;
|
||||
import org.yacos.web.system.session.SessionService;
|
||||
|
||||
/**
|
||||
* FormController class for the user Sign In scenario (user registration)
|
||||
@@ -24,17 +25,6 @@ import org.yacos.web.system.form.FormUser;
|
||||
*/
|
||||
public class SUserRegistrationController extends SimpleFormController {
|
||||
private IUserManager userManager;
|
||||
|
||||
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)
|
||||
*/
|
||||
@@ -45,7 +35,7 @@ public class SUserRegistrationController extends SimpleFormController {
|
||||
FormUser userCommand = (FormUser) command;
|
||||
|
||||
// Tries to retrieve the current user : case of user modification
|
||||
User user = sessionService.getCurrentUser(request);
|
||||
User user = SessionService.getInstance().getCurrentUser();
|
||||
|
||||
if( (userCommand.getIsNew()) && (userManager.getUser(userCommand.getLogin()) != null)){
|
||||
errors.rejectValue("login", "form.register.error.alreadyExists");
|
||||
@@ -87,9 +77,9 @@ public class SUserRegistrationController extends SimpleFormController {
|
||||
User currentUser = new User();
|
||||
user.setIsNew(true);
|
||||
// TODO : see how JAAS could change the way of getting the current User
|
||||
if(sessionService.isUserLogged(request)){
|
||||
if(SessionService.getInstance().isUserLogged()){
|
||||
// If he is logged, we use his profile information to fill the form
|
||||
currentUser = (User) sessionService.getCurrentUser(request);
|
||||
currentUser = (User) SessionService.getInstance().getCurrentUser();
|
||||
user.setIsNew(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
package org.yacos.web.system.controller;
|
||||
|
||||
import java.io.IOException;
|
||||
package org.yacos.web.system.session;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
import org.yacos.core.conferences.Conference;
|
||||
@@ -24,9 +16,11 @@ import org.yacos.core.users.User;
|
||||
* @author christiancorsano
|
||||
*
|
||||
*/
|
||||
public class SessionService implements Filter {
|
||||
private static ServletContext servletContext = null;
|
||||
private static HttpServletRequest request = null;
|
||||
public class SessionService {
|
||||
private static SessionService instance = new SessionService();
|
||||
|
||||
protected static ServletContext servletContext = null;
|
||||
protected static HttpServletRequest request = null;
|
||||
|
||||
private IConferenceManager conferenceManager;
|
||||
private IUserManager userManager;
|
||||
@@ -34,14 +28,21 @@ public class SessionService implements Filter {
|
||||
private User currentUser;
|
||||
private Conference currentConference;
|
||||
|
||||
public SessionService(){
|
||||
private SessionService(){
|
||||
}
|
||||
|
||||
public static SessionService getInstance(){
|
||||
if(instance == null){
|
||||
instance = new SessionService();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Init method to be called at filter time
|
||||
* Makes the sessionAttributes available in the request context
|
||||
*/
|
||||
private void init(){
|
||||
protected void init(){
|
||||
if(request != null && servletContext != null){
|
||||
try {
|
||||
if(userManager == null || conferenceManager == null){
|
||||
@@ -51,10 +52,10 @@ public class SessionService implements Filter {
|
||||
}
|
||||
// Put the current conference in the request context to make it accessible in JSPs
|
||||
// This avoid to put the whole object in the session
|
||||
request.setAttribute("currentConference", getCurrentConference(request));
|
||||
request.setAttribute("currentConference", getCurrentConference());
|
||||
// Put the current conference in the request context to make it accessible in JSPs
|
||||
// This avoid to put the whole object in the session
|
||||
request.setAttribute("currentUser", getCurrentUser(request));
|
||||
request.setAttribute("currentUser", getCurrentUser());
|
||||
} catch (NamingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@@ -85,7 +86,7 @@ public class SessionService implements Filter {
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @return The current conference id
|
||||
*/
|
||||
public Integer getCurrentConferenceId(HttpServletRequest request) {
|
||||
public Integer getCurrentConferenceId() {
|
||||
// Retrieve the ID from the session
|
||||
Integer confId = (Integer) WebUtils.getSessionAttribute(request, "currentConferenceId");
|
||||
return confId;
|
||||
@@ -96,7 +97,7 @@ public class SessionService implements Filter {
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @param currentConferenceId The id to set as the new current conference
|
||||
*/
|
||||
public void setCurrentConferenceId(HttpServletRequest request,Integer currentConferenceId) {
|
||||
public void setCurrentConferenceId(Integer currentConferenceId) {
|
||||
WebUtils.setSessionAttribute(request, "currentConferenceId", currentConferenceId);
|
||||
currentConference = conferenceManager.getConference(currentConferenceId);
|
||||
request.setAttribute("currentConference", currentConference);
|
||||
@@ -107,8 +108,8 @@ public class SessionService implements Filter {
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @param c The conference object to set as current
|
||||
*/
|
||||
public void setCurrentConference(HttpServletRequest request, Conference c){
|
||||
setCurrentConferenceId(request, c.getId() );
|
||||
public void setCurrentConference(Conference c){
|
||||
setCurrentConferenceId( c.getId() );
|
||||
currentConference = c;
|
||||
}
|
||||
|
||||
@@ -117,8 +118,8 @@ public class SessionService implements Filter {
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @return The conference object
|
||||
*/
|
||||
public Conference getCurrentConference(HttpServletRequest request){
|
||||
Integer id = getCurrentConferenceId(request);
|
||||
public Conference getCurrentConference(){
|
||||
Integer id = getCurrentConferenceId();
|
||||
|
||||
// The currentConference can be out to date : force the update
|
||||
if(currentConference != null && id != currentConference.getId()){
|
||||
@@ -136,9 +137,9 @@ public class SessionService implements Filter {
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @param currentUser The new current user to set
|
||||
*/
|
||||
public void setCurrentUser(HttpServletRequest request,User currentUser) {
|
||||
public void setCurrentUser(User currentUser) {
|
||||
this.currentUser = currentUser;
|
||||
this.setCurrentUserLogin(request,currentUser.getLogin());
|
||||
this.setCurrentUserLogin(currentUser.getLogin());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +147,7 @@ public class SessionService implements Filter {
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @param login The new current user login
|
||||
*/
|
||||
public void setCurrentUserLogin(HttpServletRequest request, String login) {
|
||||
public void setCurrentUserLogin(String login) {
|
||||
WebUtils.setSessionAttribute(request, "currentUserLogin", login);
|
||||
currentUser = userManager.getUser(login);
|
||||
request.setAttribute("currentUser", currentUser);
|
||||
@@ -159,7 +160,7 @@ public class SessionService implements Filter {
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @return The current user login
|
||||
*/
|
||||
public String getCurrentUserLogin(HttpServletRequest request){
|
||||
public String getCurrentUserLogin(){
|
||||
// Retrieve the login from the session
|
||||
String login = (String) WebUtils.getSessionAttribute(request, "currentUserLogin");
|
||||
return login;
|
||||
@@ -167,12 +168,11 @@ public class SessionService implements Filter {
|
||||
|
||||
/**
|
||||
* Get (if necessary) and returns the current user object
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @return The current User
|
||||
*/
|
||||
public User getCurrentUser(HttpServletRequest request) {
|
||||
public User getCurrentUser() {
|
||||
// Retrieve the login from the session
|
||||
String login = this.getCurrentUserLogin(request);
|
||||
String login = this.getCurrentUserLogin();
|
||||
|
||||
// The user can be out to date : force the update
|
||||
if(currentUser != null && (! currentUser.getLogin().equals(login)) ){
|
||||
@@ -180,7 +180,7 @@ public class SessionService implements Filter {
|
||||
}
|
||||
|
||||
// Check if the user object is already in cache
|
||||
if(currentUser == null || (this.getCurrentUser(request).getLogin().equals(login)) ){
|
||||
if(currentUser == null || (this.getCurrentUser().getLogin().equals(login)) ){
|
||||
// Checks login for validity
|
||||
if(login == null || login.equals("")){
|
||||
currentUser = null;
|
||||
@@ -194,42 +194,27 @@ public class SessionService implements Filter {
|
||||
|
||||
/**
|
||||
* Returns whether the user is logged (is there a current User for this session)
|
||||
* @param request The current HTTP request object, necessary for session operations
|
||||
* @return true if there's a current user in the session, false otherwise
|
||||
*/
|
||||
public boolean isUserLogged(HttpServletRequest request){
|
||||
return getCurrentUser(request) != null;
|
||||
}
|
||||
|
||||
/* ========================= */
|
||||
/* = Filter implementation = */
|
||||
/* ========================= */
|
||||
|
||||
/**
|
||||
* Inits the filter, and set the servletContext for this session service
|
||||
*/
|
||||
public void init(FilterConfig config) throws ServletException {
|
||||
SessionService.servletContext = config.getServletContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the current session data so that every JSP has access to it
|
||||
*/
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
if(request instanceof HttpServletRequest){
|
||||
SessionService.request = (HttpServletRequest) request;
|
||||
init();
|
||||
}
|
||||
|
||||
chain.doFilter(request, response);
|
||||
public boolean isUserLogged(){
|
||||
return getCurrentUser() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the static attributes
|
||||
* Puts an object in the session
|
||||
* @param name The name of the attribute in the session
|
||||
* @param attribute The attribute object
|
||||
*/
|
||||
public void destroy() {
|
||||
SessionService.servletContext = null;
|
||||
SessionService.request = null;
|
||||
public void setSessionAttribute(String name, Object attribute){
|
||||
WebUtils.setSessionAttribute(request, name, attribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves an object from the Session
|
||||
* @param name The name of the attribute to retrieve
|
||||
* @return The attribute object
|
||||
*/
|
||||
public Object getSessionAttribute(String name){
|
||||
return WebUtils.getSessionAttribute(request, name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.yacos.web.system.session;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Filter to initialize the SessionService singleton
|
||||
* @author christiancorsano
|
||||
*
|
||||
*/
|
||||
public class SessionServiceFilter implements Filter {
|
||||
/**
|
||||
* Inits the filter, and set the servletContext for this session service
|
||||
*/
|
||||
public void init(FilterConfig config) throws ServletException {
|
||||
SessionService.servletContext = config.getServletContext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the request object into SessionService and tells it to init
|
||||
*/
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
if(request instanceof HttpServletRequest){
|
||||
SessionService.request = (HttpServletRequest) request;
|
||||
SessionService.getInstance().init();
|
||||
}
|
||||
|
||||
// Continue the filter chain
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the static attributes
|
||||
*/
|
||||
public void destroy() {
|
||||
SessionService.servletContext = null;
|
||||
SessionService.request = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user