aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/control/pro/account_control.js
blob: 031dbe6b2f88c0ea7537172c14dfef576cd74921 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("stringutils");
import("stringutils.*");
import("funhtml.*");
import("email.sendEmail");
import("cache_utils.syncedWithCache");

import("etherpad.helpers");
import("etherpad.utils.*");
import("etherpad.sessions.getSession");
import("etherpad.pro.pro_accounts");
import("etherpad.pro.pro_accounts.getSessionProAccount");
import("etherpad.pro.domains");
import("etherpad.pro.pro_utils");
import("etherpad.pro.pro_account_auto_signin");
import("etherpad.pro.pro_config");
import("etherpad.pad.pad_security");
import("etherpad.pad.padutils");
import("etherpad.pad.padusers");
import("etherpad.collab.collab_server");

function onRequest() {
  if (!getSession().tempFormData) {
    getSession().tempFormData = {};
  }

  return false; // path not handled here
}

//--------------------------------------------------------------------------------
// helpers
//--------------------------------------------------------------------------------

function _redirOnError(m, clearQuery) {
  if (m) {
    getSession().accountFormError = m;

    var dest = request.url;
    if (clearQuery) {
      dest = request.path;
    }
    response.redirect(dest);
  }
}

function setSigninNotice(m) {
  getSession().accountSigninNotice = m;
}

function setSessionError(m) {
  getSession().accountFormError = m;
}

function _topDiv(id, name) {
  var m = getSession()[name];
  if (m) {
    delete getSession()[name];
    return DIV({id: id}, m);
  } else {
    return '';
  }
}

function _messageDiv() { return _topDiv('account-message', 'accountMessage'); }
function _errorDiv() { return _topDiv('account-error', 'accountFormError'); }
function _signinNoticeDiv() { return _topDiv('signin-notice', 'accountSigninNotice'); }

function _renderTemplate(name, data) {
  data.messageDiv = _messageDiv;
  data.errorDiv = _errorDiv;
  data.signinNotice = _signinNoticeDiv;
  data.tempFormData = getSession().tempFormData;
  renderFramed('pro/account/'+name+'.ejs', data);
}

//----------------------------------------------------------------
// /ep/account/
//----------------------------------------------------------------

function render_main_get() {
  _renderTemplate('my-account', {
    account: getSessionProAccount(),
    changePass: getSession().changePass
  });
}

function render_update_info_get() {
  response.redirect('/ep/account/');
}

function render_update_info_post() {
  var fullName = request.params.fullName;
  var email = trim(request.params.email);

  getSession().tempFormData.email = email;
  getSession().tempFormData.fullName = fullName;

  _redirOnError(pro_accounts.validateEmail(email));
  _redirOnError(pro_accounts.validateFullName(fullName));
  
  pro_accounts.setEmail(getSessionProAccount(), email);
  pro_accounts.setFullName(getSessionProAccount(), fullName);

  getSession().accountMessage = "Info updated.";
  response.redirect('/ep/account/');
}

function render_update_password_get() {
  response.redirect('/ep/account/');
}

function render_update_password_post() {
  var password = request.params.password;
  var passwordConfirm = request.params.passwordConfirm;

  if (password != passwordConfirm) { _redirOnError('Passwords did not match.'); }

  _redirOnError(pro_accounts.validatePassword(password));
  
  pro_accounts.setPassword(getSessionProAccount(), password);

  if (getSession().changePass) {
    delete getSession().changePass;
    response.redirect('/');
  }

  getSession().accountMessage = "Password updated.";
  response.redirect('/ep/account/');
}

//--------------------------------------------------------------------------------
// signin/signout
//--------------------------------------------------------------------------------

function render_sign_in_get() {
  if (request.params.uid && request.params.tp) {
    var m = pro_accounts.authenticateTempSignIn(Number(request.params.uid), request.params.tp);
    if (m) {
      getSession().accountFormError = m;
      response.redirect('/ep/account/');
    }
  }
  if (request.params.instantSigninKey) {
    _attemptInstantSignin(request.params.instantSigninKey);
  }
  if (getSession().recentlySignedOut && getSession().accountFormError) {
    delete getSession().accountFormError;
    delete getSession().recentlySignedOut;
  }
  // Note: must check isAccountSignedIn before calling checkAutoSignin()!
  if (pro_accounts.isAccountSignedIn()) {
    _redirectToPostSigninDestination();
  }
  pro_account_auto_signin.checkAutoSignin();
  var domainRecord = domains.getRequestDomainRecord();
  var showGuestBox = false;
  if (request.params.guest && request.params.padId) {
    showGuestBox = true;
  }
  _renderTemplate('signin', {
    domain: pro_utils.getFullProDomain(),
    siteName: toHTML(pro_config.getConfig().siteName),
    email: getSession().tempFormData.email || "",
    password: getSession().tempFormData.password || "",
    rememberMe: getSession().tempFormData.rememberMe || false,
    showGuestBox: showGuestBox,
    localPadId: request.params.padId
  });
}

function _attemptInstantSignin(key) {
  // See src/etherpad/control/global_pro_account_control.js
  var email = null;
  var password = null;
  syncedWithCache('global_signin_passwords', function(c) {
    if (c[key]) {
      email = c[key].email;
      password = c[key].password;
    }
    delete c[key];
  });
  getSession().tempFormData.email = email;
  _redirOnError(pro_accounts.authenticateSignIn(email, password), true);
}

