@nymphjs/tilmeld 1.0.0-beta.0 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/User.js CHANGED
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -21,30 +12,23 @@ const guid_1 = require("@nymphjs/guid");
21
12
  const AbleObject_1 = __importDefault(require("./AbleObject"));
22
13
  const errors_1 = require("./errors");
23
14
  class User extends AbleObject_1.default {
24
- static factory(guid) {
25
- const _super = Object.create(null, {
26
- factory: { get: () => super.factory }
27
- });
28
- return __awaiter(this, void 0, void 0, function* () {
29
- return (yield _super.factory.call(this, guid));
30
- });
15
+ static async factory(guid) {
16
+ return (await super.factory(guid));
31
17
  }
32
- static factoryUsername(username) {
33
- return __awaiter(this, void 0, void 0, function* () {
34
- const entity = new this();
35
- if (username != null) {
36
- const entity = yield this.nymph.getEntity({
37
- class: this,
38
- }, {
39
- type: '&',
40
- ilike: ['username', username.replace(/([\\%_])/g, (s) => `\\${s}`)],
41
- });
42
- if (entity != null) {
43
- return entity;
44
- }
18
+ static async factoryUsername(username) {
19
+ const entity = new this();
20
+ if (username != null) {
21
+ const entity = await this.nymph.getEntity({
22
+ class: this,
23
+ }, {
24
+ type: '&',
25
+ ilike: ['username', username.replace(/([\\%_])/g, (s) => `\\${s}`)],
26
+ });
27
+ if (entity != null) {
28
+ return entity;
45
29
  }
46
- return entity;
47
- });
30
+ }
31
+ return entity;
48
32
  }
49
33
  static factorySync(guid) {
50
34
  return super.factorySync(guid);
@@ -70,117 +54,111 @@ class User extends AbleObject_1.default {
70
54
  }
71
55
  return this.tilmeld.currentUser;
72
56
  }
73
- static sendRecovery(data) {
74
- var _a, _b, _c, _d;
75
- return __awaiter(this, void 0, void 0, function* () {
76
- if (!this.tilmeld.config.pwRecovery) {
77
- return {
78
- result: false,
79
- message: 'Account recovery is not allowed.',
80
- };
81
- }
82
- let user;
83
- const options = {};
84
- if (!this.tilmeld.config.emailUsernames &&
85
- data.recoveryType === 'username') {
86
- const getUser = yield this.nymph.getEntity({ class: this.tilmeld.User, skipAc: true }, {
87
- type: '&',
88
- ilike: ['email', data.account.replace(/([\\%_])/g, (s) => `\\${s}`)],
89
- });
90
- if (getUser == null) {
91
- return {
92
- result: false,
93
- message: 'Requested account is not accessible.',
94
- };
95
- }
96
- user = getUser;
97
- options.template = 'RecoverUsername';
98
- options.message = {
99
- to: {
100
- name: (_a = user.name) !== null && _a !== void 0 ? _a : '',
101
- address: (_b = user.email) !== null && _b !== void 0 ? _b : '',
102
- },
103
- };
104
- options.locals = {};
105
- }
106
- else if (data.recoveryType === 'password') {
107
- const getUser = yield this.tilmeld.User.factoryUsername(data.account);
108
- if (getUser.guid == null) {
109
- return {
110
- result: false,
111
- message: 'Requested account is not accessible.',
112
- };
113
- }
114
- getUser.recoverSecret = (0, guid_1.humanSecret)();
115
- getUser.recoverSecretDate = Date.now();
116
- if (!(yield getUser.$saveSkipAC())) {
117
- return { result: false, message: "Couldn't save recovery secret." };
118
- }
119
- user = getUser;
120
- options.template = 'RecoverPassword';
121
- options.message = {
122
- to: {
123
- name: (_c = user.name) !== null && _c !== void 0 ? _c : '',
124
- address: (_d = user.email) !== null && _d !== void 0 ? _d : '',
125
- },
126
- };
127
- options.locals = {
128
- recoverCode: getUser.recoverSecret,
129
- timeLimit: this.tilmeld.config.pwRecoveryTimeLimit,
130
- };
131
- }
132
- else {
133
- return { result: false, message: 'Invalid recovery type.' };
134
- }
135
- if (yield this.tilmeld.config.sendEmail(this.tilmeld, options, user)) {
136
- return {
137
- result: true,
138
- message: "We've sent an email to your registered address. Please " +
139
- 'check your email to continue with account recovery.',
140
- };
141
- }
142
- else {
143
- return { result: false, message: "Couldn't send recovery email." };
144
- }
145
- });
146
- }
147
- static recover(data) {
148
- var _a;
149
- return __awaiter(this, void 0, void 0, function* () {
150
- if (!this.tilmeld.config.pwRecovery) {
57
+ static async sendRecovery(data) {
58
+ if (!this.tilmeld.config.pwRecovery) {
59
+ return {
60
+ result: false,
61
+ message: 'Account recovery is not allowed.',
62
+ };
63
+ }
64
+ let user;
65
+ const options = {};
66
+ if (!this.tilmeld.config.emailUsernames &&
67
+ data.recoveryType === 'username') {
68
+ const getUser = await this.nymph.getEntity({ class: this.tilmeld.User, skipAc: true }, {
69
+ type: '&',
70
+ ilike: ['email', data.account.replace(/([\\%_])/g, (s) => `\\${s}`)],
71
+ });
72
+ if (getUser == null) {
151
73
  return {
152
74
  result: false,
153
- message: 'Account recovery is not allowed.',
75
+ message: 'Requested account is not accessible.',
154
76
  };
155
77
  }
156
- const user = yield this.tilmeld.User.factoryUsername(data.username);
157
- if (user.guid == null ||
158
- user.recoverSecret == null ||
159
- data.secret !== user.recoverSecret ||
160
- (0, strtotime_1.default)('+' + this.tilmeld.config.pwRecoveryTimeLimit, Math.floor(((_a = user.recoverSecretDate) !== null && _a !== void 0 ? _a : 0) / 1000)) *
161
- 1000 <
162
- Date.now()) {
78
+ user = getUser;
79
+ options.template = 'RecoverUsername';
80
+ options.message = {
81
+ to: {
82
+ name: user.name ?? '',
83
+ address: user.email ?? '',
84
+ },
85
+ };
86
+ options.locals = {};
87
+ }
88
+ else if (data.recoveryType === 'password') {
89
+ const getUser = await this.tilmeld.User.factoryUsername(data.account);
90
+ if (getUser.guid == null) {
163
91
  return {
164
92
  result: false,
165
- message: 'The secret code does not match.',
166
- };
167
- }
168
- if (!('password' in data) || !data.password.length) {
169
- return { result: false, message: 'Password cannot be empty.' };
170
- }
171
- user.$password(data.password);
172
- delete user.recoverSecret;
173
- delete user.recoverSecretDate;
174
- if (yield user.$saveSkipAC()) {
175
- return {
176
- result: true,
177
- message: 'Your password has been reset. You can now log in using your new password.',
93
+ message: 'Requested account is not accessible.',
178
94
  };
179
95
  }
180
- else {
181
- return { result: false, message: 'Error saving new password.' };
182
- }
183
- });
96
+ getUser.recoverSecret = (0, guid_1.humanSecret)();
97
+ getUser.recoverSecretDate = Date.now();
98
+ if (!(await getUser.$saveSkipAC())) {
99
+ return { result: false, message: "Couldn't save recovery secret." };
100
+ }
101
+ user = getUser;
102
+ options.template = 'RecoverPassword';
103
+ options.message = {
104
+ to: {
105
+ name: user.name ?? '',
106
+ address: user.email ?? '',
107
+ },
108
+ };
109
+ options.locals = {
110
+ recoverCode: getUser.recoverSecret,
111
+ timeLimit: this.tilmeld.config.pwRecoveryTimeLimit,
112
+ };
113
+ }
114
+ else {
115
+ return { result: false, message: 'Invalid recovery type.' };
116
+ }
117
+ if (await this.tilmeld.config.sendEmail(this.tilmeld, options, user)) {
118
+ return {
119
+ result: true,
120
+ message: "We've sent an email to your registered address. Please " +
121
+ 'check your email to continue with account recovery.',
122
+ };
123
+ }
124
+ else {
125
+ return { result: false, message: "Couldn't send recovery email." };
126
+ }
127
+ }
128
+ static async recover(data) {
129
+ if (!this.tilmeld.config.pwRecovery) {
130
+ return {
131
+ result: false,
132
+ message: 'Account recovery is not allowed.',
133
+ };
134
+ }
135
+ const user = await this.tilmeld.User.factoryUsername(data.username);
136
+ if (user.guid == null ||
137
+ user.recoverSecret == null ||
138
+ data.secret !== user.recoverSecret ||
139
+ (0, strtotime_1.default)('+' + this.tilmeld.config.pwRecoveryTimeLimit, Math.floor((user.recoverSecretDate ?? 0) / 1000)) *
140
+ 1000 <
141
+ Date.now()) {
142
+ return {
143
+ result: false,
144
+ message: 'The secret code does not match.',
145
+ };
146
+ }
147
+ if (!('password' in data) || !data.password.length) {
148
+ return { result: false, message: 'Password cannot be empty.' };
149
+ }
150
+ user.$password(data.password);
151
+ delete user.recoverSecret;
152
+ delete user.recoverSecretDate;
153
+ if (await user.$saveSkipAC()) {
154
+ return {
155
+ result: true,
156
+ message: 'Your password has been reset. You can now log in using your new password.',
157
+ };
158
+ }
159
+ else {
160
+ return { result: false, message: 'Error saving new password.' };
161
+ }
184
162
  }
185
163
  static getClientConfig() {
186
164
  return {
@@ -194,21 +172,19 @@ class User extends AbleObject_1.default {
194
172
  unverifiedAccess: this.tilmeld.config.unverifiedAccess,
195
173
  };
196
174
  }
197
- static loginUser(data) {
198
- return __awaiter(this, void 0, void 0, function* () {
199
- if (!('username' in data) || !data.username.length) {
200
- return { result: false, message: 'Incorrect login/password.' };
201
- }
202
- const user = yield this.tilmeld.User.factoryUsername(data.username);
203
- const result = user.$login(data);
204
- if (result.result) {
205
- user.$updateDataProtection();
206
- result.user = user;
207
- }
208
- return result;
209
- });
175
+ static async loginUser(data) {
176
+ if (!('username' in data) || !data.username.length) {
177
+ return { result: false, message: 'Incorrect login/password.' };
178
+ }
179
+ const user = await this.tilmeld.User.factoryUsername(data.username);
180
+ const result = await user.$login(data);
181
+ if (result.result) {
182
+ user.$updateDataProtection();
183
+ result.user = user;
184
+ }
185
+ return result;
210
186
  }
211
- $login(data) {
187
+ async $login(data) {
212
188
  const tilmeld = this.$nymph.tilmeld;
213
189
  if (this.guid == null) {
214
190
  return { result: false, message: 'Incorrect login/password.' };
@@ -226,7 +202,7 @@ class User extends AbleObject_1.default {
226
202
  for (let callback of this.constructor
227
203
  .beforeLoginCallbacks) {
228
204
  if (callback) {
229
- callback(this, data);
205
+ await callback(this, data);
230
206
  }
231
207
  }
232
208
  }
@@ -242,18 +218,18 @@ class User extends AbleObject_1.default {
242
218
  for (let callback of this.constructor
243
219
  .afterLoginCallbacks) {
244
220
  if (callback) {
245
- callback(this);
221
+ await callback(this);
246
222
  }
247
223
  }
248
224
  return { result: true, message: 'You are logged in.' };
249
225
  }
250
- $logout() {
226
+ async $logout() {
251
227
  const tilmeld = this.$nymph.tilmeld;
252
228
  try {
253
229
  for (let callback of this.constructor
254
230
  .beforeLogoutCallbacks) {
255
231
  if (callback) {
256
- callback(this);
232
+ await callback(this);
257
233
  }
258
234
  }
259
235
  }
@@ -267,7 +243,7 @@ class User extends AbleObject_1.default {
267
243
  for (let callback of this.constructor
268
244
  .afterLogoutCallbacks) {
269
245
  if (callback) {
270
- callback(this);
246
+ await callback(this);
271
247
  }
272
248
  }
273
249
  return { result: true, message: 'You have been logged out.' };
@@ -283,40 +259,36 @@ class User extends AbleObject_1.default {
283
259
  (0, md5_1.default)(this.$data.email.trim().toLowerCase()).toString() +
284
260
  '?d=identicon&s=40');
285
261
  }
286
- $getDescendantGroups() {
287
- var _a, _b;
288
- return __awaiter(this, void 0, void 0, function* () {
289
- if (this.$descendantGroups == null) {
290
- this.$descendantGroups = [];
291
- if (this.$data.group != null) {
292
- this.$descendantGroups = yield this.$data.group.$getDescendants();
293
- }
294
- for (let curGroup of (_a = this.$data.groups) !== null && _a !== void 0 ? _a : []) {
295
- this.$descendantGroups = (_b = this.$descendantGroups) === null || _b === void 0 ? void 0 : _b.concat(yield curGroup.$getDescendants());
296
- }
262
+ async $getDescendantGroups() {
263
+ if (this.$descendantGroups == null) {
264
+ this.$descendantGroups = [];
265
+ if (this.$data.group != null) {
266
+ this.$descendantGroups = await this.$data.group.$getDescendants();
267
+ }
268
+ for (let curGroup of this.$data.groups ?? []) {
269
+ this.$descendantGroups = this.$descendantGroups?.concat(await curGroup.$getDescendants());
297
270
  }
298
- return this.$descendantGroups;
299
- });
271
+ }
272
+ return this.$descendantGroups;
300
273
  }
301
274
  $getDescendantGroupsSync() {
302
- var _a, _b;
303
275
  if (this.$descendantGroups == null) {
304
276
  this.$descendantGroups = [];
305
277
  if (this.$data.group != null) {
306
278
  this.$descendantGroups = this.$data.group.$getDescendantsSync();
307
279
  }
308
- for (let curGroup of (_a = this.$data.groups) !== null && _a !== void 0 ? _a : []) {
309
- this.$descendantGroups = (_b = this.$descendantGroups) === null || _b === void 0 ? void 0 : _b.concat(curGroup.$getDescendantsSync());
280
+ for (let curGroup of this.$data.groups ?? []) {
281
+ this.$descendantGroups = this.$descendantGroups?.concat(curGroup.$getDescendantsSync());
310
282
  }
311
283
  }
312
284
  return this.$descendantGroups;
313
285
  }
314
286
  $jsonAcceptData(input, allowConflict = false) {
315
- var _a, _b;
316
287
  const tilmeld = this.$nymph.tilmeld;
317
288
  this.$referenceWake();
318
- if (((_a = input.data.abilities) === null || _a === void 0 ? void 0 : _a.indexOf('system/admin')) !== -1 &&
319
- ((_b = this.$data.abilities) === null || _b === void 0 ? void 0 : _b.indexOf('system/admin')) === -1 &&
289
+ if ('abilities' in input.data &&
290
+ input.data.abilities.indexOf('system/admin') !== -1 &&
291
+ this.$data.abilities?.indexOf('system/admin') === -1 &&
320
292
  tilmeld.gatekeeper('tilmeld/admin') &&
321
293
  !tilmeld.gatekeeper('system/admin')) {
322
294
  throw new errors_1.BadDataError("You don't have the authority to make this user a system admin.");
@@ -325,11 +297,11 @@ class User extends AbleObject_1.default {
325
297
  super.$jsonAcceptData(input, allowConflict);
326
298
  }
327
299
  $jsonAcceptPatch(patch, allowConflict = false) {
328
- var _a, _b;
329
300
  const tilmeld = this.$nymph.tilmeld;
330
301
  this.$referenceWake();
331
- if (((_a = patch.set.abilities) === null || _a === void 0 ? void 0 : _a.indexOf('system/admin')) !== -1 &&
332
- ((_b = this.$data.abilities) === null || _b === void 0 ? void 0 : _b.indexOf('system/admin')) === -1 &&
302
+ if ('abilities' in patch.set &&
303
+ patch.set.abilities.indexOf('system/admin') !== -1 &&
304
+ this.$data.abilities?.indexOf('system/admin') === -1 &&
333
305
  tilmeld.gatekeeper('tilmeld/admin') &&
334
306
  !tilmeld.gatekeeper('system/admin')) {
335
307
  throw new errors_1.BadDataError("You don't have the authority to make this user a system admin.");
@@ -342,9 +314,8 @@ class User extends AbleObject_1.default {
342
314
  this.$updateDataProtection();
343
315
  }
344
316
  $updateDataProtection(givenUser) {
345
- var _a;
346
317
  const tilmeld = this.$nymph.tilmeld;
347
- let user = givenUser !== null && givenUser !== void 0 ? givenUser : tilmeld.User.current();
318
+ let user = givenUser ?? tilmeld.User.current();
348
319
  this.$clientEnabledMethods = [...User.DEFAULT_CLIENT_ENABLED_METHODS];
349
320
  this.$privateData = [...User.DEFAULT_PRIVATE_DATA];
350
321
  this.$allowlistData = [...User.DEFAULT_ALLOWLIST_DATA];
@@ -352,9 +323,9 @@ class User extends AbleObject_1.default {
352
323
  this.$privateData.push('username');
353
324
  }
354
325
  const isCurrentUser = user != null && user.$is(this);
355
- const abilities = (_a = (isCurrentUser || user == null
326
+ const abilities = (isCurrentUser || user == null
356
327
  ? this.$data.abilities
357
- : user === null || user === void 0 ? void 0 : user.abilities)) !== null && _a !== void 0 ? _a : [];
328
+ : user?.abilities) ?? [];
358
329
  const isNewUser = this.guid == null;
359
330
  if (isCurrentUser) {
360
331
  this.$clientEnabledMethods.push('$gatekeeper');
@@ -399,23 +370,22 @@ class User extends AbleObject_1.default {
399
370
  }
400
371
  }
401
372
  $gatekeeper(ability) {
402
- var _a, _b, _c, _d, _e, _f;
403
373
  const tilmeld = this.$nymph.tilmeld;
404
374
  if (ability == null) {
405
- return (_b = (_a = tilmeld.User.current()) === null || _a === void 0 ? void 0 : _a.$is(this)) !== null && _b !== void 0 ? _b : false;
375
+ return tilmeld.User.current()?.$is(this) ?? false;
406
376
  }
407
377
  if (this.$gatekeeperCache == null) {
408
- let abilities = (_c = this.$data.abilities) !== null && _c !== void 0 ? _c : [];
378
+ let abilities = this.$data.abilities ?? [];
409
379
  if (this.$data.inheritAbilities) {
410
- for (let curGroup of (_d = this.$data.groups) !== null && _d !== void 0 ? _d : []) {
380
+ for (let curGroup of this.$data.groups ?? []) {
411
381
  if (curGroup.enabled) {
412
- abilities = abilities.concat((_e = curGroup.abilities) !== null && _e !== void 0 ? _e : []);
382
+ abilities = abilities.concat(curGroup.abilities ?? []);
413
383
  }
414
384
  }
415
385
  if (this.$data.group != null &&
416
386
  this.$data.group.cdate != null &&
417
387
  this.$data.group.enabled) {
418
- abilities = abilities.concat((_f = this.$data.group.abilities) !== null && _f !== void 0 ? _f : []);
388
+ abilities = abilities.concat(this.$data.group.abilities ?? []);
419
389
  }
420
390
  }
421
391
  this.$gatekeeperCache = Object.fromEntries(abilities.map((ability) => [ability, true]));
@@ -429,72 +399,69 @@ class User extends AbleObject_1.default {
429
399
  this.$gatekeeperCache = undefined;
430
400
  return ret;
431
401
  }
432
- $sendEmailVerification() {
433
- var _a, _b, _c, _d, _e, _f, _g;
434
- return __awaiter(this, void 0, void 0, function* () {
435
- const tilmeld = this.$nymph.tilmeld;
436
- if (this.guid == null) {
437
- return false;
438
- }
439
- let success = true;
440
- const verifyUrl = `${tilmeld.config.appUrl.replace(/\/$/, () => '')}${tilmeld.config.setupPath}/verify`;
441
- if (this.$data.secret != null) {
442
- const link = `${verifyUrl}?action=verify&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.secret)}`;
443
- success =
444
- success &&
445
- (yield tilmeld.config.sendEmail(tilmeld, {
446
- template: 'VerifyEmail',
447
- message: {
448
- to: {
449
- name: (_a = this.$data.name) !== null && _a !== void 0 ? _a : '',
450
- address: (_b = this.$data.email) !== null && _b !== void 0 ? _b : '',
451
- },
452
- },
453
- locals: {
454
- verifyLink: link,
455
- },
456
- }, this));
457
- }
458
- if (this.$data.newEmailSecret != null) {
459
- const link = `${verifyUrl}?action=verifychange&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.newEmailSecret)}`;
460
- success =
461
- success &&
462
- (yield tilmeld.config.sendEmail(tilmeld, {
463
- template: 'VerifyEmailChange',
464
- message: {
465
- to: {
466
- name: (_c = this.$data.name) !== null && _c !== void 0 ? _c : '',
467
- address: (_d = this.$data.newEmailAddress) !== null && _d !== void 0 ? _d : '',
468
- },
469
- },
470
- locals: {
471
- verifyLink: link,
472
- oldEmail: this.$data.cancelEmailAddress,
473
- newEmail: this.$data.newEmailAddress,
402
+ async $sendEmailVerification() {
403
+ const tilmeld = this.$nymph.tilmeld;
404
+ if (this.guid == null) {
405
+ return false;
406
+ }
407
+ let success = true;
408
+ const verifyUrl = `${tilmeld.config.appUrl.replace(/\/$/, () => '')}${tilmeld.config.setupPath}/verify`;
409
+ if (this.$data.secret != null) {
410
+ const link = `${verifyUrl}?action=verify&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.secret)}`;
411
+ success =
412
+ success &&
413
+ (await tilmeld.config.sendEmail(tilmeld, {
414
+ template: 'VerifyEmail',
415
+ message: {
416
+ to: {
417
+ name: this.$data.name ?? '',
418
+ address: this.$data.email ?? '',
474
419
  },
475
- }, this));
476
- }
477
- if (this.$data.cancelEmailSecret != null) {
478
- const link = `${verifyUrl}?action=cancelchange&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.cancelEmailSecret)}`;
479
- success =
480
- success &&
481
- (yield tilmeld.config.sendEmail(tilmeld, {
482
- template: 'CancelEmailChange',
483
- message: {
484
- to: {
485
- name: (_e = this.$data.name) !== null && _e !== void 0 ? _e : '',
486
- address: (_f = this.$data.cancelEmailAddress) !== null && _f !== void 0 ? _f : '',
487
- },
420
+ },
421
+ locals: {
422
+ verifyLink: link,
423
+ },
424
+ }, this));
425
+ }
426
+ if (this.$data.newEmailSecret != null) {
427
+ const link = `${verifyUrl}?action=verifychange&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.newEmailSecret)}`;
428
+ success =
429
+ success &&
430
+ (await tilmeld.config.sendEmail(tilmeld, {
431
+ template: 'VerifyEmailChange',
432
+ message: {
433
+ to: {
434
+ name: this.$data.name ?? '',
435
+ address: this.$data.newEmailAddress ?? '',
488
436
  },
489
- locals: {
490
- cancelLink: link,
491
- oldEmail: this.$data.cancelEmailAddress,
492
- newEmail: (_g = this.$data.newEmailAddress) !== null && _g !== void 0 ? _g : this.$data.email,
437
+ },
438
+ locals: {
439
+ verifyLink: link,
440
+ oldEmail: this.$data.cancelEmailAddress,
441
+ newEmail: this.$data.newEmailAddress,
442
+ },
443
+ }, this));
444
+ }
445
+ if (this.$data.cancelEmailSecret != null) {
446
+ const link = `${verifyUrl}?action=cancelchange&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.cancelEmailSecret)}`;
447
+ success =
448
+ success &&
449
+ (await tilmeld.config.sendEmail(tilmeld, {
450
+ template: 'CancelEmailChange',
451
+ message: {
452
+ to: {
453
+ name: this.$data.name ?? '',
454
+ address: this.$data.cancelEmailAddress ?? '',
493
455
  },
494
- }, this));
495
- }
496
- return success;
497
- });
456
+ },
457
+ locals: {
458
+ cancelLink: link,
459
+ oldEmail: this.$data.cancelEmailAddress,
460
+ newEmail: this.$data.newEmailAddress ?? this.$data.email,
461
+ },
462
+ }, this));
463
+ }
464
+ return success;
498
465
  }
499
466
  $addGroup(group) {
500
467
  if (this.$data.groups == null) {
@@ -533,50 +500,45 @@ class User extends AbleObject_1.default {
533
500
  return true;
534
501
  }
535
502
  $inGroup(group) {
536
- var _a, _b, _c;
537
503
  if (typeof group === 'string') {
538
- if (((_a = this.$data.group) === null || _a === void 0 ? void 0 : _a.guid) === group) {
504
+ if (this.$data.group?.guid === group) {
539
505
  return true;
540
506
  }
541
- for (let curGroup of (_b = this.$data.groups) !== null && _b !== void 0 ? _b : []) {
507
+ for (let curGroup of this.$data.groups ?? []) {
542
508
  if (curGroup.guid === group) {
543
509
  return true;
544
510
  }
545
511
  }
546
512
  return false;
547
513
  }
548
- return (group.$is(this.$data.group) || group.$inArray((_c = this.$data.groups) !== null && _c !== void 0 ? _c : []));
514
+ return (group.$is(this.$data.group) || group.$inArray(this.$data.groups ?? []));
549
515
  }
550
516
  $isDescendant(group) {
551
- var _a, _b;
552
- if (((_a = this.$data.group) === null || _a === void 0 ? void 0 : _a.cdate) != null &&
517
+ if (this.$data.group?.cdate != null &&
553
518
  this.$data.group.$isDescendant(group)) {
554
519
  return true;
555
520
  }
556
- for (let curGroup of (_b = this.$data.groups) !== null && _b !== void 0 ? _b : []) {
521
+ for (let curGroup of this.$data.groups ?? []) {
557
522
  if (curGroup.$isDescendant(group)) {
558
523
  return true;
559
524
  }
560
525
  }
561
526
  return false;
562
527
  }
563
- $changePassword(data) {
564
- var _a;
565
- return __awaiter(this, void 0, void 0, function* () {
566
- if (!('newPassword' in data) || !data.newPassword.length) {
567
- return { result: false, message: 'Please specify a password.' };
568
- }
569
- if (!this.$checkPassword((_a = data.currentPassword) !== null && _a !== void 0 ? _a : '')) {
570
- return { result: false, message: 'Incorrect password.' };
571
- }
572
- this.$data.passwordTemp = data.newPassword;
573
- if (yield this.$save()) {
574
- return { result: true, message: 'Your password has been changed.' };
575
- }
576
- else {
577
- return { result: false, message: "Couldn't save new password." };
578
- }
579
- });
528
+ async $changePassword(data) {
529
+ if (!('newPassword' in data) || !data.newPassword.length) {
530
+ return { result: false, message: 'Please specify a password.' };
531
+ }
532
+ if (!this.$checkPassword(data.currentPassword ?? '')) {
533
+ return { result: false, message: 'Incorrect password.' };
534
+ }
535
+ this.$data.passwordTemp = data.newPassword;
536
+ if (await this.$save()) {
537
+ return { result: true, message: 'Your password has been changed.' };
538
+ }
539
+ else {
540
+ return { result: false, message: "Couldn't save new password." };
541
+ }
580
542
  }
581
543
  $password(password) {
582
544
  const tilmeld = this.$nymph.tilmeld;
@@ -597,585 +559,568 @@ class User extends AbleObject_1.default {
597
559
  }
598
560
  return this.$data.password;
599
561
  }
600
- $checkUsername() {
601
- return __awaiter(this, void 0, void 0, function* () {
602
- const tilmeld = this.$nymph.tilmeld;
603
- if (!tilmeld.config.emailUsernames) {
604
- if (this.$data.username == null || !this.$data.username.length) {
605
- return { result: false, message: 'Please specify a username.' };
606
- }
607
- if (tilmeld.config.maxUsernameLength > 0 &&
608
- this.$data.username.length > tilmeld.config.maxUsernameLength) {
609
- return {
610
- result: false,
611
- message: 'Usernames must not exceed ' +
612
- tilmeld.config.maxUsernameLength +
613
- ' characters.',
614
- };
615
- }
616
- if ((0, lodash_1.difference)(this.$data.username.split(''), tilmeld.config.validChars.split('')).length) {
617
- return {
618
- result: false,
619
- message: tilmeld.config.validCharsNotice,
620
- };
621
- }
622
- if (!tilmeld.config.validRegex.test(this.$data.username)) {
623
- return {
624
- result: false,
625
- message: tilmeld.config.validRegexNotice,
626
- };
627
- }
628
- const selector = {
629
- type: '&',
630
- ilike: [
631
- 'username',
632
- this.$data.username.replace(/([\\%_])/g, (s) => `\\${s}`),
633
- ],
634
- };
635
- if (this.guid != null) {
636
- selector['!guid'] = this.guid;
637
- }
638
- const test = yield this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
639
- if (test != null) {
640
- return { result: false, message: 'That username is taken.' };
641
- }
562
+ async $checkUsername() {
563
+ const tilmeld = this.$nymph.tilmeld;
564
+ if (!tilmeld.config.emailUsernames) {
565
+ if (this.$data.username == null || !this.$data.username.length) {
566
+ return { result: false, message: 'Please specify a username.' };
567
+ }
568
+ if (tilmeld.config.maxUsernameLength > 0 &&
569
+ this.$data.username.length > tilmeld.config.maxUsernameLength) {
642
570
  return {
643
- result: true,
644
- message: this.guid != null ? 'Username is valid.' : 'Username is available!',
571
+ result: false,
572
+ message: 'Usernames must not exceed ' +
573
+ tilmeld.config.maxUsernameLength +
574
+ ' characters.',
645
575
  };
646
576
  }
647
- else {
648
- this.$data.email = this.$data.username;
649
- if (this.$data.username == null || !this.$data.username.length) {
650
- return { result: false, message: 'Please specify an email.' };
651
- }
652
- if (tilmeld.config.maxUsernameLength > 0 &&
653
- this.$data.username.length > tilmeld.config.maxUsernameLength) {
654
- return {
655
- result: false,
656
- message: 'Emails must not exceed ' +
657
- tilmeld.config.maxUsernameLength +
658
- ' characters.',
659
- };
660
- }
661
- return yield this.$checkEmail();
662
- }
663
- });
664
- }
665
- $checkEmail() {
666
- return __awaiter(this, void 0, void 0, function* () {
667
- const tilmeld = this.$nymph.tilmeld;
668
- if (this.$data.email == null || !this.$data.email.length) {
669
- if (tilmeld.config.verifyEmail) {
670
- return { result: false, message: 'Please specify an email.' };
671
- }
672
- else {
673
- return { result: true, message: '' };
674
- }
577
+ if ((0, lodash_1.difference)(this.$data.username.split(''), tilmeld.config.validChars.split('')).length) {
578
+ return {
579
+ result: false,
580
+ message: tilmeld.config.validCharsNotice,
581
+ };
675
582
  }
676
- if (!tilmeld.config.validEmailRegex.test(this.$data.email)) {
583
+ if (!tilmeld.config.validRegex.test(this.$data.username)) {
677
584
  return {
678
585
  result: false,
679
- message: tilmeld.config.validEmailRegexNotice,
586
+ message: tilmeld.config.validRegexNotice,
680
587
  };
681
588
  }
682
589
  const selector = {
683
590
  type: '&',
684
- ilike: ['email', this.$data.email.replace(/([\\%_])/g, (s) => `\\${s}`)],
591
+ ilike: [
592
+ 'username',
593
+ this.$data.username.replace(/([\\%_])/g, (s) => `\\${s}`),
594
+ ],
685
595
  };
686
596
  if (this.guid != null) {
687
597
  selector['!guid'] = this.guid;
688
598
  }
689
- const test = yield this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
599
+ const test = await this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
690
600
  if (test != null) {
691
- return {
692
- result: false,
693
- message: 'That email address is already registered.',
694
- };
601
+ return { result: false, message: 'That username is taken.' };
695
602
  }
696
603
  return {
697
604
  result: true,
698
- message: this.guid != null ? 'Email is valid.' : 'Email address is valid!',
605
+ message: this.guid != null ? 'Username is valid.' : 'Username is available!',
699
606
  };
700
- });
701
- }
702
- $checkPhone() {
703
- return __awaiter(this, void 0, void 0, function* () {
704
- const tilmeld = this.$nymph.tilmeld;
705
- if (this.$data.phone == null || !this.$data.phone.length) {
706
- return { result: false, message: 'Please specify a phone number.' };
607
+ }
608
+ else {
609
+ this.$data.email = this.$data.username;
610
+ if (this.$data.username == null || !this.$data.username.length) {
611
+ return { result: false, message: 'Please specify an email.' };
707
612
  }
708
- const stripToDigits = this.$data.phone.replace(/\D/g, () => '');
709
- if (!/\d{10}/.test(stripToDigits)) {
613
+ if (tilmeld.config.maxUsernameLength > 0 &&
614
+ this.$data.username.length > tilmeld.config.maxUsernameLength) {
710
615
  return {
711
616
  result: false,
712
- message: 'Phone must contain at least 10 digits, but formatting does not matter.',
617
+ message: 'Emails must not exceed ' +
618
+ tilmeld.config.maxUsernameLength +
619
+ ' characters.',
713
620
  };
714
621
  }
715
- const selector = {
716
- type: '&',
717
- equal: ['phone', stripToDigits],
718
- };
719
- if (this.guid != null) {
720
- selector['!guid'] = this.guid;
622
+ return await this.$checkEmail();
623
+ }
624
+ }
625
+ async $checkEmail() {
626
+ const tilmeld = this.$nymph.tilmeld;
627
+ if (this.$data.email == null || !this.$data.email.length) {
628
+ if (tilmeld.config.verifyEmail) {
629
+ return { result: false, message: 'Please specify an email.' };
721
630
  }
722
- const test = yield this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
723
- if (test != null) {
724
- return { result: false, message: 'Phone number is in use.' };
631
+ else {
632
+ return { result: true, message: '' };
725
633
  }
634
+ }
635
+ if (!tilmeld.config.validEmailRegex.test(this.$data.email)) {
726
636
  return {
727
- result: true,
728
- message: this.guid != null ? 'Phone number is valid.' : 'Phone number is valid!',
637
+ result: false,
638
+ message: tilmeld.config.validEmailRegexNotice,
729
639
  };
730
- });
640
+ }
641
+ const selector = {
642
+ type: '&',
643
+ ilike: ['email', this.$data.email.replace(/([\\%_])/g, (s) => `\\${s}`)],
644
+ };
645
+ if (this.guid != null) {
646
+ selector['!guid'] = this.guid;
647
+ }
648
+ const test = await this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
649
+ if (test != null) {
650
+ return {
651
+ result: false,
652
+ message: 'That email address is already registered.',
653
+ };
654
+ }
655
+ return {
656
+ result: true,
657
+ message: this.guid != null ? 'Email is valid.' : 'Email address is valid!',
658
+ };
731
659
  }
732
- $register(data) {
733
- var _a;
734
- return __awaiter(this, void 0, void 0, function* () {
735
- let tilmeld = this.$nymph.tilmeld;
736
- if (!tilmeld.config.allowRegistration) {
737
- return {
738
- result: false,
739
- loggedin: false,
740
- message: 'Registration is not allowed.',
741
- };
660
+ async $checkPhone() {
661
+ const tilmeld = this.$nymph.tilmeld;
662
+ if (this.$data.phone == null || !this.$data.phone.length) {
663
+ return { result: false, message: 'Please specify a phone number.' };
664
+ }
665
+ const stripToDigits = this.$data.phone.replace(/\D/g, () => '');
666
+ if (!/\d{10}/.test(stripToDigits)) {
667
+ return {
668
+ result: false,
669
+ message: 'Phone must contain at least 10 digits, but formatting does not matter.',
670
+ };
671
+ }
672
+ const selector = {
673
+ type: '&',
674
+ equal: ['phone', stripToDigits],
675
+ };
676
+ if (this.guid != null) {
677
+ selector['!guid'] = this.guid;
678
+ }
679
+ const test = await this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
680
+ if (test != null) {
681
+ return { result: false, message: 'Phone number is in use.' };
682
+ }
683
+ return {
684
+ result: true,
685
+ message: this.guid != null ? 'Phone number is valid.' : 'Phone number is valid!',
686
+ };
687
+ }
688
+ async $register(data) {
689
+ let tilmeld = this.$nymph.tilmeld;
690
+ if (!tilmeld.config.allowRegistration) {
691
+ return {
692
+ result: false,
693
+ loggedin: false,
694
+ message: 'Registration is not allowed.',
695
+ };
696
+ }
697
+ if (this.guid != null) {
698
+ return {
699
+ result: false,
700
+ loggedin: false,
701
+ message: 'This is already a registered user.',
702
+ };
703
+ }
704
+ try {
705
+ for (let callback of this.constructor
706
+ .beforeRegisterCallbacks) {
707
+ if (callback) {
708
+ await callback(this, data);
709
+ }
742
710
  }
743
- if (this.guid != null) {
744
- return {
745
- result: false,
746
- loggedin: false,
747
- message: 'This is already a registered user.',
748
- };
711
+ }
712
+ catch (e) {
713
+ return {
714
+ result: false,
715
+ loggedin: false,
716
+ message: e.message,
717
+ };
718
+ }
719
+ if (!('password' in data) || !data.password.length) {
720
+ return {
721
+ result: false,
722
+ loggedin: false,
723
+ message: 'Password is a required field.',
724
+ };
725
+ }
726
+ const unCheck = await this.$checkUsername();
727
+ if (!unCheck.result) {
728
+ return { ...unCheck, loggedin: false };
729
+ }
730
+ this.$password(data.password);
731
+ if (tilmeld.config.regFields.indexOf('name') !== -1) {
732
+ this.$data.name =
733
+ this.$data.nameFirst +
734
+ (this.$data.nameMiddle == null ? '' : ' ' + this.$data.nameMiddle) +
735
+ (this.$data.nameLast == null ? '' : ' ' + this.$data.nameLast);
736
+ if (this.$data.name === '') {
737
+ this.$data.name = this.$data.username;
749
738
  }
750
- try {
751
- for (let callback of this.constructor
752
- .beforeRegisterCallbacks) {
753
- if (callback) {
754
- yield callback(this, data);
755
- }
739
+ }
740
+ if (tilmeld.config.emailUsernames) {
741
+ this.$data.email = this.$data.username;
742
+ }
743
+ const transaction = 'tilmeld-register-' + (0, guid_1.nanoid)();
744
+ const nymph = this.$nymph;
745
+ const tnymph = await this.$nymph.startTransaction(transaction);
746
+ this.$nymph = tnymph;
747
+ tilmeld = this.$nymph.tilmeld;
748
+ try {
749
+ let generatedPrimaryGroup = null;
750
+ if (tilmeld.config.generatePrimary) {
751
+ generatedPrimaryGroup = await tilmeld.Group.factory();
752
+ generatedPrimaryGroup.groupname = this.$data.username;
753
+ generatedPrimaryGroup.avatar = this.$data.avatar;
754
+ generatedPrimaryGroup.name = this.$data.name;
755
+ generatedPrimaryGroup.email = this.$data.email;
756
+ const parent = await tnymph.getEntity({ class: tilmeld.Group }, {
757
+ type: '&',
758
+ equal: ['defaultPrimary', true],
759
+ });
760
+ if (parent != null) {
761
+ generatedPrimaryGroup.parent = parent;
756
762
  }
763
+ if (!(await generatedPrimaryGroup.$saveSkipAC())) {
764
+ await tnymph.rollback(transaction);
765
+ this.$nymph = nymph;
766
+ return {
767
+ result: false,
768
+ loggedin: false,
769
+ message: 'Error creating primary group for user.',
770
+ };
771
+ }
772
+ this.$data.group = generatedPrimaryGroup;
757
773
  }
758
- catch (e) {
759
- return {
760
- result: false,
761
- loggedin: false,
762
- message: e.message,
763
- };
774
+ else {
775
+ const group = await tnymph.getEntity({ class: tilmeld.Group }, {
776
+ type: '&',
777
+ equal: ['defaultPrimary', true],
778
+ });
779
+ if (group != null) {
780
+ this.$data.group = group;
781
+ }
764
782
  }
765
- if (!('password' in data) || !data.password.length) {
766
- return {
767
- result: false,
768
- loggedin: false,
769
- message: 'Password is a required field.',
770
- };
783
+ if (tilmeld.config.verifyEmail && tilmeld.config.unverifiedAccess) {
784
+ this.$data.groups = await tnymph.getEntities({ class: tilmeld.Group }, {
785
+ type: '&',
786
+ equal: ['unverifiedSecondary', true],
787
+ });
771
788
  }
772
- const unCheck = yield this.$checkUsername();
773
- if (!unCheck.result) {
774
- return Object.assign(Object.assign({}, unCheck), { loggedin: false });
789
+ else {
790
+ this.$data.groups = await tnymph.getEntities({ class: tilmeld.Group }, {
791
+ type: '&',
792
+ equal: ['defaultSecondary', true],
793
+ });
775
794
  }
776
- this.$password(data.password);
777
- if (tilmeld.config.regFields.indexOf('name') !== -1) {
778
- this.$data.name =
779
- this.$data.nameFirst +
780
- (this.$data.nameMiddle == null ? '' : ' ' + this.$data.nameMiddle) +
781
- (this.$data.nameLast == null ? '' : ' ' + this.$data.nameLast);
782
- if (this.$data.name === '') {
783
- this.$data.name = this.$data.username;
795
+ if (tilmeld.config.verifyEmail) {
796
+ if (!tilmeld.config.unverifiedAccess) {
797
+ this.$data.enabled = false;
784
798
  }
785
799
  }
786
- if (tilmeld.config.emailUsernames) {
787
- this.$data.email = this.$data.username;
800
+ else {
801
+ this.$data.enabled = true;
802
+ }
803
+ let madeAdmin = false;
804
+ if (tilmeld.config.createAdmin) {
805
+ const otherUsers = await tnymph.getEntities({
806
+ class: tilmeld.User,
807
+ skipAc: true,
808
+ limit: 1,
809
+ return: 'guid',
810
+ });
811
+ if (!otherUsers.length) {
812
+ this.$grant('system/admin');
813
+ this.$data.enabled = true;
814
+ madeAdmin = true;
815
+ }
788
816
  }
789
- const transaction = 'tilmeld-register-' + (0, guid_1.nanoid)();
790
- const nymph = this.$nymph;
791
- const tnymph = yield this.$nymph.startTransaction(transaction);
792
- this.$nymph = tnymph;
793
- tilmeld = this.$nymph.tilmeld;
794
- try {
795
- let generatedPrimaryGroup = null;
796
- if (tilmeld.config.generatePrimary) {
797
- generatedPrimaryGroup = yield tilmeld.Group.factory();
798
- generatedPrimaryGroup.groupname = this.$data.username;
799
- generatedPrimaryGroup.avatar = this.$data.avatar;
800
- generatedPrimaryGroup.name = this.$data.name;
801
- generatedPrimaryGroup.email = this.$data.email;
802
- const parent = yield tnymph.getEntity({ class: tilmeld.Group }, {
803
- type: '&',
804
- equal: ['defaultPrimary', true],
805
- });
806
- if (parent != null) {
807
- generatedPrimaryGroup.parent = parent;
808
- }
809
- if (!(yield generatedPrimaryGroup.$saveSkipAC())) {
810
- yield tnymph.rollback(transaction);
817
+ if (await this.$saveSkipAC()) {
818
+ if (tilmeld.config.userRegisteredRecipient != null) {
819
+ await tilmeld.config.sendEmail(tilmeld, {
820
+ template: 'UserRegistered',
821
+ message: {
822
+ to: tilmeld.config.userRegisteredRecipient,
823
+ },
824
+ locals: {
825
+ userUsername: this.$data.username,
826
+ userName: this.$data.name,
827
+ userFirstName: this.$data.nameFirst,
828
+ userLastName: this.$data.nameLast,
829
+ userEmail: this.$data.email,
830
+ userPhone: this.$data.phone,
831
+ },
832
+ }, this);
833
+ }
834
+ let message = '';
835
+ let loggedin = false;
836
+ if (generatedPrimaryGroup != null) {
837
+ generatedPrimaryGroup.user = this;
838
+ if (!(await generatedPrimaryGroup.$saveSkipAC())) {
839
+ await tnymph.rollback(transaction);
811
840
  this.$nymph = nymph;
812
841
  return {
813
842
  result: false,
814
843
  loggedin: false,
815
- message: 'Error creating primary group for user.',
844
+ message: 'Your primary group could not be assigned. Please try again later.',
816
845
  };
817
846
  }
818
- this.$data.group = generatedPrimaryGroup;
819
847
  }
820
- else {
821
- const group = yield tnymph.getEntity({ class: tilmeld.Group }, {
822
- type: '&',
823
- equal: ['defaultPrimary', true],
824
- });
825
- if (group != null) {
826
- this.$data.group = group;
827
- }
828
- }
829
- if (tilmeld.config.verifyEmail && tilmeld.config.unverifiedAccess) {
830
- this.$data.groups = yield tnymph.getEntities({ class: tilmeld.Group }, {
831
- type: '&',
832
- equal: ['unverifiedSecondary', true],
833
- });
834
- }
835
- else {
836
- this.$data.groups = yield tnymph.getEntities({ class: tilmeld.Group }, {
837
- type: '&',
838
- equal: ['defaultSecondary', true],
839
- });
848
+ if (tilmeld.config.verifyEmail &&
849
+ !tilmeld.config.unverifiedAccess &&
850
+ !madeAdmin) {
851
+ message +=
852
+ `Almost there. An email has been sent to ${this.$data.email} ` +
853
+ 'with a verification link for you to finish registration.';
840
854
  }
841
- if (tilmeld.config.verifyEmail) {
842
- if (!tilmeld.config.unverifiedAccess) {
843
- this.$data.enabled = false;
855
+ else if (tilmeld.config.verifyEmail &&
856
+ tilmeld.config.unverifiedAccess &&
857
+ !madeAdmin) {
858
+ if (!tilmeld.login(this, true)) {
859
+ throw new Error('An error occurred trying to log you in.');
844
860
  }
861
+ message +=
862
+ "You're now logged in! An email has been sent to " +
863
+ `${this.$data.email} with a verification link for you to finish ` +
864
+ 'registration.';
865
+ loggedin = true;
845
866
  }
846
867
  else {
847
- this.$data.enabled = true;
848
- }
849
- let madeAdmin = false;
850
- if (tilmeld.config.createAdmin) {
851
- const otherUsers = yield tnymph.getEntities({
852
- class: tilmeld.User,
853
- skipAc: true,
854
- limit: 1,
855
- return: 'guid',
856
- });
857
- if (!otherUsers.length) {
858
- this.$grant('system/admin');
859
- this.$data.enabled = true;
860
- madeAdmin = true;
868
+ if (!tilmeld.login(this, true)) {
869
+ throw new Error('An error occurred trying to log you in.');
861
870
  }
871
+ message += "You're now registered and logged in!";
872
+ loggedin = true;
862
873
  }
863
- if (yield this.$saveSkipAC()) {
864
- if (tilmeld.config.userRegisteredRecipient != null) {
865
- yield tilmeld.config.sendEmail(tilmeld, {
866
- template: 'UserRegistered',
867
- message: {
868
- to: tilmeld.config.userRegisteredRecipient,
869
- },
870
- locals: {
871
- userUsername: this.$data.username,
872
- userName: this.$data.name,
873
- userFirstName: this.$data.nameFirst,
874
- userLastName: this.$data.nameLast,
875
- userEmail: this.$data.email,
876
- userPhone: this.$data.phone,
877
- },
878
- }, this);
879
- }
880
- let message = '';
881
- let loggedin = false;
882
- if (generatedPrimaryGroup != null) {
883
- generatedPrimaryGroup.user = this;
884
- if (!(yield generatedPrimaryGroup.$saveSkipAC())) {
885
- yield tnymph.rollback(transaction);
886
- this.$nymph = nymph;
887
- return {
888
- result: false,
889
- loggedin: false,
890
- message: 'Your primary group could not be assigned. Please try again later.',
891
- };
892
- }
893
- }
894
- if (tilmeld.config.verifyEmail &&
895
- !tilmeld.config.unverifiedAccess &&
896
- !madeAdmin) {
897
- message +=
898
- `Almost there. An email has been sent to ${this.$data.email} ` +
899
- 'with a verification link for you to finish registration.';
900
- }
901
- else if (tilmeld.config.verifyEmail &&
902
- tilmeld.config.unverifiedAccess &&
903
- !madeAdmin) {
904
- if (!tilmeld.login(this, true)) {
905
- throw new Error('An error occurred trying to log you in.');
906
- }
907
- message +=
908
- "You're now logged in! An email has been sent to " +
909
- `${this.$data.email} with a verification link for you to finish ` +
910
- 'registration.';
911
- loggedin = true;
912
- }
913
- else {
914
- if (!tilmeld.login(this, true)) {
915
- throw new Error('An error occurred trying to log you in.');
916
- }
917
- message += "You're now registered and logged in!";
918
- loggedin = true;
919
- }
920
- for (let callback of this.constructor
921
- .afterRegisterCallbacks) {
922
- if (callback) {
923
- yield callback(this, {
924
- loggedin,
925
- message,
926
- });
927
- }
928
- }
929
- try {
930
- yield tnymph.commit(transaction);
931
- this.$nymph = nymph;
932
- (_a = this.$nymph.tilmeld) === null || _a === void 0 ? void 0 : _a.fillSession(this);
933
- }
934
- catch (e) {
935
- throw e;
874
+ for (let callback of this.constructor
875
+ .afterRegisterCallbacks) {
876
+ if (callback) {
877
+ await callback(this, {
878
+ loggedin,
879
+ message,
880
+ });
936
881
  }
937
- return {
938
- result: true,
939
- loggedin,
940
- message,
941
- };
942
882
  }
943
- else {
944
- yield tnymph.rollback(transaction);
883
+ try {
884
+ await tnymph.commit(transaction);
945
885
  this.$nymph = nymph;
946
- return {
947
- result: false,
948
- loggedin: false,
949
- message: 'Error registering user.',
950
- };
886
+ this.$nymph.tilmeld?.fillSession(this);
951
887
  }
888
+ catch (e) {
889
+ throw e;
890
+ }
891
+ return {
892
+ result: true,
893
+ loggedin,
894
+ message,
895
+ };
952
896
  }
953
- catch (e) {
954
- yield tnymph.rollback(transaction);
897
+ else {
898
+ await tnymph.rollback(transaction);
955
899
  this.$nymph = nymph;
956
- throw e;
900
+ return {
901
+ result: false,
902
+ loggedin: false,
903
+ message: 'Error registering user.',
904
+ };
957
905
  }
958
- });
906
+ }
907
+ catch (e) {
908
+ await tnymph.rollback(transaction);
909
+ this.$nymph = nymph;
910
+ throw e;
911
+ }
959
912
  }
960
- $save() {
961
- const _super = Object.create(null, {
962
- $save: { get: () => super.$save }
963
- });
964
- var _a, _b, _c, _d, _e, _f;
965
- return __awaiter(this, void 0, void 0, function* () {
966
- let tilmeld = this.$nymph.tilmeld;
967
- if (this.$data.username == null || !this.$data.username.trim().length) {
968
- return false;
969
- }
970
- if (tilmeld.gatekeeper('tilmeld/admin') &&
971
- !tilmeld.gatekeeper('system/admin') &&
972
- this.$gatekeeper('system/admin')) {
973
- throw new errors_1.BadDataError("You don't have the authority to modify system admins.");
974
- }
975
- let sendVerification = false;
976
- this.$data.username = this.$data.username.trim();
977
- if (tilmeld.config.emailUsernames) {
978
- this.$data.email = this.$data.username;
979
- }
980
- this.$data.nameFirst = ((_a = this.$data.nameFirst) !== null && _a !== void 0 ? _a : '').trim();
981
- this.$data.nameMiddle = ((_b = this.$data.nameMiddle) !== null && _b !== void 0 ? _b : '').trim();
982
- this.$data.nameLast = ((_c = this.$data.nameLast) !== null && _c !== void 0 ? _c : '').trim();
983
- this.$data.phone = ((_d = this.$data.phone) !== null && _d !== void 0 ? _d : '').trim();
984
- this.$data.name =
985
- this.$data.nameFirst +
986
- (this.$data.nameMiddle ? ' ' + this.$data.nameMiddle : '') +
987
- (this.$data.nameLast ? ' ' + this.$data.nameLast : '');
988
- this.$data.enabled = !!this.$data.enabled;
989
- if (this.$data.nameFirst === '') {
990
- delete this.$data.nameFirst;
991
- }
992
- if (this.$data.nameMiddle === '') {
993
- delete this.$data.nameMiddle;
994
- }
995
- if (this.$data.nameLast === '') {
996
- delete this.$data.nameLast;
997
- }
998
- if (this.$data.name === '') {
999
- delete this.$data.name;
1000
- }
1001
- if (this.$data.email === '') {
1002
- delete this.$data.email;
1003
- }
1004
- if (this.$data.avatar === '') {
1005
- delete this.$data.avatar;
1006
- }
1007
- if (this.$data.phone === '') {
1008
- delete this.$data.phone;
1009
- }
1010
- if (this.$data.secret === '') {
1011
- delete this.$data.secret;
1012
- }
1013
- if (this.$data.emailChangeDate === 0) {
1014
- delete this.$data.emailChangeDate;
1015
- }
1016
- if (this.$data.newEmailSecret === '') {
1017
- delete this.$data.newEmailSecret;
1018
- }
1019
- if (this.$data.newEmailAddress === '') {
1020
- delete this.$data.newEmailAddress;
1021
- }
1022
- if (this.$data.cancelEmailSecret === '') {
1023
- delete this.$data.cancelEmailSecret;
1024
- }
1025
- if (this.$data.cancelEmailAddress === '') {
1026
- delete this.$data.cancelEmailAddress;
1027
- }
1028
- if (this.$data.recoverSecret === '') {
1029
- delete this.$data.recoverSecret;
1030
- }
1031
- if (this.$data.recoverSecretDate === 0) {
1032
- delete this.$data.recoverSecretDate;
1033
- }
1034
- if (this.$data.password === '') {
1035
- delete this.$data.password;
1036
- }
1037
- if (this.$data.passwordTemp === '') {
1038
- delete this.$data.passwordTemp;
1039
- }
1040
- if (this.$data.inheritAbilities === false) {
1041
- delete this.$data.inheritAbilities;
1042
- }
1043
- const unCheck = yield this.$checkUsername();
1044
- if (!unCheck.result) {
1045
- throw new errors_1.BadUsernameError(unCheck.message);
913
+ async $save() {
914
+ let tilmeld = this.$nymph.tilmeld;
915
+ if (this.$data.username == null || !this.$data.username.trim().length) {
916
+ return false;
917
+ }
918
+ if (tilmeld.gatekeeper('tilmeld/admin') &&
919
+ !tilmeld.gatekeeper('system/admin') &&
920
+ this.$gatekeeper('system/admin')) {
921
+ throw new errors_1.BadDataError("You don't have the authority to modify system admins.");
922
+ }
923
+ let sendVerification = false;
924
+ this.$data.username = this.$data.username.trim();
925
+ if (tilmeld.config.emailUsernames) {
926
+ this.$data.email = this.$data.username;
927
+ }
928
+ this.$data.nameFirst = (this.$data.nameFirst ?? '').trim();
929
+ this.$data.nameMiddle = (this.$data.nameMiddle ?? '').trim();
930
+ this.$data.nameLast = (this.$data.nameLast ?? '').trim();
931
+ this.$data.phone = (this.$data.phone ?? '').trim();
932
+ this.$data.name =
933
+ this.$data.nameFirst +
934
+ (this.$data.nameMiddle ? ' ' + this.$data.nameMiddle : '') +
935
+ (this.$data.nameLast ? ' ' + this.$data.nameLast : '');
936
+ this.$data.enabled = !!this.$data.enabled;
937
+ if (this.$data.nameFirst === '') {
938
+ delete this.$data.nameFirst;
939
+ }
940
+ if (this.$data.nameMiddle === '') {
941
+ delete this.$data.nameMiddle;
942
+ }
943
+ if (this.$data.nameLast === '') {
944
+ delete this.$data.nameLast;
945
+ }
946
+ if (this.$data.name === '') {
947
+ delete this.$data.name;
948
+ }
949
+ if (this.$data.email === '') {
950
+ delete this.$data.email;
951
+ }
952
+ if (this.$data.avatar === '') {
953
+ delete this.$data.avatar;
954
+ }
955
+ if (this.$data.phone === '') {
956
+ delete this.$data.phone;
957
+ }
958
+ if (this.$data.secret === '') {
959
+ delete this.$data.secret;
960
+ }
961
+ if (this.$data.emailChangeDate === 0) {
962
+ delete this.$data.emailChangeDate;
963
+ }
964
+ if (this.$data.newEmailSecret === '') {
965
+ delete this.$data.newEmailSecret;
966
+ }
967
+ if (this.$data.newEmailAddress === '') {
968
+ delete this.$data.newEmailAddress;
969
+ }
970
+ if (this.$data.cancelEmailSecret === '') {
971
+ delete this.$data.cancelEmailSecret;
972
+ }
973
+ if (this.$data.cancelEmailAddress === '') {
974
+ delete this.$data.cancelEmailAddress;
975
+ }
976
+ if (this.$data.recoverSecret === '') {
977
+ delete this.$data.recoverSecret;
978
+ }
979
+ if (this.$data.recoverSecretDate === 0) {
980
+ delete this.$data.recoverSecretDate;
981
+ }
982
+ if (this.$data.password === '') {
983
+ delete this.$data.password;
984
+ }
985
+ if (this.$data.passwordTemp === '') {
986
+ delete this.$data.passwordTemp;
987
+ }
988
+ if (this.$data.inheritAbilities === false) {
989
+ delete this.$data.inheritAbilities;
990
+ }
991
+ const unCheck = await this.$checkUsername();
992
+ if (!unCheck.result) {
993
+ throw new errors_1.BadUsernameError(unCheck.message);
994
+ }
995
+ if (!tilmeld.config.emailUsernames) {
996
+ const emCheck = await this.$checkEmail();
997
+ if (!emCheck.result) {
998
+ throw new errors_1.BadEmailError(emCheck.message);
1046
999
  }
1047
- if (!tilmeld.config.emailUsernames) {
1048
- const emCheck = yield this.$checkEmail();
1049
- if (!emCheck.result) {
1050
- throw new errors_1.BadEmailError(emCheck.message);
1000
+ }
1001
+ if (!tilmeld.gatekeeper('tilmeld/admin')) {
1002
+ if (tilmeld.config.verifyEmail) {
1003
+ if (this.guid == null) {
1004
+ if ((this.$data.abilities?.indexOf('system/admin') ?? -1) === -1) {
1005
+ this.$data.secret = (0, guid_1.nanoid)();
1006
+ sendVerification = true;
1007
+ }
1051
1008
  }
1052
- }
1053
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
1054
- if (tilmeld.config.verifyEmail) {
1055
- if (this.guid == null) {
1056
- if (((_f = (_e = this.$data.abilities) === null || _e === void 0 ? void 0 : _e.indexOf('system/admin')) !== null && _f !== void 0 ? _f : -1) === -1) {
1057
- this.$data.secret = (0, guid_1.nanoid)();
1058
- sendVerification = true;
1059
- }
1009
+ else if (this.$originalEmail != null &&
1010
+ this.$originalEmail !== this.$data.email) {
1011
+ if (tilmeld.config.emailRateLimit !== '' &&
1012
+ this.$data.emailChangeDate != null &&
1013
+ this.$data.emailChangeDate >=
1014
+ (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000) {
1015
+ throw new errors_1.EmailChangeRateLimitExceededError('You already changed your email address recently. Please wait until ' +
1016
+ new Date((0, strtotime_1.default)('+' + tilmeld.config.emailRateLimit, Math.floor(this.$data.emailChangeDate / 1000)) * 1000).toString() +
1017
+ ' to change your email address again.');
1060
1018
  }
1061
- else if (this.$originalEmail != null &&
1062
- this.$originalEmail !== this.$data.email) {
1063
- if (tilmeld.config.emailRateLimit !== '' &&
1064
- this.$data.emailChangeDate != null &&
1065
- this.$data.emailChangeDate >=
1066
- (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000) {
1067
- throw new errors_1.EmailChangeRateLimitExceededError('You already changed your email address recently. Please wait until ' +
1068
- new Date((0, strtotime_1.default)('+' + tilmeld.config.emailRateLimit, Math.floor(this.$data.emailChangeDate / 1000)) * 1000).toString() +
1069
- ' to change your email address again.');
1019
+ else {
1020
+ if (this.$data.newEmailSecret == null &&
1021
+ tilmeld.config.emailRateLimit !== '' &&
1022
+ (this.$data.emailChangeDate == null ||
1023
+ this.$data.emailChangeDate <
1024
+ (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000)) {
1025
+ this.$data.cancelEmailAddress = this.$originalEmail;
1026
+ this.$data.cancelEmailSecret = (0, guid_1.nanoid)();
1027
+ this.$data.emailChangeDate = Date.now();
1070
1028
  }
1071
- else {
1072
- if (this.$data.newEmailSecret == null &&
1073
- tilmeld.config.emailRateLimit !== '' &&
1074
- (this.$data.emailChangeDate == null ||
1075
- this.$data.emailChangeDate <
1076
- (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000)) {
1077
- this.$data.cancelEmailAddress = this.$originalEmail;
1078
- this.$data.cancelEmailSecret = (0, guid_1.nanoid)();
1079
- this.$data.emailChangeDate = Date.now();
1080
- }
1081
- this.$data.newEmailAddress = this.$data.email;
1082
- this.$data.email = this.$originalEmail;
1083
- if (tilmeld.config.emailUsernames) {
1084
- this.$data.username = this.$data.email;
1085
- }
1086
- this.$data.newEmailSecret = (0, guid_1.nanoid)();
1087
- sendVerification = true;
1029
+ this.$data.newEmailAddress = this.$data.email;
1030
+ this.$data.email = this.$originalEmail;
1031
+ if (tilmeld.config.emailUsernames) {
1032
+ this.$data.username = this.$data.email;
1088
1033
  }
1034
+ this.$data.newEmailSecret = (0, guid_1.nanoid)();
1035
+ sendVerification = true;
1089
1036
  }
1090
1037
  }
1091
- else if (this.guid != null &&
1092
- this.$originalEmail != null &&
1093
- this.$originalEmail !== this.$data.email &&
1094
- (this.$data.emailChangeDate == null ||
1095
- this.$data.emailChangeDate <
1096
- (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000)) {
1097
- this.$data.cancelEmailAddress = this.$originalEmail;
1098
- this.$data.cancelEmailSecret = (0, guid_1.nanoid)();
1099
- sendVerification = true;
1100
- }
1101
- }
1102
- if (this.$data.passwordTemp != null) {
1103
- this.$password(this.$data.passwordTemp);
1104
- }
1105
- delete this.$data.passwordTemp;
1106
- if (this.$data.password == null) {
1107
- throw new errors_1.BadDataError('A password is required.');
1108
1038
  }
1109
- try {
1110
- tilmeld.config.validatorUser(this);
1039
+ else if (this.guid != null &&
1040
+ this.$originalEmail != null &&
1041
+ this.$originalEmail !== this.$data.email &&
1042
+ (this.$data.emailChangeDate == null ||
1043
+ this.$data.emailChangeDate <
1044
+ (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000)) {
1045
+ this.$data.cancelEmailAddress = this.$originalEmail;
1046
+ this.$data.cancelEmailSecret = (0, guid_1.nanoid)();
1047
+ sendVerification = true;
1111
1048
  }
1112
- catch (e) {
1113
- throw new errors_1.BadDataError(e === null || e === void 0 ? void 0 : e.message);
1114
- }
1115
- const transaction = 'tilmeld-save-' + (0, guid_1.nanoid)();
1116
- const nymph = this.$nymph;
1117
- const tnymph = yield this.$nymph.startTransaction(transaction);
1118
- this.$nymph = tnymph;
1119
- tilmeld = this.$nymph.tilmeld;
1120
- if (this.$data.group != null &&
1121
- this.$data.group.user != null &&
1122
- this.$is(this.$data.group.user)) {
1123
- try {
1124
- this.$data.group.groupname = this.$data.username;
1125
- this.$data.group.avatar = this.$data.avatar;
1126
- this.$data.group.email = this.$data.email;
1127
- this.$data.group.name = this.$data.name;
1128
- this.$data.group.phone = this.$data.phone;
1129
- yield this.$data.group.$saveSkipAC();
1130
- }
1131
- catch (e) {
1132
- yield tnymph.rollback(transaction);
1133
- this.$nymph = nymph;
1134
- throw e;
1135
- }
1136
- }
1137
- let ret;
1138
- let preGuid = this.guid;
1139
- let preCdate = this.cdate;
1140
- let preMdate = this.mdate;
1049
+ }
1050
+ if (this.$data.passwordTemp != null) {
1051
+ this.$password(this.$data.passwordTemp);
1052
+ }
1053
+ delete this.$data.passwordTemp;
1054
+ if (this.$data.password == null) {
1055
+ throw new errors_1.BadDataError('A password is required.');
1056
+ }
1057
+ try {
1058
+ tilmeld.config.validatorUser(this);
1059
+ }
1060
+ catch (e) {
1061
+ throw new errors_1.BadDataError(e?.message);
1062
+ }
1063
+ const transaction = 'tilmeld-save-' + (0, guid_1.nanoid)();
1064
+ const nymph = this.$nymph;
1065
+ const tnymph = await this.$nymph.startTransaction(transaction);
1066
+ this.$nymph = tnymph;
1067
+ tilmeld = this.$nymph.tilmeld;
1068
+ if (this.$data.group != null &&
1069
+ this.$data.group.user != null &&
1070
+ this.$is(this.$data.group.user)) {
1141
1071
  try {
1142
- ret = yield _super.$save.call(this);
1072
+ this.$data.group.groupname = this.$data.username;
1073
+ this.$data.group.avatar = this.$data.avatar;
1074
+ this.$data.group.email = this.$data.email;
1075
+ this.$data.group.name = this.$data.name;
1076
+ this.$data.group.phone = this.$data.phone;
1077
+ await this.$data.group.$saveSkipAC();
1143
1078
  }
1144
1079
  catch (e) {
1145
- yield tnymph.rollback(transaction);
1080
+ await tnymph.rollback(transaction);
1146
1081
  this.$nymph = nymph;
1147
1082
  throw e;
1148
1083
  }
1149
- if (ret) {
1150
- if (sendVerification) {
1151
- if (!(yield this.$sendEmailVerification())) {
1152
- yield tnymph.rollback(transaction);
1153
- this.guid = preGuid;
1154
- this.cdate = preCdate;
1155
- this.mdate = preMdate;
1156
- this.$nymph = nymph;
1157
- throw new Error("Couldn't send verification email.");
1158
- }
1159
- }
1160
- this.$descendantGroups = undefined;
1161
- this.$gatekeeperCache = undefined;
1162
- yield tnymph.commit(transaction);
1163
- if (nymph.tilmeld.User.current(true).$is(this)) {
1164
- nymph.tilmeld.fillSession(this);
1084
+ }
1085
+ let ret;
1086
+ let preGuid = this.guid;
1087
+ let preCdate = this.cdate;
1088
+ let preMdate = this.mdate;
1089
+ try {
1090
+ ret = await super.$save();
1091
+ }
1092
+ catch (e) {
1093
+ await tnymph.rollback(transaction);
1094
+ this.$nymph = nymph;
1095
+ throw e;
1096
+ }
1097
+ if (ret) {
1098
+ if (sendVerification) {
1099
+ if (!(await this.$sendEmailVerification())) {
1100
+ await tnymph.rollback(transaction);
1101
+ this.guid = preGuid;
1102
+ this.cdate = preCdate;
1103
+ this.mdate = preMdate;
1104
+ this.$nymph = nymph;
1105
+ throw new Error("Couldn't send verification email.");
1165
1106
  }
1166
1107
  }
1167
- else {
1168
- yield tnymph.rollback(transaction);
1108
+ this.$descendantGroups = undefined;
1109
+ this.$gatekeeperCache = undefined;
1110
+ await tnymph.commit(transaction);
1111
+ if (nymph.tilmeld.User.current(true).$is(this)) {
1112
+ nymph.tilmeld.fillSession(this);
1169
1113
  }
1170
- this.$nymph = nymph;
1171
- return ret;
1172
- });
1114
+ }
1115
+ else {
1116
+ await tnymph.rollback(transaction);
1117
+ }
1118
+ this.$nymph = nymph;
1119
+ return ret;
1173
1120
  }
1174
- $saveSkipAC() {
1175
- return __awaiter(this, void 0, void 0, function* () {
1176
- this.$skipAcWhenSaving = true;
1177
- return yield this.$save();
1178
- });
1121
+ async $saveSkipAC() {
1122
+ this.$skipAcWhenSaving = true;
1123
+ return await this.$save();
1179
1124
  }
1180
1125
  $tilmeldSaveSkipAC() {
1181
1126
  if (this.$skipAcWhenSaving) {
@@ -1184,24 +1129,19 @@ class User extends AbleObject_1.default {
1184
1129
  }
1185
1130
  return false;
1186
1131
  }
1187
- $delete() {
1188
- const _super = Object.create(null, {
1189
- $delete: { get: () => super.$delete }
1190
- });
1191
- return __awaiter(this, void 0, void 0, function* () {
1192
- const tilmeld = this.$nymph.tilmeld;
1193
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
1194
- throw new errors_1.BadDataError("You don't have the authority to delete users.");
1195
- }
1196
- if (!tilmeld.gatekeeper('system/admin') &&
1197
- this.$gatekeeper('system/admin')) {
1198
- throw new errors_1.BadDataError("You don't have the authority to delete system admins.");
1199
- }
1200
- if (tilmeld.User.current(true).$is(this)) {
1201
- this.$logout();
1202
- }
1203
- return yield _super.$delete.call(this);
1204
- });
1132
+ async $delete() {
1133
+ const tilmeld = this.$nymph.tilmeld;
1134
+ if (!tilmeld.gatekeeper('tilmeld/admin')) {
1135
+ throw new errors_1.BadDataError("You don't have the authority to delete users.");
1136
+ }
1137
+ if (!tilmeld.gatekeeper('system/admin') &&
1138
+ this.$gatekeeper('system/admin')) {
1139
+ throw new errors_1.BadDataError("You don't have the authority to delete system admins.");
1140
+ }
1141
+ if (tilmeld.User.current(true).$is(this)) {
1142
+ await this.$logout();
1143
+ }
1144
+ return await super.$delete();
1205
1145
  }
1206
1146
  static on(event, callback) {
1207
1147
  const prop = (event + 'Callbacks');