aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/control/global_pro_account_control.js
blob: 65d212480a15e78b08182d413c01d30696e0f794 (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
/**
 * 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("funhtml.*");
import("stringutils");
import("stringutils.*");
import("email.sendEmail");
import("cache_utils.syncedWithCache");

import("etherpad.utils.*");
import("etherpad.sessions.getSession");

import("etherpad.pro.domains");
import("etherpad.pro.pro_accounts");
import("etherpad.pro.pro_utils");

jimport("java.lang.System.out.println");

function onRequest() {
  if (!getSession().oldFormData) {
    getSession().oldFormData = {};
  }
  return false;  // not handled yet.
}

function _errorDiv() {
  var m = getSession().proAccountControlError;
  delete getSession().proAccountControlError;
  if (m) {
    return DIV({className: "error"}, m);
  }
  return "";
}

function _redirectError(m) {
  getSession().proAccountControlError = m;
  response.redirect(request.path);
}


function render_main_get() {
  response.redirect('/ep/pro-account/sign-in');
}

function render_sign_in_get() {
  renderFramed('pro-account/sign-in.ejs', {
    oldData: getSession().oldFormData,
    errorDiv: _errorDiv
  });
}


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

  subDomain = subDomain.toLowerCase();

  getSession().oldFormData.email = email;
  getSession().oldFormData.subDomain = subDomain;

  var domainRecord = domains.getDomainRecordFromSubdomain(subDomain);
  if (!domainRecord) {
    _redirectError("Site address not found: "+subDomain+"."+request.host);
  }

  var instantSigninKey = stringutils.randomString(20);
  syncedWithCache('global_signin_passwords', function(c) {
    c[instantSigninKey] = {
      email: email,
      password: password
    };
  });

  response.redirect(
    "https://"+subDomain+"."+httpsHost(request.host)+
    "/ep/account/sign-in?instantSigninKey="+instantSigninKey);
}

function render_recover_get() {
  renderFramed('pro-account/recover.ejs', {
    oldData: getSession().oldFormData,
    errorDiv: _errorDiv
  });
}

function render_recover_post() {

  function _recoverLink(accountRecord, domainRecord) {
    var host = (domainRecord.subDomain + "." + httpsHost(request.host));
    return (
      "https://"+host+"/ep/account/forgot-password?instantSubmit=1&email="+
      encodeURIComponent(accountRecord.email));
  }

  var email = trim(request.params.email);

  // lookup all domains associated with this email
  var accountList = pro_accounts.getAllAccountsWithEmail(email);
  println("account records matching ["+email+"]: "+accountList.length);

  var domainList = [];
  for (var i = 0; i < accountList.length; i++) {
    domainList[i] = domains.getDomainRecord(accountList[i].domainId);
  }

  if (accountList.length == 0) {
    _redirectError("No accounts were found associated with the email address \""+email+"\".");
  }
  if (accountList.length == 1) {
    response.redirect(_recoverLink(accountList[0], domainList[0]));
  }
  if (accountList.length > 1) {
    var fromAddr = '"EtherPad" <noreply@pad.spline.inf.fu-berlin.de>';
    var subj = "EtherPad: account information";
    var body = renderTemplateAsString(
      'pro/account/global-multi-domain-recover-email.ejs', {
        accountList: accountList,
        domainList: domainList,
        recoverLink: _recoverLink,
        email: email
      }
    );
    sendEmail(email, fromAddr, subj, {}, body);
    pro_utils.renderFramedMessage("Instructions have been sent to "+email+".");
  }
}