1 package org.paneris.user.controller;
2
3 import java.sql.Connection;
4 import java.sql.ResultSet;
5 import java.sql.Statement;
6
7 import org.paneris.jal.model.DBConnectionManager;
8 import org.paneris.jal.model.Email;
9 import org.paneris.jal.model.SystemProperties;
10 import org.paneris.user.model.User;
11 import org.paneris.util.SessionUtil;
12 import org.webmacro.Template;
13 import org.webmacro.servlet.HandlerException;
14 import org.webmacro.servlet.PanerisPage;
15 import org.webmacro.servlet.WebContext;
16
17 public class LoginForgot extends PanerisPage {
18
19 private static final long serialVersionUID = 1L;
20 private static final boolean debug = false;
21 String db;
22
23 public Template handle(WebContext context) throws HandlerException {
24 db = (String) context.getForm("db");
25 context.put("db",db);
26 String send = (String) context.getForm("send");
27 String email = (String) context.getForm("email");
28 String templateName = null;
29 try {
30 templateName = User.checkLoggedIn("controller",
31 this.getClass().getName(),
32 context,
33 " update messages.");
34 } catch (Exception e) {
35 throw new HandlerException("Could not get user:" + e.toString());
36 }
37 if (send == null) {
38 if (context.getCGI().getHTTP_REFERER() != null)
39 context.getSession().setAttribute(
40 "LoginForgotReturnURL",
41 context.getCGI().getHTTP_REFERER());
42 }
43 SessionUtil.setReturnURL("LoginForgotReturnURL",context);
44 if (templateName == null) {
45 templateName = (String) context.getForm("wmtemplate");
46 if (templateName == null) {
47 templateName = "user/view/LoginForgot.wm";
48 }
49 try {
50 if (send != null) {
51 if (email == null || email.equals("")) {
52 context.put("error","Please enter your email Address.");
53 } else {
54 DBConnectionManager connMgr = DBConnectionManager.getInstance();
55 Connection conn = connMgr.getConnection("User",db);
56 Statement s = conn.createStatement();
57 String sqlString = "SELECT id FROM users WHERE TRIM(";
58 if (connMgr.getDatabaseEngineType(db) == DBConnectionManager.ACCESS) {
59 sqlString += "UCASE";
60 } else {
61 sqlString += "UPPER";
62 }
63 sqlString += "(email)) = '" + email.toUpperCase() + "'";
64 if (debug)
65 System.err.println("sqlString" + sqlString);
66 ResultSet rs = s.executeQuery(sqlString);
67 if (rs.next()) {
68 String sitename = "Unknown";
69 try {
70 SystemProperties sp = new SystemProperties(db);
71 sitename = sp.getProperty("sitename");
72 } catch (Exception e) {
73 throw new HandlerException(
74 "Could not find site name from system.properties. ");
75 }
76 User systemuser = User.getSystemUser(db);
77 if (systemuser == null) {
78 throw new HandlerException("Could not find system user from system.properties. ");
79 }
80 User user = new User(db,new Integer(rs.getInt(1)));
81 String message = "You have requested to be sent your password for "
82 + sitename
83 + ". Your details are:\nLogin Id: "
84 + user.getFieldValue("loginid")
85 + "\nPassword: " + user.getFieldValue("password");
86 Email.send(db, systemuser.getFieldValue("email"), email, "", sitename + " Login Details", message);
87 templateName = (String) context.getForm("SuccessTemplateName");
88 if (templateName == null) {
89 templateName = "user/view/LoginForgotSuccess.wm";
90 }
91 } else {
92 context.put("error","Sorry, your email address was not recognised, please try again.");
93 }
94 }
95 }
96 } catch (Exception e) {
97 throw new HandlerException(e.toString());
98 }
99 }
100
101 try {
102 return (Template) context.getBroker().get("template",templateName);
103 } catch (Exception e) {
104 throw new HandlerException("Could not locate template: " + templateName);
105 }
106 }
107 }