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

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,35 @@ 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 (input.data.abilities?.indexOf('system/admin') !== -1 &&
290
+ this.$data.abilities?.indexOf('system/admin') === -1 &&
320
291
  tilmeld.gatekeeper('tilmeld/admin') &&
321
292
  !tilmeld.gatekeeper('system/admin')) {
322
293
  throw new errors_1.BadDataError("You don't have the authority to make this user a system admin.");
@@ -325,11 +296,10 @@ class User extends AbleObject_1.default {
325
296
  super.$jsonAcceptData(input, allowConflict);
326
297
  }
327
298
  $jsonAcceptPatch(patch, allowConflict = false) {
328
- var _a, _b;
329
299
  const tilmeld = this.$nymph.tilmeld;
330
300
  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 &&
301
+ if (patch.set.abilities?.indexOf('system/admin') !== -1 &&
302
+ this.$data.abilities?.indexOf('system/admin') === -1 &&
333
303
  tilmeld.gatekeeper('tilmeld/admin') &&
334
304
  !tilmeld.gatekeeper('system/admin')) {
335
305
  throw new errors_1.BadDataError("You don't have the authority to make this user a system admin.");
@@ -342,9 +312,8 @@ class User extends AbleObject_1.default {
342
312
  this.$updateDataProtection();
343
313
  }
344
314
  $updateDataProtection(givenUser) {
345
- var _a;
346
315
  const tilmeld = this.$nymph.tilmeld;
347
- let user = givenUser !== null && givenUser !== void 0 ? givenUser : tilmeld.User.current();
316
+ let user = givenUser ?? tilmeld.User.current();
348
317
  this.$clientEnabledMethods = [...User.DEFAULT_CLIENT_ENABLED_METHODS];
349
318
  this.$privateData = [...User.DEFAULT_PRIVATE_DATA];
350
319
  this.$allowlistData = [...User.DEFAULT_ALLOWLIST_DATA];
@@ -352,9 +321,9 @@ class User extends AbleObject_1.default {
352
321
  this.$privateData.push('username');
353
322
  }
354
323
  const isCurrentUser = user != null && user.$is(this);
355
- const abilities = (_a = (isCurrentUser || user == null
324
+ const abilities = (isCurrentUser || user == null
356
325
  ? this.$data.abilities
357
- : user === null || user === void 0 ? void 0 : user.abilities)) !== null && _a !== void 0 ? _a : [];
326
+ : user?.abilities) ?? [];
358
327
  const isNewUser = this.guid == null;
359
328
  if (isCurrentUser) {
360
329
  this.$clientEnabledMethods.push('$gatekeeper');
@@ -399,23 +368,22 @@ class User extends AbleObject_1.default {
399
368
  }
400
369
  }
401
370
  $gatekeeper(ability) {
402
- var _a, _b, _c, _d, _e, _f;
403
371
  const tilmeld = this.$nymph.tilmeld;
404
372
  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;
373
+ return tilmeld.User.current()?.$is(this) ?? false;
406
374
  }
407
375
  if (this.$gatekeeperCache == null) {
408
- let abilities = (_c = this.$data.abilities) !== null && _c !== void 0 ? _c : [];
376
+ let abilities = this.$data.abilities ?? [];
409
377
  if (this.$data.inheritAbilities) {
410
- for (let curGroup of (_d = this.$data.groups) !== null && _d !== void 0 ? _d : []) {
378
+ for (let curGroup of this.$data.groups ?? []) {
411
379
  if (curGroup.enabled) {
412
- abilities = abilities.concat((_e = curGroup.abilities) !== null && _e !== void 0 ? _e : []);
380
+ abilities = abilities.concat(curGroup.abilities ?? []);
413
381
  }
414
382
  }
415
383
  if (this.$data.group != null &&
416
384
  this.$data.group.cdate != null &&
417
385
  this.$data.group.enabled) {
418
- abilities = abilities.concat((_f = this.$data.group.abilities) !== null && _f !== void 0 ? _f : []);
386
+ abilities = abilities.concat(this.$data.group.abilities ?? []);
419
387
  }
420
388
  }
421
389
  this.$gatekeeperCache = Object.fromEntries(abilities.map((ability) => [ability, true]));
@@ -429,72 +397,69 @@ class User extends AbleObject_1.default {
429
397
  this.$gatekeeperCache = undefined;
430
398
  return ret;
431
399
  }
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,
400
+ async $sendEmailVerification() {
401
+ const tilmeld = this.$nymph.tilmeld;
402
+ if (this.guid == null) {
403
+ return false;
404
+ }
405
+ let success = true;
406
+ const verifyUrl = `${tilmeld.config.appUrl.replace(/\/$/, () => '')}${tilmeld.config.setupPath}/verify`;
407
+ if (this.$data.secret != null) {
408
+ const link = `${verifyUrl}?action=verify&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.secret)}`;
409
+ success =
410
+ success &&
411
+ (await tilmeld.config.sendEmail(tilmeld, {
412
+ template: 'VerifyEmail',
413
+ message: {
414
+ to: {
415
+ name: this.$data.name ?? '',
416
+ address: this.$data.email ?? '',
474
417
  },
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
- },
418
+ },
419
+ locals: {
420
+ verifyLink: link,
421
+ },
422
+ }, this));
423
+ }
424
+ if (this.$data.newEmailSecret != null) {
425
+ const link = `${verifyUrl}?action=verifychange&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.newEmailSecret)}`;
426
+ success =
427
+ success &&
428
+ (await tilmeld.config.sendEmail(tilmeld, {
429
+ template: 'VerifyEmailChange',
430
+ message: {
431
+ to: {
432
+ name: this.$data.name ?? '',
433
+ address: this.$data.newEmailAddress ?? '',
488
434
  },
489
- locals: {
490
- cancelLink: link,
491
- oldEmail: this.$data.cancelEmailAddress,
492
- newEmail: (_g = this.$data.newEmailAddress) !== null && _g !== void 0 ? _g : this.$data.email,
435
+ },
436
+ locals: {
437
+ verifyLink: link,
438
+ oldEmail: this.$data.cancelEmailAddress,
439
+ newEmail: this.$data.newEmailAddress,
440
+ },
441
+ }, this));
442
+ }
443
+ if (this.$data.cancelEmailSecret != null) {
444
+ const link = `${verifyUrl}?action=cancelchange&id=${encodeURIComponent(this.guid)}&secret=${encodeURIComponent(this.$data.cancelEmailSecret)}`;
445
+ success =
446
+ success &&
447
+ (await tilmeld.config.sendEmail(tilmeld, {
448
+ template: 'CancelEmailChange',
449
+ message: {
450
+ to: {
451
+ name: this.$data.name ?? '',
452
+ address: this.$data.cancelEmailAddress ?? '',
493
453
  },
494
- }, this));
495
- }
496
- return success;
497
- });
454
+ },
455
+ locals: {
456
+ cancelLink: link,
457
+ oldEmail: this.$data.cancelEmailAddress,
458
+ newEmail: this.$data.newEmailAddress ?? this.$data.email,
459
+ },
460
+ }, this));
461
+ }
462
+ return success;
498
463
  }
