@nymphjs/tilmeld 1.0.0-beta.121 → 1.0.0-beta.122

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,4 +1,5 @@
1
1
  import crypto from 'node:crypto';
2
+ import { MethodFailedError, HttpError, } from '@nymphjs/nymph';
2
3
  import { humanSecret, nanoid } from '@nymphjs/guid';
3
4
  import { strtotime } from 'locutus/php/datetime/index';
4
5
  import { difference, xor } from 'lodash-es';
@@ -214,7 +215,10 @@ export default class User extends AbleObject {
214
215
  // Create a unique secret.
215
216
  getUser.recoverSecret = humanSecret();
216
217
  getUser.recoverSecretDate = Date.now();
217
- if (!(await getUser.$saveSkipAC())) {
218
+ try {
219
+ await getUser.$saveSkipAC();
220
+ }
221
+ catch (e) {
218
222
  return { result: false, message: "Couldn't save recovery secret." };
219
223
  }
220
224
  // Send the recovery email.
@@ -280,13 +284,14 @@ export default class User extends AbleObject {
280
284
  user.$password(data.password);
281
285
  delete user.recoverSecret;
282
286
  delete user.recoverSecretDate;
283
- if (await user.$saveSkipAC()) {
287
+ try {
288
+ await user.$saveSkipAC();
284
289
  return {
285
290
  result: true,
286
291
  message: 'Your password has been reset. You can now log in using your new password.',
287
292
  };
288
293
  }
289
- else {
294
+ catch (e) {
290
295
  return { result: false, message: 'Error saving new password.' };
291
296
  }
292
297
  }
@@ -371,19 +376,6 @@ export default class User extends AbleObject {
371
376
  this.$data.inheritAbilities = true;
372
377
  this.$updateDataProtection();
373
378
  }
374
- $setNymph(nymph) {
375
- this.$nymph = nymph;
376
- if (!this.$asleep()) {
377
- if (this.$data.group && this.$data.group.$nymph !== nymph) {
378
- this.$data.group.$setNymph(nymph);
379
- }
380
- for (const group of this.$data.groups ?? []) {
381
- if (group && group.$nymph !== nymph) {
382
- group.$setNymph(nymph);
383
- }
384
- }
385
- }
386
- }
387
379
  async $getUniques() {
388
380
  const tilmeld = enforceTilmeld(this);
389
381
  const uniques = [`u:${this.$data.username}`];
@@ -1106,14 +1098,15 @@ export default class User extends AbleObject {
1106
1098
  if (data.revokeCurrentTokens) {
1107
1099
  this.$data.revokeTokenDate = Date.now();
1108
1100
  }
1109
- if (await this.$save()) {
1101
+ try {
1102
+ await this.$save();
1110
1103
  if (data.revokeCurrentTokens) {
1111
1104
  const tilmeld = enforceTilmeld(this);
1112
1105
  await tilmeld.login(this, true);
1113
1106
  }
1114
1107
  return { result: true, message: 'Your password has been changed.' };
1115
1108
  }
1116
- else {
1109
+ catch (e) {
1117
1110
  return { result: false, message: "Couldn't save new password." };
1118
1111
  }
1119
1112
  }
@@ -1159,7 +1152,8 @@ export default class User extends AbleObject {
1159
1152
  return { result: false, message: 'Incorrect password.' };
1160
1153
  }
1161
1154
  this.$data.revokeTokenDate = Date.now();
1162
- if (await this.$save()) {
1155
+ try {
1156
+ await this.$save();
1163
1157
  const tilmeld = enforceTilmeld(this);
1164
1158
  await tilmeld.login(this, true);
1165
1159
  return {
@@ -1167,7 +1161,7 @@ export default class User extends AbleObject {
1167
1161
  message: 'You have logged out of all other sessions.',
1168
1162
  };
1169
1163
  }
1170
- else {
1164
+ catch (e) {
1171
1165
  return { result: false, message: "Couldn't save revocation date." };
1172
1166
  }
1173
1167
  }
@@ -1258,10 +1252,11 @@ export default class User extends AbleObject {
1258
1252
  return { result: false, message: '2FA code is incorrect.' };
1259
1253
  }
1260
1254
  this.$data.totpSecret = data.secret;
1261
- if (await this.$save()) {
1255
+ try {
1256
+ await this.$save();
1262
1257
  return { result: true, message: 'Your two factor secret is now set.' };
1263
1258
  }
1264
- else {
1259
+ catch (e) {
1265
1260
  return { result: false, message: "Couldn't save two factor secret." };
1266
1261
  }
1267
1262
  }
@@ -1305,13 +1300,14 @@ export default class User extends AbleObject {
1305
1300
  };
1306
1301
  }
1307
1302
  delete this.$data.totpSecret;
1308
- if (await this.$save()) {
1303
+ try {
1304
+ await this.$save();
1309
1305
  return {
1310
1306
  result: true,
1311
1307
  message: 'Two factor secret has been removed.',
1312
1308
  };
1313
1309
  }
1314
- else {
1310
+ catch (e) {
1315
1311
  return { result: false, message: "Couldn't remove two factor secret." };
1316
1312
  }
1317
1313
  }
@@ -1615,7 +1611,8 @@ export default class User extends AbleObject {
1615
1611
  }
1616
1612
  }
1617
1613
  this.$nymph.config.debugLog('tilmeld', `Registering new user "${this.$data.username}".`);
1618
- if (await this.$saveSkipAC()) {
1614
+ try {
1615
+ await this.$saveSkipAC();
1619
1616
  this.$nymph.config.debugLog('tilmeld', `New user registered "${this.$data.username}".`);
1620
1617
  // Send the new user registered email.
1621
1618
  if (tilmeld.config.userRegisteredRecipient != null) {
@@ -1673,7 +1670,10 @@ export default class User extends AbleObject {
1673
1670
  }
1674
1671
  }
1675
1672
  }
1676
- else {
1673
+ catch (e) {
1674
+ if (e instanceof HttpError) {
1675
+ throw e;
1676
+ }
1677
1677
  this.$nymph.config.debugError('tilmeld', `Error registering new user "${this.$data.username}".`);
1678
1678
  await tnymph.rollback(transaction);
1679
1679
  this.$setNymph(nymph);
@@ -1707,7 +1707,7 @@ export default class User extends AbleObject {
1707
1707
  async $save() {
1708
1708
  let tilmeld = enforceTilmeld(this);
1709
1709
  if (this.$data.username == null || !this.$data.username.trim().length) {
1710
- return false;
1710
+ throw new BadUsernameError('Username must not be empty.');
1711
1711
  }
1712
1712
  if (!this.$skipAcWhenSaving &&
1713
1713
  tilmeld.gatekeeper('tilmeld/admin') &&
@@ -1985,9 +1985,7 @@ export default class User extends AbleObject {
1985
1985
  changed = true;
1986
1986
  }
1987
1987
  if (changed || group.guid == null) {
1988
- if (!(await group.$saveSkipAC())) {
1989
- throw Error('Error updating primary group for user.');
1990
- }
1988
+ await group.$saveSkipAC();
1991
1989
  }
1992
1990
  this.$data.group = group;
1993
1991
  }
@@ -2023,7 +2021,6 @@ export default class User extends AbleObject {
2023
2021
  this.$setNymph(nymph);
2024
2022
  throw e;
2025
2023
  }
2026
- let ret = false;
2027
2024
  let preGuid = this.guid;
2028
2025
  let preCdate = this.cdate;
2029
2026
  let preMdate = this.mdate;
@@ -2034,7 +2031,7 @@ export default class User extends AbleObject {
2034
2031
  await callback(this);
2035
2032
  }
2036
2033
  }
2037
- ret = await super.$save();
2034
+ await super.$save();
2038
2035
  }
2039
2036
  catch (e) {
2040
2037
  await tnymph.rollback(transaction);
@@ -2044,43 +2041,38 @@ export default class User extends AbleObject {
2044
2041
  this.$setNymph(nymph);
2045
2042
  throw e;
2046
2043
  }
2047
- if (ret) {
2048
- if (sendVerification) {
2049
- // The email has changed, so send a new verification email.
2050
- if (!(await this.$sendEmailVerification())) {
2051
- await tnymph.rollback(transaction);
2052
- this.guid = preGuid;
2053
- this.cdate = preCdate;
2054
- this.mdate = preMdate;
2055
- this.$setNymph(nymph);
2056
- throw new Error("Couldn't send verification email.");
2057
- }
2058
- }
2059
- this.$descendantGroups = undefined;
2060
- this.$gatekeeperCache = undefined;
2061
- try {
2062
- for (let callback of this.constructor
2063
- .afterSaveCallbacks) {
2064
- if (callback) {
2065
- await callback(this);
2066
- }
2067
- }
2068
- }
2069
- catch (e) {
2044
+ if (sendVerification) {
2045
+ // The email has changed, so send a new verification email.
2046
+ if (!(await this.$sendEmailVerification())) {
2070
2047
  await tnymph.rollback(transaction);
2071
2048
  this.guid = preGuid;
2072
2049
  this.cdate = preCdate;
2073
2050
  this.mdate = preMdate;
2074
2051
  this.$setNymph(nymph);
2075
- throw e;
2052
+ throw new Error("Couldn't send verification email.");
2076
2053
  }
2077
- ret = await tnymph.commit(transaction);
2078
2054
  }
2079
- else {
2055
+ this.$descendantGroups = undefined;
2056
+ this.$gatekeeperCache = undefined;
2057
+ try {
2058
+ for (let callback of this.constructor
2059
+ .afterSaveCallbacks) {
2060
+ if (callback) {
2061
+ await callback(this);
2062
+ }
2063
+ }
2064
+ }
2065
+ catch (e) {
2080
2066
  await tnymph.rollback(transaction);
2067
+ this.guid = preGuid;
2068
+ this.cdate = preCdate;
2069
+ this.mdate = preMdate;
2070
+ this.$setNymph(nymph);
2071
+ throw e;
2081
2072
  }
2073
+ const committed = await tnymph.commit(transaction);
2082
2074
  this.$setNymph(nymph);
2083
- if (ret) {
2075
+ if (committed) {
2084
2076
  this.$originalEmail = this.$data.email;
2085
2077
  this.$originalUsername = this.$data.username;
2086
2078
  const tilmeld = enforceTilmeld(nymph);
@@ -2089,14 +2081,16 @@ export default class User extends AbleObject {
2089
2081
  await tilmeld.fillSession(this);
2090
2082
  }
2091
2083
  }
2092
- return ret;
2084
+ else {
2085
+ throw new MethodFailedError('Transaction could not be committed.');
2086
+ }
2093
2087
  }
2094
2088
  /**
2095
2089
  * This should *never* be accessible on the client.
2096
2090
  */
2097
2091
  async $saveSkipAC() {
2098
2092
  this.$skipAcWhenSaving = true;
2099
- return await this.$save();
2093
+ await this.$save();
2100
2094
  }
2101
2095
  $tilmeldSaveSkipAC() {
2102
2096
  if (this.$skipAcWhenSaving) {
@@ -2122,7 +2116,6 @@ export default class User extends AbleObject {
2122
2116
  const nymph = this.$nymph;
2123
2117
  const tnymph = await nymph.startTransaction(transaction);
2124
2118
  this.$setNymph(tnymph);
2125
- let success = false;
2126
2119
  try {
2127
2120
  if (!(await this.$refresh())) {
2128
2121
  throw new BadDataError('User could not be refreshed.');
@@ -2150,16 +2143,17 @@ export default class User extends AbleObject {
2150
2143
  // Read the skip ac value here, because super.$delete changes it.
2151
2144
  const $skipAcWhenDeleting = this.$skipAcWhenDeleting;
2152
2145
  // Delete the user.
2153
- success = await super.$delete();
2154
- if (success) {
2155
- // Delete the generated group.
2156
- success = $skipAcWhenDeleting
2157
- ? await this.$data.group.$deleteSkipAC()
2158
- : await this.$data.group.$delete();
2146
+ await super.$delete();
2147
+ // Delete the generated group.
2148
+ if ($skipAcWhenDeleting) {
2149
+ await this.$data.group.$deleteSkipAC();
2150
+ }
2151
+ else {
2152
+ await this.$data.group.$delete();
2159
2153
  }
2160
2154
  }
2161
2155
  else {
2162
- success = await super.$delete();
2156
+ await super.$delete();
2163
2157
  }
2164
2158
  }
2165
2159
  catch (e) {
@@ -2167,34 +2161,42 @@ export default class User extends AbleObject {
2167
2161
  this.$setNymph(nymph);
2168
2162
  throw e;
2169
2163
  }
2170
- if (success) {
2171
- try {
2172
- for (let callback of this.constructor
2173
- .afterDeleteCallbacks) {
2174
- if (callback) {
2175
- await callback(this);
2176
- }
2164
+ try {
2165
+ for (let callback of this.constructor
2166
+ .afterDeleteCallbacks) {
2167
+ if (callback) {
2168
+ await callback(this);
2177
2169
  }
2178
2170
  }
2179
- catch (e) {
2171
+ }
2172
+ catch (e) {
2173
+ try {
2180
2174
  await tnymph.rollback(transaction);
2181
- this.$setNymph(nymph);
2182
- throw e;
2183
2175
  }
2184
- success = await tnymph.commit(transaction);
2176
+ catch (e) {
2177
+ nymph.config.debugError('tilmeld', `Rollback of transaction ${transaction} failed, reason: ${e.message}`);
2178
+ }
2179
+ this.$setNymph(nymph);
2180
+ throw e;
2185
2181
  }
2186
- else {
2187
- await tnymph.rollback(transaction);
2182
+ let committed = false;
2183
+ try {
2184
+ committed = await tnymph.commit(transaction);
2185
+ }
2186
+ catch (e) {
2187
+ committed = false;
2188
2188
  }
2189
2189
  this.$setNymph(nymph);
2190
- return success;
2190
+ if (!committed) {
2191
+ throw new MethodFailedError('Transaction could not be committed.');
2192
+ }
2191
2193
  }
2192
2194
  /*
2193
2195
  * This should *never* be accessible on the client.
2194
2196
  */
2195
2197
  async $deleteSkipAC() {
2196
2198
  this.$skipAcWhenDeleting = true;
2197
- return await this.$delete();
2199
+ await this.$delete();
2198
2200
  }
2199
2201
  $tilmeldDeleteSkipAC() {
2200
2202
  if (this.$skipAcWhenDeleting) {