view src/nabble/view/web/user/EditProfile.java @ 19:18cf4872fd7f

remove anonymous posting
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 29 May 2020 22:58:25 -0600
parents 7ecd1a4ef557
children
line wrap: on
line source


package nabble.view.web.user;

import fschmidt.db.DbDatabase;
import fschmidt.util.java.HtmlUtils;
import fschmidt.util.servlet.ServletUtils;
import nabble.model.Db;
import nabble.model.ModelException;
import nabble.model.User;
import nabble.view.lib.Jtp;
import nabble.view.lib.Shared;
import nabble.view.lib.help.Help;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public final class EditProfile extends HttpServlet {

	protected void service(HttpServletRequest request,HttpServletResponse response)
		throws ServletException, IOException
	{
		PrintWriter out = response.getWriter();
		User user = Jtp.getUser(request);
		if( user==null ) {
			Jtp.login("You must login to edit your profile.",request,response);
			return;
		}
		String password1 = null;
		String password2 = null;
		String name;
		String errorMsg = null;

		if ("save".equals(request.getParameter("action")) && "POST".equals(request.getMethod())) {
			password1 = request.getParameter("password1");
			password2 = request.getParameter("password2");
			name = request.getParameter("name");
			if (!password1.equals(password2) ) {
				errorMsg = "The password fields don't match.";
			} else if (password1.length() > 0 && password1.trim().length() == 0) {
				errorMsg = "Your password must contain valid alphanumeric characters.";
			} else {
				DbDatabase db = user.getSite().getDb();
				db.beginTransaction();
				try {
					User u = user.getGoodCopy();
					if (password1.length() > 0)
						u.setPassword(password1);
					u.setName(name);
					u.update();
					db.commitTransaction();
					String pwd = u.getPasscookie();
					ServletUtils.setCookie(request,response,"username", HtmlUtils.urlEncode(name), false, null);
					ServletUtils.setCookie(request,response,"password", HtmlUtils.urlEncode(pwd), false, null);

					StringBuffer js = new StringBuffer();
					js.append("if (parent.nabbleinfo) {");
					js.append("Nabble.setCookie('username','").append(HtmlUtils.javascriptStringEncode(HtmlUtils.urlEncode(name))).append("');");
					js.append("Nabble.setCookie('password','").append(HtmlUtils.javascriptStringEncode(HtmlUtils.urlEncode(pwd))).append("');");
					js.append("}");

					Shared.javascriptRedirect(request,response, "/template/NamlServlet.jtp?macro=user_profile", js.toString());
					return;
				} catch(ModelException e) {
					errorMsg = e.getMessage();
				} finally {
					db.endTransaction();
				}
			}
		} else {
			name = user.getName();
		}
		
		out.print( "\r\n<html>\r\n	<head>\r\n		" );
 Shared.title(request,response,"Edit Personal Information"); 
		out.print( "\r\n	</head>\r\n	<body>\r\n		" );
 Shared.minHeaderGlobal(request, response); 
		out.print( "\r\n		" );
 Shared.profileHeading(request,out,user,"Edit Personal Information"); 
		out.print( "\r\n		" );
 Shared.errorMessage(request,response,errorMsg, "Please re-enter the information and click on \"Update Information\"."); 
		out.print( "\r\n		<style>\r\n			div.field-title {\r\n				margin-top: 0;\r\n			}\r\n		</style>\r\n		<form method=post action=\"EditProfile.jtp\">\r\n			<input type=hidden name=\"action\" value=\"save\">\r\n\r\n			<div class=\"field-box light-border-color\">\r\n				<div class=\"second-font field-title\">Email</div>\r\n				<div class=\"weak-color\">\r\n					" );
		out.print( (user.getEmail()) );
		out.print( "\r\n					&#187; <a href=\"ChangeEmail.jtp\">Change Email</a>\r\n				</div>\r\n			</div>\r\n\r\n			<div class=\"field-box light-border-color\" id=\"username-field\" >\r\n				<div class=\"second-font field-title\">Your User Name</div>\r\n				<div class=\"weak-color\">\r\n					Your user name must be unique in " );
		out.print( (user.getSite().getRootNode().getSubjectHtml()) );
		out.print( ".\r\n				</div>\r\n				<div><input name=\"name\" size=\"25\" maxlength=\"25\" value=\"" );
		out.print( (HtmlUtils.htmlEncode(Jtp.hideNull(name))) );
		out.print( "\" /></div>\r\n			</div>\r\n\r\n			<div class=\"field-box light-border-color\">\r\n				<div class=\"second-font field-title\">Change Password</div>\r\n				<div class=\"weak-color\">Nabble encrypts your password (<a href=\"" );
		out.print( (Help.password.url(request)) );
		out.print( "\">?</a>)</div>\r\n				<table style=\"margin: .4em 0\" class=\"shaded-bg-color\">\r\n					<tr valign=\"top\">\r\n						<td class=\"form-label\" style=\"padding-top:.6em\">Password:&nbsp;</td>\r\n						<td><input type=\"password\" name=\"password1\" size=\"25\" value=\"" );
		out.print( (Jtp.hideNull(password1)) );
		out.print( "\"/></td>\r\n					</tr>\r\n					<tr>\r\n						<td class=\"form-label\">Confirm Password:&nbsp;</td>\r\n						<td><input type=\"password\" name=\"password2\" size=\"25\" value=\"" );
		out.print( (Jtp.hideNull(password2)) );
		out.print( "\"/></td>\r\n					</tr>\r\n				</table>\r\n			</div>\r\n\r\n			<div class=\"field-box light-border-color\" style=\"padding-top:0\">\r\n				<input type=submit value=\"Update Personal Information\" />\r\n				or <a href=\"/template/NamlServlet.jtp?macro=user_profile\">Cancel</a>\r\n			</div>\r\n		</form>\r\n\r\n		" );
 Shared.footer(request,response); 
		out.print( "\r\n		" );
 Shared.analytics(request,response); 
		out.print( "\r\n	</body>\r\n</html>\r\n" );

	}
}