aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/control/pro_signup_control.js
blob: 6bf7cc388a127cc0fb7bf925f7521c9abd041748 (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
/**
 * 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("jsutils.*");
import("cache_utils.syncedWithCache");
import("funhtml.*");
import("stringutils");
import("stringutils.*");
import("sqlbase.sqlcommon");

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

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

import("etherpad.control.pro_beta_control");
import("etherpad.control.pro.admin.account_manager_control");

import("etherpad.helpers");

function onRequest() {
  if (!getSession().ods) {
    getSession().ods = {};
  }
  if (request.method == "POST") {
    // add params to cart
    eachProperty(request.params, function(k,v) {
      getSession().ods[k] = stringutils.toHTML(v);
    });
  }
}

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

function _input(id, type) {
  return INPUT({type: type ? type : 'text', name: id, id: id,
                value: getSession().ods[id] || ""});
}

function _inf(id, label, type) {
  return DIV(
    DIV({style: "width: 100px; text-align: right; float: left; padding-top: 3px;"}, label, ":  "),
    DIV({style: "text-align: left; float: left;"},
        _input(id, type)),
    DIV({style: "height: 6px; clear: both;"}, " "));
}

function render_main_get() {
  // observe activation code
  if (request.params.sc) {
    getSession().betaActivationCode = request.params.sc;
    response.redirect(request.path);
  }

  // validate activation code
  var activationCode = getSession().betaActivationCode;
  var err = pro_beta_control.isValidCode(activationCode);
  if (err) {
    renderNoticeString(DIV({style: "border: 1px solid red; background: #fdd; font-weight: bold; padding: 1em;"},
      err));
    response.stop();
  }

  // serve activation page
  renderFramed('main/pro_signup_body.ejs', {
    errorDiv: _errorDiv,
    input: _input,
    inf: _inf
  });
}

function _err(m) {
  if (m) {
    getSession().errorMessage = m;
    response.redirect(request.path);
  }
}

function render_main_post() {
  var subdomain = trim(String(request.params.subdomain).toLowerCase());
  var fullName = request.params.fullName;
  var email = trim(request.params.email);

  // validate activation code
  var activationCode = getSession().betaActivationCode;
  var err = pro_beta_control.isValidCode(activationCode);
  if (err) {
    resonse.write(err);
  }

  /*
  var password = request.params.password;
  var passwordConfirm = request.params.passwordConfirm;
  */
  var orgName = subdomain;

  //---- basic validation ----
  if (!/^\w[\w\d\-]*$/.test(subdomain)) {
    _err("Invalid domain: "+subdomain);
  }
  if (subdomain.length < 2) {
    _err("Subdomain must be at least 2 characters.");
  }
  if (subdomain.length > 60) {
    _err("Subdomain must be <= 60 characters.");
  }

/*
  if (password != passwordConfirm) {
    _err("Passwords do not match.");
  }
  */

  _err(pro_accounts.validateFullName(fullName));
  _err(pro_accounts.validateEmail(email));

  if (!(email.match(/[Ff][Uu]-[Bb][Ee][Rr][Ll][Ii][Nn].[Dd][Ee]$/))) { _err("Please use your *.fu-berlin.de email address."); }
//  _err(pro_accounts.validatePassword(password));

  //---- database validation ----

  if (domains.doesSubdomainExist(subdomain)) {
    _err("The domain "+subdomain+" is already in use.");
  }

  //---- looks good.  create records! ----

  // TODO: log a bunch of stuff, and request IP address, etc.

  var ok = false;
  sqlcommon.inTransaction(function() {
    var tempPass = stringutils.randomString(10);
    // TODO: move validation code into domains.createNewSubdomain...
    var domainId = domains.createNewSubdomain(subdomain, orgName);
    var accountId = pro_accounts.createNewAccount(domainId, fullName, email, tempPass, true);
    // send welcome email
    syncedWithCache('pro-activations', function(c) {
      c[domainId] = true;
    });
    ok = true;
    if (activationCode) {
      pro_beta_control.notifyActivated(activationCode);
    }
  });

  if (ok) {
    response.redirect('http://'+subdomain+"."+request.host+'/ep/finish-activation');
  } else {
    response.write("There was an error processing your request.");
  }
}