1 package org.paneris.bibliomania;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5
6
7 import javax.servlet.http.HttpSession;
8
9 import org.melati.Melati;
10 import org.melati.servlet.Form;
11 import org.melati.login.Login;
12 import org.melati.login.LoginHandler;
13 import org.melati.poem.AccessToken;
14 import org.melati.poem.BaseFieldAttributes;
15 import org.melati.poem.Field;
16 import org.melati.poem.FieldAttributes;
17 import org.melati.poem.PoemTask;
18 import org.melati.poem.PoemThread;
19 import org.melati.servlet.TemplateServlet;
20 import org.melati.template.ServletTemplateContext;
21 import org.melati.util.Email;
22 import org.melati.util.UnexpectedExceptionException;
23 import org.webmacro.Context;
24 import org.webmacro.FastWriter;
25 import org.webmacro.WebMacroException;
26 import org.webmacro.servlet.WebContext;
27
28
29
30 public class BibliomaniaLogin extends BibliomaniaServlet {
31
32
33
34
35 private static final long serialVersionUID = 1L;
36
37 protected void fillContext(Melati melati, BibContext it) {
38 it.setLogicalDatabase("bibliomania");
39 }
40
41 public String loginTemplate(String name) {
42 return bibliomaniaTemplate("bibliomanialogin/" + name);
43 }
44
45 public static class Handler extends LoginHandler {
46
47 public Handler(TemplateServlet s) {
48 super(s);
49 }
50
51 public String getLogin(ServletTemplateContext context) {
52 String it = context.getFormField("field_login");
53 return it == null ? context.getFormField("field_email") : it;
54 }
55
56 protected String loginTemplate(String name) {
57 return bibliomaniaTemplate("bibliomanialogin/" + name);
58 }
59
60 protected String usernameUnknownTemplate() {
61 return loginTemplate("Login");
62 }
63
64 protected String passwordIncorrectTemplate() {
65 return loginTemplate("Login");
66 }
67 }
68
69 Handler loginHandler = null;
70
71 protected Handler getLoginHandler() {
72 if (loginHandler == null)
73 loginHandler = new Handler(this);
74
75 return loginHandler;
76 }
77
78 protected String handleRemind(Melati melati, WebContext context)
79 throws Exception {
80
81 getLoginHandler().setupContext(melati.getServletTemplateContext());
82
83 final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
84
85 UserTable users = (UserTable)db.getUserTable();
86 String login = context.getForm("field_login");
87 if (login == null)
88 login = context.getForm("field_email");
89
90 User u = (User)users.getLoginColumn().firstWhereEq(login);
91 if (u == null)
92 u = (User)users.getEmailColumn().firstWhereEq(login);
93 final User user = u;
94
95 if (user == null)
96 context.put("loginUnknown", Boolean.TRUE);
97 else {
98 final String[] email = new String[1];
99 ByteArrayOutputStream buf = new ByteArrayOutputStream();
100 final FastWriter bufp = new FastWriter(db.getWebMacro().getBroker(),
101 buf, "UTF8");
102
103 PoemThread.withAccessToken(
104 AccessToken.root,
105 new PoemTask() {
106 public void run() {
107 email[0] = user.getEmail();
108 if (email[0] == null)
109 return;
110
111 email[0] = email[0].replace("\n\r", " ");
112 final org.webmacro.Template messageTemplate =
113 db.getPasswordReminderTemplate();
114 final Context msgContext = db.getWebMacro().getContext();
115
116 msgContext.put("db", db);
117 msgContext.put("user", user);
118
119 try {
120 messageTemplate.write(bufp.getOutputStream(), msgContext);
121 }
122 catch (WebMacroException e) {
123 throw new UnexpectedExceptionException(
124 "Error expanding password reminder message", e);
125 }
126 catch (IOException e) {
127 throw new UnexpectedExceptionException(
128 "Error expanding password reminder message", e);
129 }
130 }
131 });
132
133 if (email[0] == null)
134 context.put("reminded", "(no address)");
135 else {
136 bufp.flush();
137
138 String from = db.getPasswordReminderFrom().replace("\n\r", " ");
139
140
141
142
143
144
145
146
147
148
149
150
151
152 Email.send(db.getSmtpServer(), from, email[0], null, "Your bibliomania.com password", buf.toString().trim());
153 context.put("reminded", email[0]);
154 }
155 }
156
157 return loginTemplate("Login");
158 }
159
160 protected String handleRegister(Melati melati, WebContext context,
161 User template)
162 throws Exception {
163
164 final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
165 UserTable userTable = (UserTable)db.getUserTable();
166 ServletTemplateContext tc = melati.getServletTemplateContext();
167 String email = context.getForm("field_email");
168 if (email == null) email = context.getForm("field_login");
169
170 context.put("email", new Field(email,
171 userTable.getEmailColumn()).withNullable(false));
172 context.put("name", new Field(Form.getFieldNulled(tc,"field_name"),
173 new BaseFieldAttributes((FieldAttributes)userTable.getNameColumn(), true)));
174 context.put("password",
175 new Field(Form.getFieldNulled(tc,"field_password"),
176 userTable.getPasswordColumn()));
177 context.put("age", new Field(Form.getIntegerField(tc,"field_age"),
178 userTable.getAgeColumn()));
179 context.put("sex", new Field(Form.getIntegerField(tc,"field_sex"),
180 userTable.getSexColumn()));
181 context.put("country",
182 new Field(
183 Form.getIntegerField(tc,"field_country"),
184 userTable.getCountryColumn()));
185 context.put("fulltimeeducation",
186 new Field(Form.getBooleanField(
187 tc, "field_fulltimeeducation"),
188 userTable.getFulltimeeducationColumn()));
189 context.put("wantemailalerts",
190 new Field(Form.getBooleanField(tc,
191 "field_wantemailalerts"),
192 userTable.getWantemailalertsColumn()));
193 context.put("wantspam",
194 new Field(Form.getBooleanField(tc, "field_wantspam"),
195 userTable.getWantspamColumn()));
196
197 if (template == null) {
198 final User[] t = new User[1];
199 PoemThread.withAccessToken(
200 AccessToken.root,
201 new PoemTask() {
202 public void run() {
203 t[0] = (User)db.getTemplateRegisterUser().duplicated();
204 t[0].setName_unsafe(null);
205 }
206 });
207 template = t[0];
208 }
209
210 return loginTemplate("Register");
211 }
212
213 protected String handleRegisterFinish(final Melati melati,
214 final WebContext context)
215 throws Exception {
216 final BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase();
217 final UserTable userTable = (UserTable)db.getUserTable();
218
219 String login = context.getForm("field_login");
220 if (login == null)
221 login = context.getForm("field_email");
222
223 final User existing =
224 (User)userTable.getLoginColumn().firstWhereEq(login);
225
226 if (existing == null) {
227 final User fromForm = (User)userTable.newPersistent();
228 Form.extractFields(melati.getServletTemplateContext(), fromForm);
229 fromForm.generateDefaults();
230 String email = fromForm.getEmail().trim();
231
232
233
234
235
236
237
238
239 if (email != null) fromForm.setLogin(email);
240 PoemThread.withAccessToken(
241 AccessToken.root,
242 new PoemTask() {
243 public void run() {
244 userTable.create(fromForm);
245 }
246 });
247
248 HttpSession session = context.getSession();
249 session.removeAttribute(Login.TRIGGERING_REQUEST_PARAMETERS);
250 session.removeAttribute(Login.TRIGGERING_EXCEPTION);
251 context.remove("continuationURL");
252 return getLoginHandler().loginSuccessfullyAs(melati,
253 melati.getServletTemplateContext(), fromForm);
254 }
255 else if (existing.getPassword_unsafe().equals(
256 context.getForm("field_password"))) {
257 PoemThread.withAccessToken(
258 existing,
259 new PoemTask() {
260 public void run() {
261 Form.extractFields(melati.getServletTemplateContext(), existing);
262 }
263 });
264
265 return getLoginHandler().loginSuccessfullyAs(melati,
266 melati.getServletTemplateContext(), existing);
267 }
268 else {
269 User fromForm = (User)userTable.newPersistent();
270 Form.extractFields(melati.getServletTemplateContext(), fromForm);
271 context.put("loginInUse", Boolean.TRUE);
272 return handleRegister(melati, context, fromForm);
273 }
274 }
275
276 protected String bibliomaniaHandle(Melati melati, final WebContext context)
277 throws Exception {
278 String action = context.getForm("action");
279
280 if (action == null) action = "";
281
282 String login = context.getForm("field_login");
283 if (login == null)
284 login = context.getForm("field_email");
285
286 if (action.equals("remind") && login != null)
287 return handleRemind(melati, context);
288 else if (action.equals("register"))
289 return handleRegister(melati, context, null);
290 else if (action.equals("create"))
291 return handleRegisterFinish(melati, context);
292 else
293 return getLoginHandler().doTemplateRequest(melati,
294 melati.getServletTemplateContext());
295 }
296 }