~davewalker/etherpad/ubuntu-unlimited-max-users-and-revisions

« back to all changes in this revision

Viewing changes to etherpad/src/etherpad/control/pro/account_control.js

  • Committer: James Page
  • Date: 2011-04-13 08:00:43 UTC
  • Revision ID: james.page@canonical.com-20110413080043-eee2nq7y1v7cv2mp
* Refactoring to use native Ubuntu Java libraries. 
* debian/control:
  - use openjdk instead of sun's java
  - update maintainer
* debian/etherpad.init.orig, debian/etherpad.upstart:
  - move the init script out of the way
  - create a basic upstart script
  - note that the open office document conversion daemon was dropped
    from the upstart configuration; if this behavior is desired, please
    create a separate upstart job for it
* debian/rules:
  - just use basic dh_installinit, as it will pick up the new upstart job
* New release
* Changed maintainer to Packaging
* Fixed installation scripts
* Initial Release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2009 Google Inc.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS-IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
import("stringutils");
 
18
import("stringutils.*");
 
19
import("funhtml.*");
 
20
import("email.sendEmail");
 
21
import("cache_utils.syncedWithCache");
 
22
 
 
23
import("etherpad.helpers");
 
24
import("etherpad.utils.*");
 
25
import("etherpad.sessions.getSession");
 
26
import("etherpad.pro.pro_accounts");
 
27
import("etherpad.pro.pro_accounts.getSessionProAccount");
 
28
import("etherpad.pro.domains");
 
29
import("etherpad.pro.pro_utils");
 
30
import("etherpad.pro.pro_account_auto_signin");
 
31
import("etherpad.pro.pro_config");
 
32
import("etherpad.pad.pad_security");
 
33
import("etherpad.pad.padutils");
 
34
import("etherpad.pad.padusers");
 
35
import("etherpad.collab.collab_server");
 
36
 
 
37
function onRequest() {
 
38
  if (!getSession().tempFormData) {
 
39
    getSession().tempFormData = {};
 
40
  }
 
41
 
 
42
  return false; // path not handled here
 
43
}
 
44
 
 
45
//--------------------------------------------------------------------------------
 
46
// helpers
 
47
//--------------------------------------------------------------------------------
 
48
 
 
49
function _redirOnError(m, clearQuery) {
 
50
  if (m) {
 
51
    getSession().accountFormError = m;
 
52
 
 
53
    var dest = request.url;
 
54
    if (clearQuery) {
 
55
      dest = request.path;
 
56
    }
 
57
    response.redirect(dest);
 
58
  }
 
59
}
 
60
 
 
61
function setSigninNotice(m) {
 
62
  getSession().accountSigninNotice = m;
 
63
}
 
64
 
 
65
function setSessionError(m) {
 
66
  getSession().accountFormError = m;
 
67
}
 
68
 
 
69
function _topDiv(id, name) {
 
70
  var m = getSession()[name];
 
71
  if (m) {
 
72
    delete getSession()[name];
 
73
    return DIV({id: id}, m);
 
74
  } else {
 
75
    return '';
 
76
  }
 
77
}
 
78
 
 
79
function _messageDiv() { return _topDiv('account-message', 'accountMessage'); }
 
80
function _errorDiv() { return _topDiv('account-error', 'accountFormError'); }
 
81
function _signinNoticeDiv() { return _topDiv('signin-notice', 'accountSigninNotice'); }
 
82
 
 
83
function _renderTemplate(name, data) {
 
84
  data.messageDiv = _messageDiv;
 
85
  data.errorDiv = _errorDiv;
 
86
  data.signinNotice = _signinNoticeDiv;
 
87
  data.tempFormData = getSession().tempFormData;
 
88
  renderFramed('pro/account/'+name+'.ejs', data);
 
89
}
 
90
 
 
91
//----------------------------------------------------------------
 
92
// /ep/account/
 
