Index: lams_central/src/java/org/lamsfoundation/lams/web/ShibLearnerServlet.java =================================================================== RCS file: /usr/local/cvsroot/lams_central/src/java/org/lamsfoundation/lams/web/Attic/ShibLearnerServlet.java,v diff -u -r1.1.2.1 -r1.1.2.2 --- lams_central/src/java/org/lamsfoundation/lams/web/ShibLearnerServlet.java 17 Jul 2007 03:02:03 -0000 1.1.2.1 +++ lams_central/src/java/org/lamsfoundation/lams/web/ShibLearnerServlet.java 17 Jul 2007 05:36:22 -0000 1.1.2.2 @@ -9,20 +9,17 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; +import org.lamsfoundation.lams.federation.service.IFederationService; import org.lamsfoundation.lams.lesson.Lesson; import org.lamsfoundation.lams.lesson.LessonClass; import org.lamsfoundation.lams.lesson.service.ILessonService; import org.lamsfoundation.lams.usermanagement.Role; import org.lamsfoundation.lams.usermanagement.User; -import org.lamsfoundation.lams.usermanagement.dto.UserDTO; import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; import org.lamsfoundation.lams.util.Configuration; import org.lamsfoundation.lams.util.ConfigurationKeys; -import org.lamsfoundation.lams.web.session.SessionManager; -import org.lamsfoundation.lams.web.util.AttributeNames; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; @@ -37,6 +34,7 @@ private static Logger log = Logger.getLogger(ShibLearnerServlet.class); private static IUserManagementService service; private static ILessonService lessonService; + private static IFederationService fedService; private ILessonService getLessonService(){ if(lessonService==null){ @@ -54,15 +52,14 @@ return service; } - private UserDTO getUserDTO() { - HttpSession ss = SessionManager.getSession(); - return (UserDTO) ss.getAttribute(AttributeNames.USER); + private IFederationService getFedService(){ + if(fedService==null){ + WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); + fedService = (IFederationService) ctx.getBean("federationService"); + } + return fedService; } - private User getUser(UserDTO dto) { - return getService().getUserByLogin(dto.getLogin()); - } - public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -71,12 +68,20 @@ // add this shib user to lesson if they have learner role and are not already a member String roles = request.getHeader(Configuration.get(ConfigurationKeys.SHIB_ATTR_ROLES)); + String username = request.getHeader(Configuration.get(ConfigurationKeys.SHIB_ATTR_LOGIN)); + String identityProvider = request.getHeader("Shib-Identity-Provider"); + User user = null; if (roles != null && roles.indexOf(Role.LEARNER)>0) { - User user = getUser(getUserDTO()); + try { + String localUsername = getFedService().getLocalUsername(username, identityProvider); + user = getService().getUserByLogin(localUsername); + } catch (Exception e) { + log.error("Couldn't get local user from shib username "+username); + } Lesson lesson = lessonId != null ? getLessonService().getLesson(lessonId) : null; if (lesson != null) { LessonClass lessonClass = lesson.getLessonClass(); - if (lessonClass != null && !lesson.getLessonClass().getLearners().contains(user)) { + if (user != null && lessonClass != null && !lesson.getLessonClass().getLearners().contains(user)) { lessonClass.addLearner(user); log.info("Added shib user "+user.getLogin()+" to lesson with id "+lessonIdStr); } Index: lams_central/src/java/org/lamsfoundation/lams/web/ShibLoginServlet.java =================================================================== RCS file: /usr/local/cvsroot/lams_central/src/java/org/lamsfoundation/lams/web/Attic/ShibLoginServlet.java,v diff -u -r1.1.2.8 -r1.1.2.9 --- lams_central/src/java/org/lamsfoundation/lams/web/ShibLoginServlet.java 17 Jul 2007 03:02:15 -0000 1.1.2.8 +++ lams_central/src/java/org/lamsfoundation/lams/web/ShibLoginServlet.java 17 Jul 2007 05:36:22 -0000 1.1.2.9 @@ -85,7 +85,7 @@ // prefix new usernames with their origin server's fedId so as not to mix up with local usernames String prefixedUsername = null; try { - prefixedUsername = getUsername(username, identityProvider); + prefixedUsername = fedService.getLocalUsername(username, identityProvider); } catch (FederationException e) { flagError(request, response); } @@ -143,41 +143,5 @@ request.getSession().setAttribute("shibLoginError", "true"); response.sendRedirect("/lams/"); } - - private String getHost(String url) { - if (url == null) { - log.error("Couldn't get host from url."); - return url; - } else { - int doubleSlash = url.indexOf("//"); - if (doubleSlash > 0) { - url = url.substring(doubleSlash+2); - } - int firstSlash = url.indexOf('/'); - return (firstSlash > 0 ? url.substring(0, firstSlash) : url); - } - } - - // produces a local version of shibboleth user's username; it is prefixed by the fedId of their origin server - private String getUsername(String username, String providerId) throws FederationException { - String host = getHost(providerId); - // check if user is from local server - if (host.equals(getHost(Configuration.get(ConfigurationKeys.SERVER_URL)))) { - log.info("Shibboleth user "+username+" appears to be a local user."); - return username; - } - try { - FederationServer fedServer = fedService.getFedServerByURLHost(host); - if (fedServer != null) { - log.info("Shibboleth user "+username+" appears to come from fedServer with fedId "+fedServer.getFedId()); - return fedServer.getFedId()+"_"+username; - } - } catch (Exception e) { - log.error("Couldn't create prefixed username: "+e); - } - // return nothing rather than original username so there is no chance of accidental login - // to another person's local account - return null; - } } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/federation/service/FederationService.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/federation/service/Attic/FederationService.java,v diff -u -r1.1.2.9 -r1.1.2.10 --- lams_common/src/java/org/lamsfoundation/lams/federation/service/FederationService.java 12 Jul 2007 06:24:43 -0000 1.1.2.9 +++ lams_common/src/java/org/lamsfoundation/lams/federation/service/FederationService.java 17 Jul 2007 05:36:22 -0000 1.1.2.10 @@ -360,4 +360,40 @@ throw new FederationException("Found more than 1 FederationServer with the hostname: "+host); } } + + private String getHost(String url) { + if (url == null) { + log.error("Couldn't get host from url."); + return url; + } else { + int doubleSlash = url.indexOf("//"); + if (doubleSlash > 0) { + url = url.substring(doubleSlash+2); + } + int firstSlash = url.indexOf('/'); + return (firstSlash > 0 ? url.substring(0, firstSlash) : url); + } + } + + // produces a local version of shibboleth user's username; it is prefixed by the fedId of their origin server + public String getLocalUsername(String username, String providerId) throws FederationException { + String host = getHost(providerId); + // check if user is from local server + if (host.equals(getHost(Configuration.get(ConfigurationKeys.SERVER_URL)))) { + log.info("Shibboleth user "+username+" appears to be a local user."); + return username; + } + try { + FederationServer fedServer = getFedServerByURLHost(host); + if (fedServer != null) { + log.info("Shibboleth user "+username+" appears to come from fedServer with fedId "+fedServer.getFedId()); + return fedServer.getFedId()+"_"+username; + } + } catch (Exception e) { + log.error("Couldn't create prefixed username: "+e); + } + // return nothing rather than original username so there is no chance of accidental login + // to another person's local account + return null; + } } Index: lams_common/src/java/org/lamsfoundation/lams/federation/service/IFederationService.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/federation/service/Attic/IFederationService.java,v diff -u -r1.1.2.7 -r1.1.2.8 --- lams_common/src/java/org/lamsfoundation/lams/federation/service/IFederationService.java 12 Jul 2007 02:53:11 -0000 1.1.2.7 +++ lams_common/src/java/org/lamsfoundation/lams/federation/service/IFederationService.java 17 Jul 2007 05:36:22 -0000 1.1.2.8 @@ -112,4 +112,13 @@ * @throws FederationException */ public FederationServer getFedServerByURLHost(String host) throws FederationException; + + /** + * Returns username prefixed with fedId of shib user's origin server. + * @param username + * @param providerId + * @return + * @throws FederationException + */ + public String getLocalUsername(String username, String providerId) throws FederationException; }