function render_sign_in_post() {
  var email = trim(request.params.email);
  var password = request.params.password;

  getSession().tempFormData.email = email;
  getSession().tempFormData.rememberMe = request.params.rememberMe;

  _redirOnError(pro_accounts.authenticateSignIn(email, password));
  pro_account_auto_signin.setAutoSigninCookie(request.params.rememberMe);
  _redirectToPostSigninDestination();
}

function render_guest_sign_in_get() {
  var localPadId = request.params.padId;
  var domainId = domains.getRequestDomainId();
  var globalPadId = padutils.makeGlobalId(domainId, localPadId);
  var userId = padusers.getUserId();

  pro_account_auto_signin.checkAutoSignin();
  pad_security.clearKnockStatus(userId, globalPadId);

  _renderTemplate('signin-guest', {
    localPadId: localPadId,
    errorMessage: getSession().guestAccessError,
    siteName: toHTML(pro_config.getConfig().siteName),
    guestName: padusers.getUserName() || ""
  });
}

function render_guest_sign_in_post() {
  function _err(m) {
    if (m) {
      getSession().guestAccessError = m;
      response.redirect(request.url);
    }
  }
  var displayName = request.params.guestDisplayName;
  var localPadId = request.params.localPadId;
  if (!(displayName && displayName.length > 0)) {
    _err("Please enter a display name");
  }
  getSession().guestDisplayName = displayName;
  response.redirect('/ep/account/guest-knock?padId='+encodeURIComponent(localPadId)+
    "&guestDisplayName="+encodeURIComponent(displayName));
}

function render_guest_knock_get() {
  var localPadId = request.params.padId;
  helpers.addClientVars({
    localPadId: localPadId,
    guestDisplayName: request.params.guestDisplayName,
    padUrl: "http://"+httpHost(request.host)+"/"+localPadId
  });
  _renderTemplate('guest-knock', {});
}

function render_guest_knock_post() {
  var localPadId = request.params.padId;
  var displayName = request.params.guestDisplayName;
  var domainId = domains.getRequestDomainId();
  var globalPadId = padutils.makeGlobalId(domainId, localPadId);
  var userId = padusers.getUserId();

  response.setContentType("text/plain; charset=utf-8");
  // has the knock already been answsered?
  var currentAnswer = pad_security.getKnockAnswer(userId, globalPadId);
  if (currentAnswer) {
    response.write(currentAnswer);
  } else {
    collab_server.guestKnock(globalPadId, userId, displayName);
    response.write("wait");
  }
}

function _redirectToPostSigninDestination() {
  var cont = request.params.cont;
  if (!cont) { cont = '/'; }
  response.redirect(cont);
}

function render_sign_out() {
  pro_account_auto_signin.setAutoSigninCookie(false);
  pro_accounts.signOut();
  delete getSession().padPasswordAuth;
  getSession().recentlySignedOut = true;
  response.redirect("/");
}

//--------------------------------------------------------------------------------
// create-admin-account (eepnet only)
//--------------------------------------------------------------------------------

function render_create_admin_account_get() {
  if (pro_accounts.doesAdminExist()) {
    renderFramedError("An admin account already exists on this domain.");
    response.stop();
  }
  _renderTemplate('create-admin-account', {});
}

function render_create_admin_account_post() {
  var email = trim(request.params.email);
  var password = request.params.password;
  var passwordConfirm = request.params.passwordConfirm;
  var fullName = request.params.fullName;

  getSession().tempFormData.email = email;
  getSession().tempFormData.fullName = fullName;

  if (password != passwordConfirm) { _redirOnError('Passwords did not match.'); }

  _redirOnError(pro_accounts.validateEmail(email));
  _redirOnError(pro_accounts.validateFullName(fullName));
  _redirOnError(pro_accounts.validatePassword(password));

  pro_accounts.createNewAccount(null, fullName, email, password, true);

  var u = pro_accounts.getAccountByEmail(email, null);

  // TODO: should we send a welcome email here?
  //pro_accounts.sendWelcomeEmail(u);

  _redirOnError(pro_accounts.authenticateSignIn(email, password));

  response.redirect("/");
}


//--------------------------------------------------------------------------------
// forgot password
//--------------------------------------------------------------------------------

function render_forgot_password_get() {
  if (request.params.instantSubmit && request.params.email) {
    render_forgot_password_post();
  } else {
    _renderTemplate('forgot-password', {
      email: getSession().tempFormData.email || ""
    });
  }
}

function render_forgot_password_post() {
  var email = trim(request.params.email);

  getSession().tempFormData.email = email;

  var u = pro_accounts.getAccountByEmail(email, null);
  if (!u) {
    _redirOnError("Account not found: "+email);
  }

  var tempPass = stringutils.randomString(10);
  pro_accounts.setTempPassword(u, tempPass);

  var subj = "EtherPad: Request to reset your password on "+request.domain;
  var body = renderTemplateAsString('pro/account/forgot-password-email.ejs', {
    account: u,
    recoverUrl: pro_accounts.getTempSigninUrl(u, tempPass)
  });
  var fromAddr = pro_utils.getEmailFromAddr();
  sendEmail(u.email, fromAddr, subj, {}, body);

  getSession().accountMessage = "An email has been sent to "+u.email+" with instructions to reset the password.";
  response.redirect(request.path);
}