93
//----------------------------------------------------------------
 
94
 
 
95
function render_main_get() {
 
96
  _renderTemplate('my-account', {
 
97
    account: getSessionProAccount(),
 
98
    changePass: getSession().changePass
 
99
  });
 
100
}
 
101
 
 
102
function render_update_info_get() {
 
103
  response.redirect('/ep/account/');
 
104
}
 
105
 
 
106
function render_update_info_post() {
 
107
  var fullName = request.params.fullName;
 
108
  var email = trim(request.params.email);
 
109
 
 
110
  getSession().tempFormData.email = email;
 
111
  getSession().tempFormData.fullName = fullName;
 
112
 
 
113
  _redirOnError(pro_accounts.validateEmail(email));
 
114
  _redirOnError(pro_accounts.validateFullName(fullName));
 
115
  
 
116
  pro_accounts.setEmail(getSessionProAccount(), email);
 
117
  pro_accounts.setFullName(getSessionProAccount(), fullName);
 
118
 
 
119
  getSession().accountMessage = "Info updated.";
 
120
  response.redirect('/ep/account/');
 
121
}
 
122
 
 
123
function render_update_password_get() {
 
124
  response.redirect('/ep/account/');
 
125
}
 
126
 
 
127
function render_update_password_post() {
 
128
  var password = request.params.password;
 
129
  var passwordConfirm = request.params.passwordConfirm;
 
130
 
 
131
  if (password != passwordConfirm) { _redirOnError('Passwords did not match.'); }
 
132
 
 
133
  _redirOnError(pro_accounts.validatePassword(password));
 
134
  
 
135
  pro_accounts.setPassword(getSessionProAccount(), password);
 
136
 
 
137
  if (getSession().changePass) {
 
138
    delete getSession().changePass;
 
139
    response.redirect('/');
 
140
  }
 
141
 
 
142
  getSession().accountMessage = "Password updated.";
 
143
  response.redirect('/ep/account/');
 
144
}
 
145
 
 
146
//--------------------------------------------------------------------------------
 
147
// signin/signout
 
148
//--------------------------------------------------------------------------------
 
149
 
 
150
function render_sign_in_get() {
 
151
  if (request.params.uid && request.params.tp) {
 
152
    var m = pro_accounts.authenticateTempSignIn(Number(request.params.uid), request.params.tp);
 
153
    if (m) {
 
154
      getSession().accountFormError = m;
 
155
      response.redirect('/ep/account/');
 
156
    }
 
157
  }
 
158
  if (request.params.instantSigninKey) {
 
159
    _attemptInstantSignin(request.params.instantSigninKey);
 
160
  }
 
161
  if (getSession().recentlySignedOut && getSession().accountFormError) {
 
162
    delete getSession().accountFormError;
 
163
    delete getSession().recentlySignedOut;
 
164
  }
 
165
  // Note: must check isAccountSignedIn before calling checkAutoSignin()!
 
166
  if (pro_accounts.isAccountSignedIn()) {
 
167
    _redirectToPostSigninDestination();
 
168
  }
 
169
  pro_account_auto_signin.checkAutoSignin();
 
170
  var domainRecord = domains.getRequestDomainRecord();
 
171
  var showGuestBox = false;
 
172
  if (request.params.guest && request.params.padId) {
 
173
    showGuestBox = true;
 
174
  }
 
175
  _renderTemplate('signin', {
 
176
    domain: pro_utils.getFullProDomain(),
 
177
    siteName: toHTML(pro_config.getConfig().siteName),
 
178
    email: getSession().tempFormData.email || "",
 
179
    password: getSession().tempFormData.password || "",
 
180
    rememberMe: getSession().tempFormData.rememberMe || false,
 
181
    showGuestBox: showGuestBox,
 
182
    localPadId: request.params.padId
 
183
  });
 
184
}
 
185
 
 
186
function _attemptInstantSignin(key) {
 
187
  // See src/etherpad/control/global_pro_account_control.js
 
188
  var email = null;
 
189
  var password = null;
 
190
  syncedWithCache('global_signin_passwords', function(c) {
 
191
    if (c[key]) {
 
192
      email = c[key].email;
 
193
      password = c[key].password;
 
194
    }
 
195
    delete c[key];
 
196
  });
 
197
  getSession().tempFormData.email = email;
 
198
  _redirOnError(pro_accounts.authenticateSignIn(email, password), true);
 
199
}
 
200
 
 
201
function render_sign_in_post() {
 
202
  var email = trim(request.params.email);
 
203
  var password = request.params.password;
 
204
 
 
205
  getSession().tempFormData.email = email;
 
206
  getSession().tempFormData.rememberMe = request.params.rememberMe;
 
207
 
 
208
  _redirOnError(pro_accounts.authenticateSignIn(email, password));
 
209
  pro_account_auto_signin.setAutoSigninCookie(request.params.rememberMe);
 
210
  _redirectToPostSigninDestination();
 
211
}
 
212
 
 
213
function render_guest_sign_in_get() {
 
214
  var localPadId = request.params.padId;
 
215
  var domainId = domains.getRequestDomainId();
 
216
  var globalPadId = padutils.makeGlobalId(domainId, localPadId);
 
217
  var userId = padusers.getUserId();
 
218
 
 
219
  pro_account_auto_signin.checkAutoSignin();
 
220
  pad_security.clearKnockStatus(userId, globalPadId);
 
221
 
 
222
  _renderTemplate('signin-guest', {
 
223
    localPadId: localPadId,
 
224
    errorMessage: getSession().guestAccessError,
 
225
    siteName: toHTML(pro_config.getConfig().siteName),
 
226
    guestName: padusers.getUserName() || ""
 
227
  });
 
228
}
 
229
 
 
230
function render_guest_sign_in_post() {
 
231
  function _err(m) {
 
232
    if (m) {
 
233
      getSession().guestAccessError = m;
 
234
      response.redirect(request.url);
 
235
    }
 
236
  }
 
237
  var displayName = request.params.guestDisplayName;
 
238
  var localPadId = request.params.localPadId;
 
239
  if (!(displayName && displayName.length > 0)) {
 
240
    _err("Please enter a display name");
 
241
  }
 
242
  getSession().guestDisplayName = displayName;
 
243
  response.redirect('/ep/account/guest-knock?padId='+encodeURIComponent(localPadId)+
 
244
    "&guestDisplayName="+encodeURIComponent(displayName));
 
245
}
 
246
 
 
247
function render_guest_knock_get() {
 
248
  var localPadId = request.params.padId;
 
249
  helpers.addClientVars({
 
250
    localPadId: localPadId,
 
251
    guestDisplayName: request.params.guestDisplayName,
 
252
    padUrl: "http://"+httpHost(request.host)+"/"+localPadId
 
253
  });
 
254
  _renderTemplate('guest-knock', {});
 
255
}
 
256
 
 
257
function render_guest_knock_post() {
 
258
  var localPadId = request.params.padId;
 
259
  var displayName = request.params.guestDisplayName;
 
260
  var domainId = domains.getRequestDomainId();
 
261
  var globalPadId = padutils.makeGlobalId(domainId, localPadId);
 
262
  var userId = padusers.getUserId();
 
263
 
 
264
  response.setContentType("text/plain; charset=utf-8");
 
265
  // has the knock already been answsered?
 
266
  var currentAnswer = pad_security.getKnockAnswer(userId, globalPadId);
 
267
  if (currentAnswer) {
 
268
    response.write(currentAnswer);
 
269
  } else {
 
270
    collab_server.guestKnock(globalPadId, userId, displayName);
 
271
    response.write("wait");
 
272
  }
 
273
}
 
