-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new form to update the user details. #376
- Loading branch information
Keshav Nangare
committed
Apr 16, 2019
1 parent
f121dd5
commit 638363c
Showing
5 changed files
with
279 additions
and
4 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
teamengine-web/src/main/java/com/occamlab/te/web/UpdateUserDetailsHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters