diff --git a/teamengine-web/src/main/java/com/occamlab/te/web/UpdateUserDetailsHandler.java b/teamengine-web/src/main/java/com/occamlab/te/web/UpdateUserDetailsHandler.java new file mode 100644 index 000000000..3e628625f --- /dev/null +++ b/teamengine-web/src/main/java/com/occamlab/te/web/UpdateUserDetailsHandler.java @@ -0,0 +1,118 @@ +package com.occamlab.te.web; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import com.occamlab.te.realm.PasswordStorage; + +import java.io.File; + +/** + * Handles requests to update user details. + * + */ +public class UpdateUserDetailsHandler extends HttpServlet { + + Config conf; + + public void init() throws ServletException { + conf = new Config(); + } + + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException { + process(request, response); + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException { + process(request, response); + } + + public void process(HttpServletRequest request, HttpServletResponse response) + throws ServletException { + + try { + String username = request.getParameter("username"); + if(username == null){ + username = request.getRemoteUser(); + } + String verifyPassword = request.getParameter("password"); + String email = request.getParameter("email"); + String organization = request.getParameter("organization"); + + File userDir = new File(conf.getUsersDir(), username); + if (!userDir.exists()) { + String url = "updateUserDetails.jsp?error=userNotExists&username=" + + username; + response.sendRedirect(url); + } else { + File xmlfile = new File(userDir, "user.xml"); + Document doc = XMLUtils.parseDocument(xmlfile); + Element userDetails = (Element) (doc.getElementsByTagName("user") + .item(0)); + + if(email == null && organization == null){ + + NodeList emailList = userDetails + .getElementsByTagName("email"); + String registeredEmail = ""; + if (emailList.getLength() > 0) { + Element registeredEmailElement = (Element) emailList.item(0); + registeredEmail = registeredEmailElement.getTextContent(); + } + HttpSession session = request.getSession(); + + session.setAttribute("email", registeredEmail); + NodeList organizationList = userDetails + .getElementsByTagName("organization"); + String registeredOrganization = ""; + if (organizationList.getLength() > 0) { + Element registeredOrgElement = (Element) organizationList.item(0); + registeredOrganization = registeredOrgElement.getTextContent(); + } + session.setAttribute("organization", registeredOrganization); + response.sendRedirect("updateUserDetails.jsp"); + } else { + NodeList storedPwdList = userDetails + .getElementsByTagName("password"); + String storedPassword = null; + if (storedPwdList.getLength() > 0) { + Element storedPwdElement = (Element) storedPwdList.item(0); + storedPassword = storedPwdElement.getTextContent(); + } + Boolean isValid = PasswordStorage.verifyPassword(verifyPassword, storedPassword); + if(isValid){ + //Update email + doc = XMLUtils.removeElement(doc, userDetails, "email"); + Element emailElement = doc.createElement("email"); + emailElement.setTextContent(email); + userDetails.appendChild(emailElement); + //Update organization + doc = XMLUtils.removeElement(doc, userDetails, "organization"); + Element orgElement = doc.createElement("organization"); + orgElement.setTextContent(organization); + userDetails.appendChild(orgElement); + + XMLUtils.transformDocument(doc, new File(userDir, "user.xml")); + + String url = "viewSessions.jsp?success=updateDetails"; + response.sendRedirect(url); + } else { + String url = "updateUserDetails.jsp?error=invalidPwd"; + response.sendRedirect(url); + } + } + } + } catch (Exception e) { + throw new ServletException(e); + } + } +} diff --git a/teamengine-web/src/main/webapp/WEB-INF/web.xml b/teamengine-web/src/main/webapp/WEB-INF/web.xml index 562377d24..1905c3995 100644 --- a/teamengine-web/src/main/webapp/WEB-INF/web.xml +++ b/teamengine-web/src/main/webapp/WEB-INF/web.xml @@ -61,6 +61,10 @@ changePasswordHandler com.occamlab.te.web.ChangePasswordHandler + + updateUserDetailsHandler + com.occamlab.te.web.UpdateUserDetailsHandler + emailLog com.occamlab.te.web.EmailLogServlet @@ -132,6 +136,10 @@ changePasswordHandler /changePasswordHandler + + updateUserDetailsHandler + /updateUserDetailsHandler + logout /logout @@ -172,6 +180,7 @@ /emailSent.jsp /userProfile.jsp /startTesting.jsp + /updateUserDetails.jsp user diff --git a/teamengine-web/src/main/webapp/header.jsp b/teamengine-web/src/main/webapp/header.jsp index 3153b6467..eb765b7a3 100644 --- a/teamengine-web/src/main/webapp/header.jsp +++ b/teamengine-web/src/main/webapp/header.jsp @@ -1,7 +1,39 @@ +
+ > TEAM Engine Banner
@@ -13,9 +45,16 @@ Cookie userName=new Cookie("User", user); response.addCookie(userName); if (user != null && user.length() > 0) { - out.println("\t\t
"); - out.println("\t\t\tUser: " + user + "
"); - out.println("\t\t\tLogout\t\t\t" + " | Change Password"); + out.println("\t\t
"); + out.println("
"); + out.println("
User: " + user + " ▼
"); + out.println("
"); + out.println(" Change Password"); + out.println(" Update User Details"); + out.println(" Logout"); + out.println("
"); + out.println("
"); + out.println("\t\t
"); } %> diff --git a/teamengine-web/src/main/webapp/updateUserDetails.jsp b/teamengine-web/src/main/webapp/updateUserDetails.jsp new file mode 100644 index 000000000..19e837450 --- /dev/null +++ b/teamengine-web/src/main/webapp/updateUserDetails.jsp @@ -0,0 +1,101 @@ +<%@page import="java.util.Collection"%> +<%@ page language="java" + import="java.io.File, javax.xml.parsers.*, java.util.Arrays, com.occamlab.te.web.*, java.util.List, java.util.ArrayList"%> +<% +String username = request.getRemoteUser(); +String email = session.getAttribute("email").toString(); +String organization = session.getAttribute("organization").toString(); +%> + + + + + +Update User Details + + + + <%@ include file="header.jsp"%> +

Update User Details

+
+ <% + if ("invalidPwd".equals(request.getParameter("error"))) { + out.println("Password did not match."); + } else if("userNotExists".equals(request.getParameter("error"))){ + out.println("Not valid user!"); + } + %> +
+ +
+

+ Enter all mandatory fields:

+ + + + + + + + + + + + + + + + + + + + +
" />
Password :
Email :" />
Organization :" />
+

+
+ <%@ include file="footer.jsp"%> + + diff --git a/teamengine-web/src/main/webapp/viewSessions.jsp b/teamengine-web/src/main/webapp/viewSessions.jsp index 4c7e87cc5..c2936c93c 100644 --- a/teamengine-web/src/main/webapp/viewSessions.jsp +++ b/teamengine-web/src/main/webapp/viewSessions.jsp @@ -66,6 +66,14 @@ <%@ include file="header.jsp" %>

Test Sessions

+
+ <% + if ("updateDetails".equals(request.getParameter("success"))) { + out.println("User details updated successfully!"); + } + %> +
+
Create a new session