Mercurial Hosting > nabble
comparison src/nabble/view/web/user/ChangeEmail3.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 | |
2 package nabble.view.web.user; | |
3 | |
4 import fschmidt.db.DbDatabase; | |
5 import fschmidt.util.servlet.ServletUtils; | |
6 import nabble.model.ModelException; | |
7 import nabble.model.Site; | |
8 import nabble.model.User; | |
9 import nabble.naml.compiler.Template; | |
10 import nabble.naml.compiler.TemplatePrintWriter; | |
11 import nabble.naml.namespaces.BasicNamespace; | |
12 import nabble.view.lib.Jtp; | |
13 import nabble.view.lib.Shared; | |
14 import nabble.view.web.template.NabbleNamespace; | |
15 import org.slf4j.Logger; | |
16 import org.slf4j.LoggerFactory; | |
17 | |
18 import javax.servlet.ServletException; | |
19 import javax.servlet.http.HttpServlet; | |
20 import javax.servlet.http.HttpServletRequest; | |
21 import javax.servlet.http.HttpServletResponse; | |
22 import java.io.IOException; | |
23 import java.io.PrintWriter; | |
24 import java.util.HashMap; | |
25 import java.util.Map; | |
26 | |
27 | |
28 public final class ChangeEmail3 extends HttpServlet { | |
29 private static final Logger logger = LoggerFactory.getLogger(ChangeEmail3.class); | |
30 | |
31 protected void service(HttpServletRequest request,HttpServletResponse response) | |
32 throws ServletException, IOException | |
33 { | |
34 PrintWriter out = response.getWriter(); | |
35 | |
36 out.print( "\r\n<html>\r\n<head>\r\n " ); | |
37 Shared.title(request,response,"Change Email Confirmation"); | |
38 out.print( "\r\n</head>\r\n<body>\r\n " ); | |
39 Shared.minHeaderGlobal(request,response); | |
40 out.print( "\r\n <h1>Change Email Confirmation</h1>\r\n " ); | |
41 | |
42 String email = request.getParameter("email"); | |
43 long userId = Long.valueOf(request.getParameter("user")); | |
44 | |
45 Site site = Jtp.getSite(request); | |
46 User user = site.getUser(userId); | |
47 String newEmail = user.getProperty("_new_email"); | |
48 int hash = Integer.parseInt(request.getParameter("h")); | |
49 user.setProperty("_new_email", null); // delete key | |
50 if (newEmail == null || !newEmail.equals(email) || hash != ChangeEmail.emailHash(newEmail)) | |
51 { | |
52 | |
53 out.print( "\r\n<p><strong>We were unable to change your email address.</strong></p>\r\n<p>Please try <a href=\"ChangeEmail.jtp?user=" ); | |
54 out.print( (user.getId()) ); | |
55 out.print( "\">changing your email</a> again or <a href=\"/template/NamlServlet.jtp?macro=user_profile\">go back to menu</a>.</p>\r\n" ); | |
56 | |
57 } else { | |
58 DbDatabase db = site.getDb(); | |
59 db.beginTransaction(); | |
60 try { | |
61 User u = user.getGoodCopy(); | |
62 String oldEmail = u.getEmail(); | |
63 u.setEmail(email); | |
64 u.update(); | |
65 ServletUtils.setCookie(request, response, "email", email, true, null); | |
66 db.commitTransaction(); | |
67 callNaml(site, oldEmail, email); | |
68 | |
69 out.print( "\r\n<p>Your email has been changed.</p>\r\n<p><a href=\"/template/NamlServlet.jtp?macro=user_profile\">Go back to your profile</a></p>\r\n" ); | |
70 | |
71 } catch(ModelException e) { | |
72 logger.info("ex inTrans="+db.isInTransaction(),e); | |
73 | |
74 out.print( "\r\nEmail change failed:\r\n<br />" ); | |
75 out.print( (e.getMessage()) ); | |
76 out.print( "\r\n" ); | |
77 | |
78 } catch(RuntimeException e) { | |
79 logger.error("ex inTrans="+db.isInTransaction(),e); | |
80 throw e; | |
81 } finally { | |
82 db.endTransaction(); | |
83 } | |
84 } | |
85 | |
86 out.print( "\r\n\r\n" ); | |
87 Shared.footer(request,response); | |
88 out.print( "\r\n" ); | |
89 Shared.analytics(request,response); | |
90 out.print( "\r\n</body>\r\n</html>\r\n" ); | |
91 | |
92 } | |
93 | |
94 private static void callNaml(Site site, String oldEmail, String newEmail) { | |
95 Template template = site.getTemplate( "user email changed", | |
96 BasicNamespace.class, NabbleNamespace.class | |
97 ); | |
98 Map<String,Object> params = new HashMap<String,Object>(); | |
99 params.put("old_email",oldEmail); | |
100 params.put("new_email",newEmail); | |
101 template.run( TemplatePrintWriter.NULL, params, | |
102 new BasicNamespace(template), | |
103 new NabbleNamespace(site) | |
104 ); | |
105 } | |
106 } | |
107 |