499
464
  $addGroup(group) {
500
465
  if (this.$data.groups == null) {
@@ -533,50 +498,45 @@ class User extends AbleObject_1.default {
533
498
  return true;
534
499
  }
535
500
  $inGroup(group) {
536
- var _a, _b, _c;
537
501
  if (typeof group === 'string') {
538
- if (((_a = this.$data.group) === null || _a === void 0 ? void 0 : _a.guid) === group) {
502
+ if (this.$data.group?.guid === group) {
539
503
  return true;
540
504
  }
541
- for (let curGroup of (_b = this.$data.groups) !== null && _b !== void 0 ? _b : []) {
505
+ for (let curGroup of this.$data.groups ?? []) {
542
506
  if (curGroup.guid === group) {
543
507
  return true;
544
508
  }
545
509
  }
546
510
  return false;
547
511
  }
548
- return (group.$is(this.$data.group) || group.$inArray((_c = this.$data.groups) !== null && _c !== void 0 ? _c : []));
512
+ return (group.$is(this.$data.group) || group.$inArray(this.$data.groups ?? []));
549
513
  }
550
514
  $isDescendant(group) {
551
- var _a, _b;
552
- if (((_a = this.$data.group) === null || _a === void 0 ? void 0 : _a.cdate) != null &&
515
+ if (this.$data.group?.cdate != null &&
553
516
  this.$data.group.$isDescendant(group)) {
554
517
  return true;
555
518
  }
556
- for (let curGroup of (_b = this.$data.groups) !== null && _b !== void 0 ? _b : []) {
519
+ for (let curGroup of this.$data.groups ?? []) {
557
520
  if (curGroup.$isDescendant(group)) {
558
521
  return true;
559
522
  }
560
523
  }
561
524
  return false;
562
525
  }
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
- });
526
+ async $changePassword(data) {
527
+ if (!('newPassword' in data) || !data.newPassword.length) {
528
+ return { result: false, message: 'Please specify a password.' };
529
+ }
530
+ if (!this.$checkPassword(data.currentPassword ?? '')) {
531
+ return { result: false, message: 'Incorrect password.' };
532
+ }
533
+ this.$data.passwordTemp = data.newPassword;
534
+ if (await this.$save()) {
535
+ return { result: true, message: 'Your password has been changed.' };
536
+ }
537
+ else {
538
+ return { result: false, message: "Couldn't save new password." };
539
+ }
580
540
  }
581
541
  $password(password) {
582
542
  const tilmeld = this.$nymph.tilmeld;
@@ -597,585 +557,568 @@ class User extends AbleObject_1.default {
597
557
  }
598
558
  return this.$data.password;
599
559
  }
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
- }
560
+ async $checkUsername() {
561
+ const tilmeld = this.$nymph.tilmeld;
562
+ if (!tilmeld.config.emailUsernames) {
563
+ if (this.$data.username == null || !this.$data.username.length) {
564
+ return { result: false, message: 'Please specify a username.' };
565
+ }
566
+ if (tilmeld.config.maxUsernameLength > 0 &&
567
+ this.$data.username.length > tilmeld.config.maxUsernameLength) {
642
568
  return {
643
- result: true,
644
- message: this.guid != null ? 'Username is valid.' : 'Username is available!',
569
+ result: false,
570
+ message: 'Usernames must not exceed ' +
571
+ tilmeld.config.maxUsernameLength +
572
+ ' characters.',
645
573
  };
646
574
  }
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
- }
575
+ if ((0, lodash_1.difference)(this.$data.username.split(''), tilmeld.config.validChars.split('')).length) {
576
+ return {
577
+ result: false,
578
+ message: tilmeld.config.validCharsNotice,
579
+ };
675
580
  }
676
- if (!tilmeld.config.validEmailRegex.test(this.$data.email)) {
581
+ if (!tilmeld.config.validRegex.test(this.$data.username)) {
677
582
  return {
678
583
  result: false,
679
- message: tilmeld.config.validEmailRegexNotice,
584
+ message: tilmeld.config.validRegexNotice,
680
585
  };
681
586
  }
682
587
  const selector = {
683
588
  type: '&',
684
- ilike: ['email', this.$data.email.replace(/([\\%_])/g, (s) => `\\${s}`)],
589
+ ilike: [
590
+ 'username',
591
+ this.$data.username.replace(/([\\%_])/g, (s) => `\\${s}`),
592
+ ],
685
593
  };
686
594
  if (this.guid != null) {
687
595
  selector['!guid'] = this.guid;
688
596
  }
689
- const test = yield this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
597
+ const test = await this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
690
598
  if (test != null) {
691
- return {
692
- result: false,
693
- message: 'That email address is already registered.',
694
- };
599
+ return { result: false, message: 'That username is taken.' };
695
600
  }
696
601
  return {
697
602
  result: true,
698
- message: this.guid != null ? 'Email is valid.' : 'Email address is valid!',
603
+ message: this.guid != null ? 'Username is valid.' : 'Username is available!',
699
604
  };
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.' };
605
+ }
606
+ else {
607
+ this.$data.email = this.$data.username;
608
+ if (this.$data.username == null || !this.$data.username.length) {
609
+ return { result: false, message: 'Please specify an email.' };
707
610
  }
708
- const stripToDigits = this.$data.phone.replace(/\D/g, () => '');
709
- if (!/\d{10}/.test(stripToDigits)) {
611
+ if (tilmeld.config.maxUsernameLength > 0 &&
612
+ this.$data.username.length > tilmeld.config.maxUsernameLength) {
710
613
  return {
711
614
  result: false,
712
- message: 'Phone must contain at least 10 digits, but formatting does not matter.',
615
+ message: 'Emails must not exceed ' +
616
+ tilmeld.config.maxUsernameLength +
617
+ ' characters.',
713
618
  };
714
619
  }
715
- const selector = {
716
- type: '&',
717
- equal: ['phone', stripToDigits],
718
- };
719
- if (this.guid != null) {
720
- selector['!guid'] = this.guid;
620
+ return await this.$checkEmail();
621
+ }
622
+ }
623
+ async $checkEmail() {
624
+ const tilmeld = this.$nymph.tilmeld;
625
+ if (this.$data.email == null || !this.$data.email.length) {
626
+ if (tilmeld.config.verifyEmail) {
627
+ return { result: false, message: 'Please specify an email.' };
721
628
  }
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.' };
629
+ else {
630
+ return { result: true, message: '' };
725
631
  }
632
+ }
633
+ if (!tilmeld.config.validEmailRegex.test(this.$data.email)) {
726
634
  return {
727
- result: true,
728
- message: this.guid != null ? 'Phone number is valid.' : 'Phone number is valid!',
635
+ result: false,
636
+ message: tilmeld.config.validEmailRegexNotice,
729
637
  };
730
- });
638
+ }
639
+ const selector = {
640
+ type: '&',
641
+ ilike: ['email', this.$data.email.replace(/([\\%_])/g, (s) => `\\${s}`)],
642
+ };
643
+ if (this.guid != null) {
644
+ selector['!guid'] = this.guid;
645
+ }
646
+ const test = await this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
647
+ if (test != null) {
648
+ return {
649
+ result: false,
650
+ message: 'That email address is already registered.',
651
+ };
652
+ }
653
+ return {
654
+ result: true,
655
+ message: this.guid != null ? 'Email is valid.' : 'Email address is valid!',
656
+ };
731
657
  }
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
- };
658
+ async $checkPhone() {
659
+ const tilmeld = this.$nymph.tilmeld;
660
+ if (this.$data.phone == null || !this.$data.phone.length) {
661
+ return { result: false, message: 'Please specify a phone number.' };
662
+ }
663
+ const stripToDigits = this.$data.phone.replace(/\D/g, () => '');
664
+ if (!/\d{10}/.test(stripToDigits)) {
665
+ return {
666
+ result: false,
667
+ message: 'Phone must contain at least 10 digits, but formatting does not matter.',
668
+ };
669
+ }
670
+ const selector = {
671
+ type: '&',
672
+ equal: ['phone', stripToDigits],
673
+ };
674
+ if (this.guid != null) {
675
+ selector['!guid'] = this.guid;
676
+ }
677
+ const test = await this.$nymph.getEntity({ class: tilmeld.User, skipAc: true }, selector);
678
+ if (test != null) {
679
+ return { result: false, message: 'Phone number is in use.' };
680
+ }
681
+ return {
682
+ result: true,
683
+ message: this.guid != null ? 'Phone number is valid.' : 'Phone number is valid!',
684
+ };
685
+ }
686
+ async $register(data) {
687
+ let tilmeld = this.$nymph.tilmeld;
688
+ if (!tilmeld.config.allowRegistration) {
689
+ return {
690
+ result: false,
691
+ loggedin: false,
692
+ message: 'Registration is not allowed.',
693
+ };
694
+ }
695
+ if (this.guid != null) {
696
+ return {
697
+ result: false,
698
+ loggedin: false,
699
+ message: 'This is already a registered user.',
700
+ };
701
+ }
702
+ try {
703
+ for (let callback of this.constructor
704
+ .beforeRegisterCallbacks) {
705
+ if (callback) {
706
+ await callback(this, data);
707
+ }
742
708
  }
743
- if (this.guid != null) {
744
- return {
745
- result: false,
746
- loggedin: false,
747
- message: 'This is already a registered user.',
748
- };
709
+ }
710
+ catch (e) {
711
+ return {
712
+ result: false,
713
+ loggedin: false,
714
+ message: e.message,
715
+ };
716
+ }
717
+ if (!('password' in data) || !data.password.length) {
718
+ return {
719
+ result: false,
720
+ loggedin: false,
721
+ message: 'Password is a required field.',
722
+ };
723
+ }
724
+ const unCheck = await this.$checkUsername();
725
+ if (!unCheck.result) {
726
+ return { ...unCheck, loggedin: false };
727
+ }
728
+ this.$password(data.password);
729
+ if (tilmeld.config.regFields.indexOf('name') !== -1) {
730
+ this.$data.name =
731
+ this.$data.nameFirst +
732
+ (this.$data.nameMiddle == null ? '' : ' ' + this.$data.nameMiddle) +
733
+ (this.$data.nameLast == null ? '' : ' ' + this.$data.nameLast);
734
+ if (this.$data.name === '') {
735
+ this.$data.name = this.$data.username;
749
736
  }
750
- try {
751
- for (let callback of this.constructor
752
- .beforeRegisterCallbacks) {
753
- if (callback) {
754
- yield callback(this, data);
755
- }
737
+ }
738
+ if (tilmeld.config.emailUsernames) {
739
+ this.$data.email = this.$data.username;
740
+ }
741
+ const transaction = 'tilmeld-register-' + (0, guid_1.nanoid)();
742
+ const nymph = this.$nymph;
743
+ const tnymph = await this.$nymph.startTransaction(transaction);
744
+ this.$nymph = tnymph;
745
+ tilmeld = this.$nymph.tilmeld;
746
+ try {
747
+ let generatedPrimaryGroup = null;
748
+ if (tilmeld.config.generatePrimary) {
749
+ generatedPrimaryGroup = await tilmeld.Group.factory();
750
+ generatedPrimaryGroup.groupname = this.$data.username;
751
+ generatedPrimaryGroup.avatar = this.$data.avatar;
752
+ generatedPrimaryGroup.name = this.$data.name;
753
+ generatedPrimaryGroup.email = this.$data.email;
754
+ const parent = await tnymph.getEntity({ class: tilmeld.Group }, {
755
+ type: '&',
756
+ equal: ['defaultPrimary', true],
757
+ });
758
+ if (parent != null) {
759
+ generatedPrimaryGroup.parent = parent;
756
760
  }
761
+ if (!(await generatedPrimaryGroup.$saveSkipAC())) {
762
+ await tnymph.rollback(transaction);
763
+ this.$nymph = nymph;
764
+ return {
765
+ result: false,
766
+ loggedin: false,
767
+ message: 'Error creating primary group for user.',
768
+ };
769
+ }
770
+ this.$data.group = generatedPrimaryGroup;
757
771
  }
758
- catch (e) {
759
- return {
760
- result: false,
761
- loggedin: false,
762
- message: e.message,
763
- };
772
+ else {
773
+ const group = await tnymph.getEntity({ class: tilmeld.Group }, {
774
+ type: '&',
775
+ equal: ['defaultPrimary', true],
776
+ });
777
+ if (group != null) {
778
+ this.$data.group = group;
779
+ }
764
780
  }
765
- if (!('password' in data) || !data.password.length) {
766
- return {
767
- result: false,
768
- loggedin: false,
769
- message: 'Password is a required field.',
770
- };
781
+ if (tilmeld.config.verifyEmail && tilmeld.config.unverifiedAccess) {
782
+ this.$data.groups = await tnymph.getEntities({ class: tilmeld.Group }, {
783
+ type: '&',
784
+ equal: ['unverifiedSecondary', true],
785
+ });
771
786
  }
772
- const unCheck = yield this.$checkUsername();
773
- if (!unCheck.result) {
774
- return Object.assign(Object.assign({}, unCheck), { loggedin: false });
787
+ else {
788
+ this.$data.groups = await tnymph.getEntities({ class: tilmeld.Group }, {
789
+ type: '&',
790
+ equal: ['defaultSecondary', true],
791
+ });
775
792
  }
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;
793
+ if (tilmeld.config.verifyEmail) {
794
+ if (!tilmeld.config.unverifiedAccess) {
795
+ this.$data.enabled = false;
784
796
  }
785
797
  }
786
- if (tilmeld.config.emailUsernames) {
787
- this.$data.email = this.$data.username;
798
+ else {
799
+ this.$data.enabled = true;
800
+ }
801
+ let madeAdmin = false;
802
+ if (tilmeld.config.createAdmin) {
803
+ const otherUsers = await tnymph.getEntities({
804
+ class: tilmeld.User,
805
+ skipAc: true,
806
+ limit: 1,
807
+ return: 'guid',
808
+ });
809
+ if (!otherUsers.length) {
810
+ this.$grant('system/admin');
811
+ this.$data.enabled = true;
812
+ madeAdmin = true;
813
+ }
788
814
  }
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);
815
+ if (await this.$saveSkipAC()) {
816
+ if (tilmeld.config.userRegisteredRecipient != null) {
817
+ await tilmeld.config.sendEmail(tilmeld, {
818
+ template: 'UserRegistered',
819
+ message: {
820
+ to: tilmeld.config.userRegisteredRecipient,
821
+ },
822
+ locals: {
823
+ userUsername: this.$data.username,
824
+ userName: this.$data.name,
825
+ userFirstName: this.$data.nameFirst,
826
+ userLastName: this.$data.nameLast,
827
+ userEmail: this.$data.email,
828
+ userPhone: this.$data.phone,
829
+ },
830
+ }, this);
831
+ }
832
+ let message = '';
833
+ let loggedin = false;
834
+ if (generatedPrimaryGroup != null) {
835
+ generatedPrimaryGroup.user = this;
836
+ if (!(await generatedPrimaryGroup.$saveSkipAC())) {
837
+ await tnymph.rollback(transaction);
811
838
  this.$nymph = nymph;
812
839
  return {
813
840
  result: false,
814
841
  loggedin: false,
815
- message: 'Error creating primary group for user.',
842
+ message: 'Your primary group could not be assigned. Please try again later.',
816
843
  };
817
844
  }
818
- this.$data.group = generatedPrimaryGroup;
819
845
  }
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
- });
846
+ if (tilmeld.config.verifyEmail &&
847
+ !tilmeld.config.unverifiedAccess &&
848
+ !madeAdmin) {
849
+ message +=
850
+ `Almost there. An email has been sent to ${this.$data.email} ` +
851
+ 'with a verification link for you to finish registration.';
840
852
  }
841
- if (tilmeld.config.verifyEmail) {
842
- if (!tilmeld.config.unverifiedAccess) {
843
- this.$data.enabled = false;
853
+ else if (tilmeld.config.verifyEmail &&
854
+ tilmeld.config.unverifiedAccess &&
855
+ !madeAdmin) {
856
+ if (!tilmeld.login(this, true)) {
857
+ throw new Error('An error occurred trying to log you in.');
844
858
  }
859
+ message +=
860
+ "You're now logged in! An email has been sent to " +
861
+ `${this.$data.email} with a verification link for you to finish ` +
862
+ 'registration.';
863
+ loggedin = true;
845
864
  }
846
865
  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;
866
+ if (!tilmeld.login(this, true)) {
867
+ throw new Error('An error occurred trying to log you in.');
861
868
  }
869
+ message += "You're now registered and logged in!";
870
+ loggedin = true;
862
871
  }
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;
872
+ for (let callback of this.constructor
873
+ .afterRegisterCallbacks) {
874
+ if (callback) {
875
+ await callback(this, {
876
+ loggedin,
877
+ message,
878
+ });
936
879
  }
937
- return {
938
- result: true,
939
- loggedin,
940
- message,
941
- };
942
880
  }
943
- else {
944
- yield tnymph.rollback(transaction);
881
+ try {
882
+ await tnymph.commit(transaction);
945
883
  this.$nymph = nymph;
946
- return {
947
- result: false,
948
- loggedin: false,
949
- message: 'Error registering user.',
950
- };
884
+ this.$nymph.tilmeld?.fillSession(this);
951
885
  }
886
+ catch (e) {
887
+ throw e;
888
+ }
889
+ return {
890
+ result: true,
891
+ loggedin,
892
+ message,
893
+ };
952
894
  }
953
- catch (e) {
954
- yield tnymph.rollback(transaction);
895
+ else {
896
+ await tnymph.rollback(transaction);
955
897
  this.$nymph = nymph;
956
- throw e;
898
+ return {
899
+ result: false,
900
+ loggedin: false,
901
+ message: 'Error registering user.',
902
+ };
957
903
  }
958
- });
904
+ }
905
+ catch (e) {
906
+ await tnymph.rollback(transaction);
907
+ this.$nymph = nymph;
908
+ throw e;
909
+ }
959
910
  }
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);
911
+ async $save() {
912
+ let tilmeld = this.$nymph.tilmeld;
913
+ if (this.$data.username == null || !this.$data.username.trim().length) {
914
+ return false;
915
+ }
916
+ if (tilmeld.gatekeeper('tilmeld/admin') &&
917
+ !tilmeld.gatekeeper('system/admin') &&
918
+ this.$gatekeeper('system/admin')) {
919
+ throw new errors_1.BadDataError("You don't have the authority to modify system admins.");
920
+ }
921
+ let sendVerification = false;
922
+ this.$data.username = this.$data.username.trim();
923
+ if (tilmeld.config.emailUsernames) {
924
+ this.$data.email = this.$data.username;
925
+ }
926
+ this.$data.nameFirst = (this.$data.nameFirst ?? '').trim();
927
+ this.$data.nameMiddle = (this.$data.nameMiddle ?? '').trim();
928
+ this.$data.nameLast = (this.$data.nameLast ?? '').trim();
929
+ this.$data.phone = (this.$data.phone ?? '').trim();
930
+ this.$data.name =
931
+ this.$data.nameFirst +
932
+ (this.$data.nameMiddle ? ' ' + this.$data.nameMiddle : '') +
933
+ (this.$data.nameLast ? ' ' + this.$data.nameLast : '');
934
+ this.$data.enabled = !!this.$data.enabled;
935
+ if (this.$data.nameFirst === '') {
936
+ delete this.$data.nameFirst;
937
+ }
938
+ if (this.$data.nameMiddle === '') {
939
+ delete this.$data.nameMiddle;
940
+ }
941
+ if (this.$data.nameLast === '') {
942
+ delete this.$data.nameLast;
943
+ }
944
+ if (this.$data.name === '') {
945
+ delete this.$data.name;
946
+ }
947
+ if (this.$data.email === '') {
948
+ delete this.$data.email;
949
+ }
950
+ if (this.$data.avatar === '') {
951
+ delete this.$data.avatar;
952
+ }
953
+ if (this.$data.phone === '') {
954
+ delete this.$data.phone;
955
+ }
956
+ if (this.$data.secret === '') {
957
+ delete this.$data.secret;
958
+ }
959
+ if (this.$data.emailChangeDate === 0) {
960
+ delete this.$data.emailChangeDate;
961
+ }
962
+ if (this.$data.newEmailSecret === '') {
963
+ delete this.$data.newEmailSecret;
964
+ }
965
+ if (this.$data.newEmailAddress === '') {
966
+ delete this.$data.newEmailAddress;
967
+ }
968
+ if (this.$data.cancelEmailSecret === '') {
969
+ delete this.$data.cancelEmailSecret;
970
+ }
971
+ if (this.$data.cancelEmailAddress === '') {
972
+ delete this.$data.cancelEmailAddress;
973
+ }
974
+ if (this.$data.recoverSecret === '') {
975
+ delete this.$data.recoverSecret;
976
+ }
977
+ if (this.$data.recoverSecretDate === 0) {
978
+ delete this.$data.recoverSecretDate;
979
+ }
980
+ if (this.$data.password === '') {
981
+ delete this.$data.password;
982
+ }
983
+ if (this.$data.passwordTemp === '') {
984
+ delete this.$data.passwordTemp;
985
+ }
986
+ if (this.$data.inheritAbilities === false) {
987
+ delete this.$data.inheritAbilities;
988
+ }
989
+ const unCheck = await this.$checkUsername();
990
+ if (!unCheck.result) {
991
+ throw new errors_1.BadUsernameError(unCheck.message);
992
+ }
993
+ if (!tilmeld.config.emailUsernames) {
994
+ const emCheck = await this.$checkEmail();
995
+ if (!emCheck.result) {
996
+ throw new errors_1.BadEmailError(emCheck.message);
1046
997
  }
1047
- if (!tilmeld.config.emailUsernames) {
1048
- const emCheck = yield this.$checkEmail();
1049
- if (!emCheck.result) {
1050
- throw new errors_1.BadEmailError(emCheck.message);
998
+ }
999
+ if (!tilmeld.gatekeeper('tilmeld/admin')) {
1000
+ if (tilmeld.config.verifyEmail) {
1001
+ if (this.guid == null) {
1002
+ if ((this.$data.abilities?.indexOf('system/admin') ?? -1) === -1) {
1003
+ this.$data.secret = (0, guid_1.nanoid)();
1004
+ sendVerification = true;
1005
+ }
1051
1006
  }
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
- }
1007
+ else if (this.$originalEmail != null &&
1008
+ this.$originalEmail !== this.$data.email) {
1009
+ if (tilmeld.config.emailRateLimit !== '' &&
1010
+ this.$data.emailChangeDate != null &&
1011
+ this.$data.emailChangeDate >=
1012
+ (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000) {
1013
+ throw new errors_1.EmailChangeRateLimitExceededError('You already changed your email address recently. Please wait until ' +
1014
+ new Date((0, strtotime_1.default)('+' + tilmeld.config.emailRateLimit, Math.floor(this.$data.emailChangeDate / 1000)) * 1000).toString() +
1015
+ ' to change your email address again.');
1060
1016
  }
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.');
1017
+ else {
1018
+ if (this.$data.newEmailSecret == null &&
1019
+ tilmeld.config.emailRateLimit !== '' &&
1020
+ (this.$data.emailChangeDate == null ||
1021
+ this.$data.emailChangeDate <
1022
+ (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000)) {
1023
+ this.$data.cancelEmailAddress = this.$originalEmail;
1024
+ this.$data.cancelEmailSecret = (0, guid_1.nanoid)();
1025
+ this.$data.emailChangeDate = Date.now();
1070
1026
  }
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;
1027
+ this.$data.newEmailAddress = this.$data.email;
1028
+ this.$data.email = this.$originalEmail;
1029
+ if (tilmeld.config.emailUsernames) {
1030
+ this.$data.username = this.$data.email;
1088
1031
  }
1032
+ this.$data.newEmailSecret = (0, guid_1.nanoid)();
1033
+ sendVerification = true;
1089
1034
  }
1090
1035
  }
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
1036
  }
1109
- try {
1110
- tilmeld.config.validatorUser(this);
1037
+ else if (this.guid != null &&
1038
+ this.$originalEmail != null &&
1039
+ this.$originalEmail !== this.$data.email &&
1040
+ (this.$data.emailChangeDate == null ||
1041
+ this.$data.emailChangeDate <
1042
+ (0, strtotime_1.default)('-' + tilmeld.config.emailRateLimit) * 1000)) {
1043
+ this.$data.cancelEmailAddress = this.$originalEmail;
1044
+ this.$data.cancelEmailSecret = (0, guid_1.nanoid)();
1045
+ sendVerification = true;
1111
1046
  }
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;
1047
+ }
1048
+ if (this.$data.passwordTemp != null) {
1049
+ this.$password(this.$data.passwordTemp);
1050
+ }
1051
+ delete this.$data.passwordTemp;
1052
+ if (this.$data.password == null) {
1053
+ throw new errors_1.BadDataError('A password is required.');
1054
+ }
1055
+ try {
1056
+ tilmeld.config.validatorUser(this);
1057
+ }
1058
+ catch (e) {
1059
+ throw new errors_1.BadDataError(e?.message);
1060
+ }
1061
+ const transaction = 'tilmeld-save-' + (0, guid_1.nanoid)();
1062
+ const nymph = this.$nymph;
1063
+ const tnymph = await this.$nymph.startTransaction(transaction);
1064
+ this.$nymph = tnymph;
1065
+ tilmeld = this.$nymph.tilmeld;
1066
+ if (this.$data.group != null &&
1067
+ this.$data.group.user != null &&
1068
+ this.$is(this.$data.group.user)) {
1141
1069
  try {
1142
- ret = yield _super.$save.call(this);
1070
+ this.$data.group.groupname = this.$data.username;
1071
+ this.$data.group.avatar = this.$data.avatar;
1072
+ this.$data.group.email = this.$data.email;
1073
+ this.$data.group.name = this.$data.name;
1074
+ this.$data.group.phone = this.$data.phone;
1075
+ await this.$data.group.$saveSkipAC();
1143
1076
  }
1144
1077
  catch (e) {
1145
- yield tnymph.rollback(transaction);
1078
+ await tnymph.rollback(transaction);
1146
1079
  this.$nymph = nymph;
1147
1080
  throw e;
1148
1081
  }
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);
1082
+ }
1083
+ let ret;
1084
+ let preGuid = this.guid;
1085
+ let preCdate = this.cdate;
1086
+ let preMdate = this.mdate;
1087
+ try {
1088
+ ret = await super.$save();
1089
+ }
1090
+ catch (e) {
1091
+ await tnymph.rollback(transaction);
1092
+ this.$nymph = nymph;
1093
+ throw e;
1094
+ }
1095
+ if (ret) {
1096
+ if (sendVerification) {
1097
+ if (!(await this.$sendEmailVerification())) {
1098
+ await tnymph.rollback(transaction);
1099
+ this.guid = preGuid;
1100
+ this.cdate = preCdate;
1101
+ this.mdate = preMdate;
1102
+ this.$nymph = nymph;
1103
+ throw new Error("Couldn't send verification email.");
1165
1104
  }
1166
1105
  }
1167
- else {
1168
- yield tnymph.rollback(transaction);
1106
+ this.$descendantGroups = undefined;
1107
+ this.$gatekeeperCache = undefined;
1108
+ await tnymph.commit(transaction);
1109
+ if (nymph.tilmeld.User.current(true).$is(this)) {
1110
+ nymph.tilmeld.fillSession(this);
1169
1111
  }
1170
- this.$nymph = nymph;
1171
- return ret;
1172
- });
1112
+ }
1113
+ else {
1114
+ await tnymph.rollback(transaction);
1115
+ }
1116
+ this.$nymph = nymph;
1117
+ return ret;
1173
1118
  }
1174
- $saveSkipAC() {
1175
- return __awaiter(this, void 0, void 0, function* () {
1176
- this.$skipAcWhenSaving = true;
1177
- return yield this.$save();
1178
- });
1119
+ async $saveSkipAC() {
1120
+ this.$skipAcWhenSaving = true;
1121
+ return await this.$save();
1179
1122
  }
1180
1123
  $tilmeldSaveSkipAC() {
1181
1124
  if (this.$skipAcWhenSaving) {
@@ -1184,24 +1127,19 @@ class User extends AbleObject_1.default {
1184
1127
  }
1185
1128
  return false;
1186
1129
  }
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
- });
1130
+ async $delete() {
1131
+ const tilmeld = this.$nymph.tilmeld;
1132
+ if (!tilmeld.gatekeeper('tilmeld/admin')) {
1133
+ throw new errors_1.BadDataError("You don't have the authority to delete users.");
1134
+ }
1135
+ if (!tilmeld.gatekeeper('system/admin') &&
1136
+ this.$gatekeeper('system/admin')) {
1137
+ throw new errors_1.BadDataError("You don't have the authority to delete system admins.");
1138
+ }
1139
+ if (tilmeld.User.current(true).$is(this)) {
1140
+ await this.$logout();
1141
+ }
1142
+ return await super.$delete();
1205
1143
  }
1206
1144
  static on(event, callback) {
1207
1145
  const prop = (event + 'Callbacks');