Skip to content

Commit

Permalink
Added new form to update the user details. #376
Browse files Browse the repository at this point in the history
  • Loading branch information
Keshav Nangare committed Apr 16, 2019
1 parent f121dd5 commit 638363c
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
9 changes: 9 additions & 0 deletions teamengine-web/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<servlet-name>changePasswordHandler</servlet-name>
<servlet-class>com.occamlab.te.web.ChangePasswordHandler</servlet-class>
</servlet>
<servlet>
<servlet-name>updateUserDetailsHandler</servlet-name>
<servlet-class>com.occamlab.te.web.UpdateUserDetailsHandler</servlet-class>
</servlet>
<servlet>
<servlet-name>emailLog</servlet-name>
<servlet-class>com.occamlab.te.web.EmailLogServlet</servlet-class>
Expand Down Expand Up @@ -132,6 +136,10 @@
<servlet-name>changePasswordHandler</servlet-name>
<url-pattern>/changePasswordHandler</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>updateUserDetailsHandler</servlet-name>
<url-pattern>/updateUserDetailsHandler</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/logout</url-pattern>
Expand Down Expand Up @@ -172,6 +180,7 @@
<url-pattern>/emailSent.jsp</url-pattern>
<url-pattern>/userProfile.jsp</url-pattern>
<url-pattern>/startTesting.jsp</url-pattern>
<url-pattern>/updateUserDetails.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
Expand Down
47 changes: 43 additions & 4 deletions teamengine-web/src/main/webapp/header.jsp
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
<style>
.dropbtn {
padding: 16px;
font-size: 16px;
border: none;
background-color: white;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: fixed;
background-color: #f1f1f1;
min-width: 150px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown:hover .dropdown-content {display: block;}
</style>
<div style="position: static">
<div
style="position: static; background-color: black; width: 100%; height: 100px; overflow: hidden"
onclick="window.location = ''">
>
<!-- Image derived from "Dinky the Steam Engine - main drive wheel", Steve Karg, http://www.burningwell.org -->
<img style="position: absolute" src="images/banner.jpg" alt="TEAM Engine Banner" />
<div style="position: absolute;">
Expand All @@ -13,9 +45,16 @@
Cookie userName=new Cookie("User", user);
response.addCookie(userName);
if (user != null && user.length() > 0) {
out.println("\t\t<div style=\"position: absolute; right:20px; top:25px; background-color: white; padding: 3px; border-style: inset\">");
out.println("\t\t\tUser: " + user + "<br/>");
out.println("\t\t\t<a href=\"logout\">Logout</a>\t\t\t" + " <b> | </b> <a href=\"changePassword.jsp\">Change Password</a>");
out.println("\t\t<div style=\"position: absolute; right:20px; top:25px; background-color: white; border-style: inset\">");
out.println("<div class=\"dropdown\">");
out.println(" <div class=\"dropbtn\"> User: " + user + " &#9660;</div>");
out.println(" <div class=\"dropdown-content\">");
out.println(" <a href=\"changePassword.jsp\">Change Password</a>");
out.println(" <a href=\"updateUserDetailsHandler\">Update User Details</a>");
out.println(" <a href=\"logout\">Logout</a>");
out.println(" </div>");
out.println("</div>");
out.println("\t\t</div>");
}
%>
Expand Down
101 changes: 101 additions & 0 deletions teamengine-web/src/main/webapp/updateUserDetails.jsp
Original file line number Diff line number Diff line change
@@ -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();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Update User Details</title>
<script>
function showerror(msg) {
var error = document.getElementById("error");
var child = error.lastChild;
if (child) {
error.replaceChild(document.createTextNode(msg), child);
} else {
error.appendChild(document.createTextNode(msg));
}
}
function submitform() {
var form = document.forms["updateUserDetails"];
var password = form.elements["password"].value;
var email = form.elements["email"].value;
var organization = form.elements["organization"].value;
var emailFormat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (password.length == 0) {
showerror("Password is required.");
return;
}
if (!email.match(emailFormat)) {
showerror("You have entered an invalid email address!");
return;
}
if (organization.length < 3) {
showerror("Organization cannot be empty!");
return;
}
form.submit();
}
function resetform() {
var form = document.forms["updateUserDetails"];
form.elements["password"].value = "";
form.elements["email"].value = "";
form.elements["organization"].value = "";
}
</script>
</head>
<body>
<%@ include file="header.jsp"%>
<h2>Update User Details</h2>
<div id="error" style="color: red">
<%
if ("invalidPwd".equals(request.getParameter("error"))) {
out.println("Password did not match.");
} else if("userNotExists".equals(request.getParameter("error"))){
out.println("Not valid user!");
}
%>
</div>

<form name="updateUserDetails" method="post"
action="updateUserDetailsHandler">
<p>
Enter all mandatory fields: <br /> <br />
<table>
<tr>
<td></td>
<td><input name="username" type="hidden"
value="<%= username == null ? "" : username %>" /></td>
</tr>
<tr>
<td>Password :</td>
<td><input name="password" type="password" /></td>
</tr>
<tr>
<td>Email :</td>
<td><input name="email" type="text"
value="<%=email == null ? "" : email %>" /></td>
</tr>
<tr>
<td>Organization :</td>
<td><input name="organization" type="text"
value="<%=organization == null ? "" : organization%>" /></td>
</tr>
<td><input type="button" value="Submit" onclick="submitform()" /></td>
<td><input type="button" value="Reset" onclick="resetform()" /></td>
</tr>
</table>
</p>
</form>
<%@ include file="footer.jsp"%>
</body>
</html>
8 changes: 8 additions & 0 deletions teamengine-web/src/main/webapp/viewSessions.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
<%@ include file="header.jsp" %>
<h2>Test Sessions</h2>

<div id="success" style="color: #0325f9">
<%
if ("updateDetails".equals(request.getParameter("success"))) {
out.println("User details updated successfully!");
}
%>
</div>

<br />
<a href="createSession.jsp">Create a new session</a>
<br />
Expand Down

0 comments on commit 638363c

Please sign in to comment.