Index: lams_central/build.xml
===================================================================
diff -u -r9890a029ab3270cf7750dec1120a0d71e6f4f86f -rca68b26281fd59bce02a7c78f8ff9c3db534d2ae
--- lams_central/build.xml (.../build.xml) (revision 9890a029ab3270cf7750dec1120a0d71e6f4f86f)
+++ lams_central/build.xml (.../build.xml) (revision ca68b26281fd59bce02a7c78f8ff9c3db534d2ae)
@@ -140,6 +140,7 @@
+ * @option password-stacking: If this is set to "useFirstPass", the login
+ * identity will be taken from the javax.security.auth.login.name
+ * value of the sharedState map, and the proof of identity from the
+ * javax.security.auth.login.password
value of the sharedState
+ * map.
+ * @option principalClass: A Principal implementation that support a ctor
+ * taking a String argument for the princpal name.
+ * @option unauthenticatedIdentity: the name of the principal to asssign
+ * and authenticate when a null username and password are seen.
+ *
+ * @param subject the Subject to update after a successful login.
+ * @param callbackHandler the CallbackHandler that will be used to obtain the
+ * the user identity and credentials.
+ * @param sharedState a Map shared between all configured login module instances
+ * @param options the parameters passed to the login module.
+ */
+ public void initialize(Subject subject, CallbackHandler callbackHandler,
+ Map sharedState, Map options)
+ {
+ this.subject = subject;
+ this.callbackHandler = callbackHandler;
+ this.sharedState = sharedState;
+ this.options = options;
+ log = Logger.getLogger(getClass());
+ log.info("initialize");
+ /* Check for password sharing options. Any non-null value for
+ password_stacking sets useFirstPass as this module has no way to
+ validate any shared password.
+ */
+ String passwordStacking = (String) options.get("password-stacking");
+ if( passwordStacking != null && passwordStacking.equalsIgnoreCase("useFirstPass") )
+ useFirstPass = true;
+
+ // Check for a custom Principal implementation
+ principalClassName = (String) options.get("principalClass");
+
+ // Check for unauthenticatedIdentity option.
+ String name = (String) options.get("unauthenticatedIdentity");
+ if( name != null )
+ {
+ try
+ {
+ unauthenticatedIdentity = createIdentity(name);
+ log.info("Saw unauthenticatedIdentity="+name);
+ }
+ catch(Exception e)
+ {
+ log.warn("Failed to create custom unauthenticatedIdentity", e);
+ }
+ }
+ }
+
+ /** Looks for javax.security.auth.login.name and javax.security.auth.login.password
+ values in the sharedState map if the useFirstPass option was true and returns
+ true if they exist. If they do not or are null this method returns false.
+
+ Note that subclasses that override the login method must set the loginOk
+ ivar to true if the login succeeds in order for the commit phase to
+ populate the Subject. This implementation sets loginOk to true if the
+ login() method returns true, otherwise, it sets loginOk to false.
+ */
+ public boolean login() throws LoginException
+ {
+ log.info("login");
+ loginOk = false;
+ // If useFirstPass is true, look for the shared password
+ if( useFirstPass == true )
+ {
+ try
+ {
+ Object identity = sharedState.get("javax.security.auth.login.name");
+ Object credential = sharedState.get("javax.security.auth.login.password");
+ if( identity != null && credential != null )
+ {
+ loginOk = true;
+ return true;
+ }
+ // Else, fall through and perform the login
+ }
+ catch(Exception e)
+ { // Dump the exception and continue
+ log.error("login failed", e);
+ }
+ }
+ return false;
+ }
+
+ /** Method to commit the authentication process (phase 2). If the login
+ method completed successfully as indicated by loginOk == true, this
+ method adds the getIdentity() value to the subject getPrincipals() Set.
+ It also adds the members of each Group returned by getRoleSets()
+ to the subject getPrincipals() Set.
+
+ @see javax.security.auth.Subject;
+ @see java.security.acl.Group;
+ @return true always.
+ */
+ public boolean commit() throws LoginException
+ {
+ log.info("commit, loginOk="+loginOk);
+ if( loginOk == false )
+ return false;
+
+ Set principals = subject.getPrincipals();
+ Principal identity = getIdentity();
+ principals.add(identity);
+ Group[] roleSets = getRoleSets();
+ for(int g = 0; g < roleSets.length; g ++)
+ {
+ Group group = roleSets[g];
+ String name = group.getName();
+ Group subjectGroup = createGroup(name, principals);
+ if( subjectGroup instanceof NestableGroup )
+ {
+ /* A NestableGroup only allows Groups to be added to it so we
+ need to add a SimpleGroup to subjectRoles to contain the roles
+ */
+ SimpleGroup tmp = new SimpleGroup("Roles");
+ subjectGroup.addMember(tmp);
+ subjectGroup = tmp;
+ }
+ // Copy the group members to the Subject group
+ Enumeration members = group.members();
+ while( members.hasMoreElements() )
+ {
+ Principal role = (Principal) members.nextElement();
+ subjectGroup.addMember(role);
+ }
+ }
+ return true;
+ }
+
+ /** Method to abort the authentication process (phase 2).
+ @return true alaways
+ */
+ public boolean abort() throws LoginException
+ {
+ log.info("abort");
+ return true;
+ }
+
+ /** Remove the user identity and roles added to the Subject during commit.
+ @return true always.
+ */
+ public boolean logout() throws LoginException
+ {
+ log.info("logout");
+ // Remove the user identity
+ Principal identity = getIdentity();
+ Set principals = subject.getPrincipals();
+ principals.remove(identity);
+ // Remove any added Groups...
+ return true;
+ }
+ //--- End LoginModule interface methods
+
+ // --- Protected methods
+
+ /** Overriden by subclasses to return the Principal that corresponds to
+ the user primary identity.
+ */
+ protected Principal getIdentity()
+ {
+ return null;
+ }
+ /** Overriden by subclasses to return the Groups that correspond to the
+ to the role sets assigned to the user. Subclasses should create at
+ least a Group named "Roles" that contains the roles assigned to the user.
+ A second common group is "CallerPrincipal" that provides the application
+ identity of the user rather than the security domain identity.
+ @return Group[] containing the sets of roles
+ */
+ protected Group[] getRoleSets() throws LoginException
+ {
+ return null;
+ }
+
+ protected boolean getUseFirstPass()
+ {
+ return useFirstPass;
+ }
+ protected Principal getUnauthenticatedIdentity()
+ {
+ return unauthenticatedIdentity;
+ }
+
+ /** Find or create a Group with the given name. Subclasses should use this
+ method to locate the 'Roles' group or create additional types of groups.
+ @return A named Group from the principals set.
+ */
+ protected Group createGroup(String name, Set principals)
+ {
+ Group roles = null;
+ Iterator iter = principals.iterator();
+ while( iter.hasNext() )
+ {
+ Object next = iter.next();
+ if( (next instanceof Group) == false )
+ continue;
+ Group grp = (Group) next;
+ if( grp.getName().equals(name) )
+ {
+ roles = grp;
+ break;
+ }
+ }
+ // If we did not find a group create one
+ if( roles == null )
+ {
+ roles = new SimpleGroup(name);
+ principals.add(roles);
+ }
+ return roles;
+ }
+
+ /** Utility method to create a Principal for the given username. This
+ * creates an instance of the principalClassName type if this option was
+ * specified using the class constructor matching: ctor(String). If
+ * principalClassName was not specified, a SimplePrincipal is created.
+ *
+ * @param username the name of the principal
+ * @return the principal instance
+ * @throws java.lang.Exception thrown if the custom principal type cannot be created.
+ */
+ protected Principal createIdentity(String username)
+ throws Exception
+ {
+ Principal p = null;
+ if( principalClassName == null )
+ {
+ p = new SimplePrincipal(username);
+ }
+ else
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Class clazz = loader.loadClass(principalClassName);
+ Class[] ctorSig = {String.class};
+ Constructor ctor = clazz.getConstructor(ctorSig);
+ Object[] ctorArgs = {username};
+ p = (Principal) ctor.newInstance(ctorArgs);
+ }
+ return p;
+ }
+}
\ No newline at end of file
Index: lams_central/src/java/org/lamsfoundation/lams/security/AuthenticationMethodConfigurer.java
===================================================================
diff -u
--- lams_central/src/java/org/lamsfoundation/lams/security/AuthenticationMethodConfigurer.java (revision 0)
+++ lams_central/src/java/org/lamsfoundation/lams/security/AuthenticationMethodConfigurer.java (revision ca68b26281fd59bce02a7c78f8ff9c3db534d2ae)
@@ -0,0 +1,129 @@
+/****************************************************************
+ * Copyright (C) 2005 LAMS Foundation (http://lamsfoundation.org)
+ * =============================================================
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ *
+ * http://www.gnu.org/licenses/gpl.txt
+ * ****************************************************************
+ */
+package org.lamsfoundation.lams.security;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+import org.lamsfoundation.lams.usermanagement.AuthenticationMethodParameter;
+import org.lamsfoundation.lams.usermanagement.AuthenticationMethod;
+import org.lamsfoundation.lams.util.XmlFileLoader;
+
+/**
+ *
+ * View Source + *
+ * + * @author Fei Yang + */ +public class AuthenticationMethodConfigurer { + + private static Document authConfigureDoc = null; + + private static String configFilePath; + + /** + * @return Returns the configFilePath. + */ + public static String getConfigFilePath() { + return configFilePath; + } + /** + * @param configFilePath The configFilePath to set. + */ + public static void setConfigFilePath(String configFilePath) { + AuthenticationMethodConfigurer.configFilePath = configFilePath; + } + + private static void loadConfiguration() + throws IOException,SAXException,SAXParseException,ParserConfigurationException{ + + if(authConfigureDoc==null){ + authConfigureDoc = XmlFileLoader.getDocumentFromFilePath(configFilePath); + } + } + + private static Element findMethodElement(String methodName) + throws IOException,SAXException,SAXParseException,ParserConfigurationException + { + NodeList nodeList = authConfigureDoc.getElementsByTagName("Method"); + for(int i=0; ilogin()
+ * prior to password validation.
+ * + * Subclasses may override it to provide customized password hashing, + * for example by adding user-specific information or salting. + *
+ * The default version calculates the hash based on the following options: + *
validatePassword()
to fail.
+ *
+ * @param username ignored in default version
+ * @param password the password string to be hashed
+ */
+ protected String createPasswordHash(String username, String password)
+ {
+ String passwordHash = createPasswordHash(hashAlgorithm, hashEncoding,
+ hashCharset, username, password);
+ return passwordHash;
+ }
+
+ /** A hook that allows subclasses to change the validation of the input
+ password against the expected password. This version checks that
+ neither inputPassword or expectedPassword are null that that
+ inputPassword.equals(expectedPassword) is true;
+ @return true if the inputPassword is valid, false otherwise.
+ */
+ protected boolean validatePassword(String inputPassword, String expectedPassword)
+ {
+ if( inputPassword == null || expectedPassword == null )
+ return false;
+ boolean valid = false;
+ if( ignorePasswordCase == true )
+ valid = inputPassword.equalsIgnoreCase(expectedPassword);
+ else
+ valid = inputPassword.equals(expectedPassword);
+ return valid;
+ }
+
+
+ /** Get the expected password for the current username available via
+ the getUsername() method. This is called from within the login()
+ method after the CallbackHandler has returned the username and
+ candidate password.
+ @return the valid password String
+ */
+ abstract protected String getUsersPassword() throws LoginException;
+
+ public static String createPasswordHash(String hashAlgorithm, String hashEncoding,
+ String hashCharset, String username, String password)
+ {
+ //currently no implementation for password hashing.
+ return password;
+ /*
+ byte[] passBytes;
+ String passwordHash = null;
+
+ // convert password to byte data
+ try
+ {
+ if(hashCharset == null)
+ passBytes = password.getBytes();
+ else
+ passBytes = password.getBytes(hashCharset);
+ }
+ catch(Exception uee)
+ {
+ Logger log = Logger.getLogger(UsernamePasswordLoginModule.class);
+ log.error("charset " + hashCharset + " not found. Using platform default.", uee);
+ passBytes = password.getBytes();
+ }
+
+ // calculate the hash and apply the encoding.
+ try
+ {
+ byte[] hash = MessageDigest.getInstance(hashAlgorithm).digest(passBytes);
+ if(hashEncoding.equalsIgnoreCase("BASE64"))
+ {
+ passwordHash = Util.encodeBase64(hash);
+ }
+ else if(hashEncoding.equalsIgnoreCase("HEX"))
+ {
+ passwordHash = Util.encodeBase16(hash);
+ }
+ else
+ {
+ Logger log = Logger.getLogger(UsernamePasswordLoginModule.class);
+ log.error("Unsupported hash encoding format " + hashEncoding);
+ }
+ }
+ catch(Exception e)
+ {
+ Logger log = Logger.getLogger(UsernamePasswordLoginModule.class);
+ log.error("Password hash calculation failed ", e);
+ }
+ return passwordHash;
+ */
+
+ }
+
+}
Index: lams_central/src/java/org/lamsfoundation/lams/security/WebAuthAuthenticator.java
===================================================================
diff -u
--- lams_central/src/java/org/lamsfoundation/lams/security/WebAuthAuthenticator.java (revision 0)
+++ lams_central/src/java/org/lamsfoundation/lams/security/WebAuthAuthenticator.java (revision ca68b26281fd59bce02a7c78f8ff9c3db534d2ae)
@@ -0,0 +1,25 @@
+/*
+ * Created on 2004-12-15
+ *
+ * To change the template for this generated file go to
+ * Window>Preferences>Java>Code Generation>Code and Comments
+ */
+package org.lamsfoundation.lams.security;
+
+/**
+ * @author kevin
+ *
+ */
+public class WebAuthAuthenticator
+{
+ public WebAuthAuthenticator()
+ {
+ }
+
+ public boolean authenticate(String username, String inputPassword)
+ {
+ //as for now, alway return true for webauth authenticated users
+ //TODO check session to see if the user is from webauth
+ return true;
+ }
+}
Index: lams_central/src/java/org/lamsfoundation/lams/security/WebAuthServlet.java
===================================================================
diff -u
--- lams_central/src/java/org/lamsfoundation/lams/security/WebAuthServlet.java (revision 0)
+++ lams_central/src/java/org/lamsfoundation/lams/security/WebAuthServlet.java (revision ca68b26281fd59bce02a7c78f8ff9c3db534d2ae)
@@ -0,0 +1,63 @@
+package org.lamsfoundation.lams.security;
+
+import java.io.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+import org.springframework.web.context.support.WebApplicationContextUtils;
+import org.springframework.web.context.WebApplicationContext;
+import org.lamsfoundation.lams.usermanagement.service.UserManagementService;
+import org.lamsfoundation.lams.usermanagement.*;
+
+/**
+ * @author kevin
+ *
+ */
+public class WebAuthServlet extends HttpServlet {
+
+ private static final String WEBAUTH_TOKEN = "WEBAUTH_USER";
+
+ public void init() throws ServletException
+ {
+ }
+
+ public void service(HttpServletRequest request, HttpServletResponse response)
+ throws IOException, ServletException
+ {
+ HttpSession oldSession = request.getSession(false);
+ if ( oldSession != null )
+ {
+ oldSession.invalidate();
+ }
+
+ String webAuthUserID = (String)request.getAttribute(WEBAUTH_TOKEN);
+ HttpSession session = request.getSession(true);
+ if ( webAuthUserID == null )
+ //there is no valid WebAuth authenticated user
+ {
+ session.removeAttribute(WEBAUTH_TOKEN);
+ }
+ else
+ {
+ WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
+ UserManagementService service = (UserManagementService)ctx.getBean("userManagementServiceTarget");
+ User webAuthUser = service.getUserByLogin(webAuthUserID);
+
+ if (webAuthUser != null)
+ //valid webauth user also is registered in Lams as well
+ {
+ session.setAttribute(WEBAUTH_TOKEN, webAuthUserID);
+ }
+ else
+ //though webauth authenticated, but not registered.
+ {
+ session.removeAttribute(WEBAUTH_TOKEN);
+ }
+ }
+
+ //In anycase, goto Lams welcome page
+ response.sendRedirect("index.jsp");
+
+ }
+
+}
Index: lams_central/src/java/org/lamsfoundation/lams/web/HomeAction.java
===================================================================
diff -u
--- lams_central/src/java/org/lamsfoundation/lams/web/HomeAction.java (revision 0)
+++ lams_central/src/java/org/lamsfoundation/lams/web/HomeAction.java (revision ca68b26281fd59bce02a7c78f8ff9c3db534d2ae)
@@ -0,0 +1,222 @@
+package org.lamsfoundation.lams.web;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.log4j.Logger;
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionForward;
+import org.apache.struts.action.ActionMapping;
+import org.apache.struts.actions.DispatchAction;
+
+import org.springframework.web.context.support.WebApplicationContextUtils;
+import org.springframework.web.context.WebApplicationContext;
+
+import org.lamsfoundation.lams.usermanagement.service.UserManagementService;
+import org.lamsfoundation.lams.web.PasswordChangeActionForm;
+import org.lamsfoundation.lams.usermanagement.Role;
+import org.lamsfoundation.lams.usermanagement.Organisation;
+import org.lamsfoundation.lams.usermanagement.util.AdminPreparer;
+
+import org.lamsfoundation.lams.util.Configuration;
+import org.lamsfoundation.lams.util.ConfigurationKeys;
+
+
+/**
+ * this is an action where all lams client environments launch.
+ * initial configuration of the individual environment setting is done here.
+ *
+ * @struts:action path="/home"
+ * validate="false"
+ * parameter="method"
+ * @struts:action-forward name="admin" path=".admin"
+ * @struts:action-forward name="learner" path="/learner.jsp"
+ * @struts:action-forward name="author" path="/author.jsp"
+ * @struts:action-forward name="staff" path="/staff.jsp"
+ * @struts:action-forward name="error" path=".error"
+ * @struts:action-forward name="passwordChange" path=".passwordChange"
+ *
+ */
+public class HomeAction extends DispatchAction {
+
+ private static Logger log = Logger.getLogger(HomeAction.class);
+ private static WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(HttpSessionManager.getInstance().getServletContext());
+ private static UserManagementService service = (UserManagementService) ctx.getBean("userManagementServiceTarget");
+
+
+ private boolean isUserInRole(String login,int orgId, String roleName)
+ {
+ if (service.getUserOrganisationRole(login, new Integer(orgId),roleName)==null)
+ return false;
+ return true;
+ }
+
+ /**
+ * request for sysadmin environment
+ */
+ public ActionForward admin(ActionMapping mapping, ActionForm form,
+ HttpServletRequest req, HttpServletResponse res)
+ throws IOException, ServletException {
+
+ try {
+ log.debug("request admin");
+
+ String login = req.getRemoteUser();
+
+ int orgId = new Integer(req.getParameter("orgId")).intValue();
+
+ if ( isUserInRole(login,orgId,Role.ADMIN))
+ {
+ log.debug("user is admin");
+ Organisation org = service.getOrganisationById(new Integer(orgId));
+ AdminPreparer.prepare(org,req,service);
+ return mapping.findForward("admin");
+ }
+ else
+ {
+ log.error("User "+login+" tried to get admin screen but isn't admin in organisation: "+orgId);
+ return mapping.findForward("error");
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ return mapping.findForward("error");
+ }
+ }
+
+ /**
+ * request for learner environment
+ */
+ public ActionForward learner(ActionMapping mapping, ActionForm form,
+ HttpServletRequest req, HttpServletResponse res)
+ throws IOException, ServletException {
+
+ try {
+ log.debug("request learner");
+
+ String login = req.getRemoteUser();
+
+ int orgId = new Integer(req.getParameter("orgId")).intValue();
+
+ if ( isUserInRole(login,orgId,Role.LEARNER) )
+ {
+ log.debug("user is learner");
+
+ String serverUrl = Configuration.get(ConfigurationKeys.SERVER_URL);
+
+ req.setAttribute("serverUrl", serverUrl);
+ return mapping.findForward("learner");
+ }
+ else
+ {
+ log.error("User "+login+" tried to get learner screen but isn't learner in organisation: "+orgId);
+ return mapping.findForward("error");
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ return mapping.findForward("error");
+ }
+ }
+
+
+ /**
+ * request for author environment
+ */
+ public ActionForward author(ActionMapping mapping, ActionForm form,
+ HttpServletRequest req, HttpServletResponse res)
+ throws IOException, ServletException {
+
+ try {
+ log.debug("request author");
+
+ String login = req.getRemoteUser();
+
+ int orgId = new Integer(req.getParameter("orgId")).intValue();
+
+ if ( isUserInRole(login,orgId,Role.AUTHOR) )
+ {
+ log.debug("user is author");
+
+ String serverUrl = Configuration.get(ConfigurationKeys.SERVER_URL);
+
+ req.setAttribute("serverUrl", serverUrl);
+ return mapping.findForward("author");
+ }
+ else
+ {
+ log.error("User "+login+" tried to get author screen but isn't author in organisation: "+orgId);
+ return mapping.findForward("error");
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ return mapping.findForward("error");
+ }
+ }
+
+
+ /**
+ * request for staff environment
+ */
+ public ActionForward staff(ActionMapping mapping, ActionForm form,
+ HttpServletRequest req, HttpServletResponse res)
+ throws IOException, ServletException {
+
+ try {
+ log.debug("request staff");
+
+ String login = req.getRemoteUser();
+
+ int orgId = new Integer(req.getParameter("orgId")).intValue();
+
+ if (isUserInRole(login, orgId, Role.STAFF)) {
+ log.debug("user is staff");
+
+ String serverUrl = Configuration
+ .get(ConfigurationKeys.SERVER_URL);
+
+ req.setAttribute("serverUrl", serverUrl);
+ return mapping.findForward("staff");
+ } else {
+ log
+ .error("User "
+ + login
+ + " tried to get staff screen but isn't staff in organisation: "
+ + orgId);
+ return mapping.findForward("error");
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ return mapping.findForward("error");
+ }
+ }
+
+ /**
+ * Loads up the user password change form
+ * @return screen reference name - "passwordChange"
+ */
+ public ActionForward passwordChange(
+ ActionMapping mapping,
+ ActionForm form,
+ HttpServletRequest request,
+ HttpServletResponse response)
+ throws IOException, ServletException {
+
+ String login = request.getRemoteUser();
+
+ PasswordChangeActionForm newForm = new PasswordChangeActionForm();
+ newForm.setLogin(login);
+
+ request.getSession(true).setAttribute(
+ PasswordChangeActionForm.formName,
+ newForm);
+
+ return mapping.findForward("passwordChange");
+ }
+
+}
\ No newline at end of file
Index: lams_central/src/java/org/lamsfoundation/lams/web/PasswordChangeAction.java
===================================================================
diff -u
--- lams_central/src/java/org/lamsfoundation/lams/web/PasswordChangeAction.java (revision 0)
+++ lams_central/src/java/org/lamsfoundation/lams/web/PasswordChangeAction.java (revision ca68b26281fd59bce02a7c78f8ff9c3db534d2ae)
@@ -0,0 +1,112 @@
+package org.lamsfoundation.lams.web;
+
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.lamsfoundation.lams.usermanagement.service.UserManagementService;
+
+import org.springframework.web.context.support.WebApplicationContextUtils;
+import org.springframework.web.context.WebApplicationContext;
+
+
+import org.apache.log4j.Logger;
+import org.apache.struts.action.ActionMessage;
+import org.apache.struts.action.ActionMessages;
+import org.apache.struts.action.ActionErrors;
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionForward;
+import org.apache.struts.action.ActionMapping;
+import org.apache.struts.action.Action;
+
+/**
+ * @author Fei Yang
+ *
+ * @struts:action path="/passwordChanged"
+ * name="PasswordChangeActionForm"
+ * input=".passwordChange"
+ * validate="true"
+ *
+ * @struts:action-forward name="okay" path=".passwordChangeOk"
+ * @struts:action-forward name="cancelled" path="/index.jsp"
+ */
+public class PasswordChangeAction extends Action {
+
+ private static Logger log = Logger.getLogger(PasswordChangeAction.class);
+
+ /**
+ * @param mapping The ActionMapping used to select this instance
+ * @param actionForm The optional ActionForm bean for this request (if any)
+ * @param request The HTTP request we are processing
+ * @param response The HTTP response we are creating
+ *
+ */
+ public ActionForward execute(ActionMapping mapping,
+ ActionForm form,
+ HttpServletRequest request,
+ HttpServletResponse response)
+ throws Exception
+ {
+ // -- isCancelled?
+ if (isCancelled(request)) {
+ request.getSession().removeAttribute(PasswordChangeActionForm.formName);
+ return mapping.findForward("cancelled");
+ }
+
+
+ ActionErrors errors = new ActionErrors();
+
+ PasswordChangeActionForm passwordChangeForm = (PasswordChangeActionForm) form;
+
+ if (errors.isEmpty())
+ {
+ try {
+
+ String loggedInUser = request.getRemoteUser();
+ String login = passwordChangeForm.getLogin();
+ String oldPassword = passwordChangeForm.getOldPassword();
+ String password = passwordChangeForm.getPassword();
+
+ if ( loggedInUser == null || ! loggedInUser.equals(login) )
+ {
+ errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.authorisation"));
+ }
+ else
+ {
+
+ WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession(true).getServletContext());
+ UserManagementService service = (UserManagementService)ctx.getBean("userManagementServiceTarget");
+ if(!service.getUserByLogin(login).getPassword().equals(oldPassword))
+ {
+ errors.add("oldPassword", new ActionMessage("error.oldpassword.mismatch"));
+ }
+ else
+ {
+ service.updatePassword(login, password );
+ }
+ }
+
+ } catch (Exception e) {
+ log.error("Exception occured ",e);
+ errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e.getMessage()));
+ }
+
+ } // end if no errors
+
+ // -- Report any errors
+ if (!errors.isEmpty()) {
+ addErrors(request, errors);
+ if (mapping.getInput()!=null)
+ {
+ passwordChangeForm.reset(mapping,request);
+ return (new ActionForward(mapping.getInput()));
+ }
+ // If no input page, use error forwarding
+ return (mapping.findForward("error"));
+ }
+ return mapping.findForward("okay");
+
+ }
+}
+
+
Index: lams_central/src/java/org/lamsfoundation/lams/web/PasswordChangeActionForm.java
===================================================================
diff -u
--- lams_central/src/java/org/lamsfoundation/lams/web/PasswordChangeActionForm.java (revision 0)
+++ lams_central/src/java/org/lamsfoundation/lams/web/PasswordChangeActionForm.java (revision ca68b26281fd59bce02a7c78f8ff9c3db534d2ae)
@@ -0,0 +1,145 @@
+package org.lamsfoundation.lams.web;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+import org.apache.struts.action.ActionMessage;
+import org.apache.struts.action.ActionErrors;
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionMapping;
+
+/**
+ * @author fmalikoff
+ * @Modified by Fei Yang
+ *
+ * @struts:form name="PasswordChangeActionForm"
+ * include-pk="true"
+ * include-all="true"
+ */
+public class PasswordChangeActionForm extends ActionForm {
+
+ public static final String formName = "PasswordChangeActionForm"; // must match name in @struts:action section above
+
+ private static Logger log = Logger.getLogger(PasswordChangeActionForm.class);
+
+ private String oldPassword;
+
+ private String password;
+
+ private String passwordConfirm;
+
+ private String login;
+
+ public PasswordChangeActionForm() {
+ }
+
+ /**
+ * Returns the login.
+ * @return String
+ */
+ public String getLogin() {
+ return login;
+ }
+
+ /**
+ * Returns the password.
+ * @return String
+ */
+ public String getPassword() {
+ return password;
+ }
+
+ /**
+ * Returns the passwordConfirm.
+ * @return String
+ */
+ public String getPasswordConfirm() {
+ return passwordConfirm;
+ }
+
+ /**
+ * Sets the login.
+ * @param login The login to set
+ */
+ public void setLogin(String login) {
+ this.login = StringUtils.trimToEmpty(login);
+ }
+
+ /**
+ * Sets the password.
+ * @param password The password to set
+ */
+ public void setPassword(String password) {
+ this.password = StringUtils.trimToEmpty(password);
+ }
+
+ /**
+ * Sets the passwordConfirm.
+ * @param passwordConfirm The passwordConfirm to set
+ */
+ public void setPasswordConfirm(String passwordConfirm) {
+ this.passwordConfirm = StringUtils.trimToEmpty(passwordConfirm);
+ }
+
+ /**
+ * @return Returns the oldPassword.
+ */
+ public String getOldPassword() {
+ return oldPassword;
+ }
+
+ /**
+ * @param oldPassword The oldPassword to set.
+ */
+ public void setOldPassword(String oldPassword) {
+ this.oldPassword = StringUtils.trimToEmpty(oldPassword);
+ }
+
+ /**
+ * Reset all properties to their default values.
+ *
+ * @param mapping The mapping used to select this instance
+ * @param request The servlet request we are processing
+ */
+ public void reset(ActionMapping mapping, HttpServletRequest request) {
+ setOldPassword(null);
+ setPassword(null);
+ setPasswordConfirm(null);
+ }
+
+ /**
+ * Validate the properties that have been set from this HTTP request,
+ * and return an ActionErrors
object that encapsulates any
+ * validation errors that have been found. If no errors are found, return
+ * null
or an ActionErrors
object with no
+ * recorded error messages.
+ *
+ * @param mapping The mapping used to select this instance
+ * @param request The servlet request we are processing
+ */
+ public ActionErrors validate(ActionMapping mapping,
+ HttpServletRequest request) {
+ ActionErrors errors = super.validate(mapping, request);
+
+ if (errors == null)
+ errors = new ActionErrors();
+
+ if (getPassword() == null || getPassword().length() == 0
+ || !getPassword().equals(getPasswordConfirm())) {
+ errors.add("password", new ActionMessage("error.newpassword.mismatch"));
+ }
+
+ if (errors.isEmpty())
+ return null;
+ else {
+ // don't want to pass back what they had as the password
+ setOldPassword(null);
+ setPassword(null);
+ setPasswordConfirm(null);
+ return errors;
+ }
+
+ }
+
+}
\ No newline at end of file
Index: lams_central/src/java/org/lamsfoundation/lams/web/SessionListener.java
===================================================================
diff -u
--- lams_central/src/java/org/lamsfoundation/lams/web/SessionListener.java (revision 0)
+++ lams_central/src/java/org/lamsfoundation/lams/web/SessionListener.java (revision ca68b26281fd59bce02a7c78f8ff9c3db534d2ae)
@@ -0,0 +1,39 @@
+package org.lamsfoundation.lams.web;
+
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+
+import org.apache.log4j.Logger;
+
+
+/**
+ * Listens for the creation and destruction of http sessions, and reports back to
+ * the ClientSessionInfoManager.
+ *
+ * @web.listener
+ */
+/* Should come out in web.xml as:
+ *
+ *