0
|
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 <html>
|
|
37 <head>
|
|
38 <% Shared.title(request,response,"Change Email Confirmation"); %>
|
|
39 </head>
|
|
40 <body>
|
|
41 <% Shared.minHeaderGlobal(request,response); %>
|
|
42 <h1>Change Email Confirmation</h1>
|
|
43 <%
|
|
44 String email = request.getParameter("email");
|
|
45 long userId = Long.valueOf(request.getParameter("user"));
|
|
46
|
|
47 Site site = Jtp.getSite(request);
|
|
48 User user = site.getUser(userId);
|
|
49 String newEmail = user.getProperty("_new_email");
|
|
50 int hash = Integer.parseInt(request.getParameter("h"));
|
|
51 user.setProperty("_new_email", null); // delete key
|
|
52 if (newEmail == null || !newEmail.equals(email) || hash != ChangeEmail.emailHash(newEmail))
|
|
53 {
|
|
54 %>
|
|
55 <p><strong>We were unable to change your email address.</strong></p>
|
|
56 <p>Please try <a href="ChangeEmail.jtp?user=<%=user.getId()%>">changing your email</a> again or <a href="/template/NamlServlet.jtp?macro=user_profile">go back to menu</a>.</p>
|
|
57 <%
|
|
58 } else {
|
|
59 DbDatabase db = site.getDb();
|
|
60 db.beginTransaction();
|
|
61 try {
|
|
62 User u = user.getGoodCopy();
|
|
63 String oldEmail = u.getEmail();
|
|
64 u.setEmail(email);
|
|
65 u.update();
|
|
66 ServletUtils.setCookie(request, response, "email", email, true, null);
|
|
67 db.commitTransaction();
|
|
68 callNaml(site, oldEmail, email);
|
|
69 %>
|
|
70 <p>Your email has been changed.</p>
|
|
71 <p><a href="/template/NamlServlet.jtp?macro=user_profile">Go back to your profile</a></p>
|
|
72 <%
|
|
73 } catch(ModelException e) {
|
|
74 logger.info("ex inTrans="+db.isInTransaction(),e);
|
|
75 %>
|
|
76 Email change failed:
|
|
77 <br /><%=e.getMessage()%>
|
|
78 <%
|
|
79 } catch(RuntimeException e) {
|
|
80 logger.error("ex inTrans="+db.isInTransaction(),e);
|
|
81 throw e;
|
|
82 } finally {
|
|
83 db.endTransaction();
|
|
84 }
|
|
85 }
|
|
86 %>
|
|
87
|
|
88 <% Shared.footer(request,response); %>
|
|
89 <% Shared.analytics(request,response); %>
|
|
90 </body>
|
|
91 </html>
|
|
92 <%
|
|
93 }
|
|
94
|
|
95 private static void callNaml(Site site, String oldEmail, String newEmail) {
|
|
96 Template template = site.getTemplate( "user email changed",
|
|
97 BasicNamespace.class, NabbleNamespace.class
|
|
98 );
|
|
99 Map<String,Object> params = new HashMap<String,Object>();
|
|
100 params.put("old_email",oldEmail);
|
|
101 params.put("new_email",newEmail);
|
|
102 template.run( TemplatePrintWriter.NULL, params,
|
|
103 new BasicNamespace(template),
|
|
104 new NabbleNamespace(site)
|
|
105 );
|
|
106 }
|
|
107 }
|
|
108 %>
|