274
 
 
275
function _redirectToPostSigninDestination() {
 
276
  var cont = request.params.cont;
 
277
  if (!cont) { cont = '/'; }
 
278
  response.redirect(cont);
 
279
}
 
280
 
 
281
function render_sign_out() {
 
282
  pro_account_auto_signin.setAutoSigninCookie(false);
 
283
  pro_accounts.signOut();
 
284
  delete getSession().padPasswordAuth;
 
285
  getSession().recentlySignedOut = true;
 
286
  response.redirect("/");
 
287
}
 
288
 
 
289
//--------------------------------------------------------------------------------
 
290
// create-admin-account (eepnet only)
 
291
//--------------------------------------------------------------------------------
 
292
 
 
293
function render_create_admin_account_get() {
 
294
  if (pro_accounts.doesAdminExist()) {
 
295
    renderFramedError("An admin account already exists on this domain.");
 
296
    response.stop();
 
297
  }
 
298
  _renderTemplate('create-admin-account', {});
 
299
}
 
300
 
 
301
function render_create_admin_account_post() {
 
302
  var email = trim(request.params.email);
 
303
  var password = request.params.password;
 
304
  var passwordConfirm = request.params.passwordConfirm;
 
305
  var fullName = request.params.fullName;
 
306
 
 
307
  getSession().tempFormData.email = email;
 
308
  getSession().tempFormData.fullName = fullName;
 
309
 
 
310
  if (password != passwordConfirm) { _redirOnError('Passwords did not match.'); }
 
311
 
 
312
  _redirOnError(pro_accounts.validateEmail(email));
 
313
  _redirOnError(pro_accounts.validateFullName(fullName));
 
314
  _redirOnError(pro_accounts.validatePassword(password));
 
315
 
 
316
  pro_accounts.createNewAccount(null, fullName, email, password, true);
 
317
 
 
318
  var u = pro_accounts.getAccountByEmail(email, null);
 
319
 
 
320
  // TODO: should we send a welcome email here?
 
321
  //pro_accounts.sendWelcomeEmail(u);
 
322
 
 
323
  _redirOnError(pro_accounts.authenticateSignIn(email, password));
 
324
 
 
325
  response.redirect("/");
 
326
}
 
327
 
 
328
 
 
329
//--------------------------------------------------------------------------------
 
330
// forgot password
 
331
//--------------------------------------------------------------------------------
 
332
 
 
333
function render_forgot_password_get() {
 
334
  if (request.params.instantSubmit && request.params.email) {
 
335
    render_forgot_password_post();
 
336
  } else {
 
337
    _renderTemplate('forgot-password', {
 
338
      email: getSession().tempFormData.email || ""
 
339
    });
 
340
  }
 
341
}
 
342
 
 
343
function render_forgot_password_post() {
 
344
  var email = trim(request.params.email);
 
345
 
 
346
  getSession().tempFormData.email = email;
 
347
 
 
348
  var u = pro_accounts.getAccountByEmail(email, null);
 
349
  if (!u) {
 
350
    _redirOnError("Account not found: "+email);
 
351
  }
 
352
 
 
353
  var tempPass = stringutils.randomString(10);
 
354
  pro_accounts.setTempPassword(u, tempPass);
 
355
 
 
356
  var subj = "EtherPad: Request to reset your password on "+request.domain;
 
357
  var body = renderTemplateAsString('pro/account/forgot-password-email.ejs', {
 
358
    account: u,
 
359
    recoverUrl: pro_accounts.getTempSigninUrl(u, tempPass)
 
360
  });
 
361
  var fromAddr = pro_utils.getEmailFromAddr();
 
362
  sendEmail(u.email, fromAddr, subj, {}, body);
 
363
 
 
364
  getSession().accountMessage = "An email has been sent to "+u.email+" with instructions to reset the password.";
 
365
  response.redirect(request.path);
 
366
}
 
367
 
 
368
 
 
369