@nymphjs/tilmeld 1.0.0-beta.50 → 1.0.0-beta.52

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/src/User.ts CHANGED
@@ -619,8 +619,8 @@ export default class User extends AbleObject<UserData> {
619
619
  const fromAbilities = user.abilities || [];
620
620
 
621
621
  if (
622
- fromAbilities.indexOf('tilmeld/switch') === -1 &&
623
- fromAbilities.indexOf('system/admin') === -1
622
+ !fromAbilities.includes('tilmeld/switch') &&
623
+ !fromAbilities.includes('system/admin')
624
624
  ) {
625
625
  return {
626
626
  result: false,
@@ -629,8 +629,8 @@ export default class User extends AbleObject<UserData> {
629
629
  }
630
630
 
631
631
  if (
632
- toAbilities.indexOf('system/admin') !== -1 ||
633
- toAbilities.indexOf('tilmeld/admin') !== -1
632
+ toAbilities.includes('system/admin') ||
633
+ toAbilities.includes('tilmeld/admin')
634
634
  ) {
635
635
  return { result: false, message: "You can't switch to an admin." };
636
636
  }
@@ -886,8 +886,8 @@ export default class User extends AbleObject<UserData> {
886
886
 
887
887
  if (
888
888
  user != null &&
889
- (abilities.indexOf('tilmeld/admin') !== -1 ||
890
- abilities.indexOf('system/admin') !== -1)
889
+ (abilities.includes('tilmeld/admin') ||
890
+ abilities.includes('system/admin'))
891
891
  ) {
892
892
  // Users who can edit other users can see most of their data.
893
893
  this.$privateData = ['password', 'salt', 'totpSecret'];
@@ -900,16 +900,16 @@ export default class User extends AbleObject<UserData> {
900
900
  this.$allowlistData.push('username');
901
901
  }
902
902
  this.$allowlistData.push('avatar');
903
- if (tilmeld.config.userFields.indexOf('name') !== -1) {
903
+ if (tilmeld.config.userFields.includes('name')) {
904
904
  this.$allowlistData.push('nameFirst');
905
905
  this.$allowlistData.push('nameMiddle');
906
906
  this.$allowlistData.push('nameLast');
907
907
  this.$allowlistData.push('name');
908
908
  }
909
- if (tilmeld.config.userFields.indexOf('email') !== -1) {
909
+ if (tilmeld.config.userFields.includes('email')) {
910
910
  this.$allowlistData.push('email');
911
911
  }
912
- if (tilmeld.config.userFields.indexOf('phone') !== -1) {
912
+ if (tilmeld.config.userFields.includes('phone')) {
913
913
  this.$allowlistData.push('phone');
914
914
  }
915
915
  this.$privateData = [
@@ -946,8 +946,8 @@ export default class User extends AbleObject<UserData> {
946
946
  if (
947
947
  user != null &&
948
948
  !isCurrentUser &&
949
- (abilities.indexOf('tilmeld/switch') !== -1 ||
950
- abilities.indexOf('system/admin') !== -1)
949
+ (abilities.includes('tilmeld/switch') ||
950
+ abilities.includes('system/admin'))
951
951
  ) {
952
952
  // Users with this ability can switch to other users.
953
953
  this.$clientEnabledMethods.push('$switchUser');
@@ -1733,7 +1733,7 @@ export default class User extends AbleObject<UserData> {
1733
1733
  }
1734
1734
 
1735
1735
  this.$password(data.password);
1736
- if (tilmeld.config.regFields.indexOf('name') !== -1) {
1736
+ if (tilmeld.config.regFields.includes('name')) {
1737
1737
  this.$data.name =
1738
1738
  this.$data.nameFirst +
1739
1739
  (this.$data.nameMiddle == null ? '' : ' ' + this.$data.nameMiddle) +
@@ -1781,8 +1781,12 @@ export default class User extends AbleObject<UserData> {
1781
1781
  generatedPrimaryGroup = await tilmeld.Group.factory();
1782
1782
  generatedPrimaryGroup.groupname = this.$data.username;
1783
1783
  generatedPrimaryGroup.avatar = this.$data.avatar;
1784
- generatedPrimaryGroup.name = this.$data.name;
1785
- generatedPrimaryGroup.email = this.$data.email;
1784
+ if (tilmeld.config.userFields.includes('name')) {
1785
+ generatedPrimaryGroup.name = this.$data.name;
1786
+ }
1787
+ if (tilmeld.config.userFields.includes('email')) {
1788
+ generatedPrimaryGroup.email = this.$data.email;
1789
+ }
1786
1790
  const parent = await tnymph.getEntity(
1787
1791
  { class: tilmeld.Group },
1788
1792
  {
@@ -1818,7 +1822,11 @@ export default class User extends AbleObject<UserData> {
1818
1822
  }
1819
1823
 
1820
1824
  // Add secondary groups.
1821
- if (tilmeld.config.verifyEmail && tilmeld.config.unverifiedAccess) {
1825
+ if (
1826
+ tilmeld.config.userFields.includes('email') &&
1827
+ tilmeld.config.verifyEmail &&
1828
+ tilmeld.config.unverifiedAccess
1829
+ ) {
1822
1830
  // Add the default secondaries for unverified users.
1823
1831
  this.$data.groups = await tnymph.getEntities(
1824
1832
  { class: tilmeld.Group },
@@ -1838,7 +1846,10 @@ export default class User extends AbleObject<UserData> {
1838
1846
  );
1839
1847
  }
1840
1848
 
1841
- if (tilmeld.config.verifyEmail) {
1849
+ if (
1850
+ tilmeld.config.userFields.includes('email') &&
1851
+ tilmeld.config.verifyEmail
1852
+ ) {
1842
1853
  // The user will be enabled after verifying their e-mail address.
1843
1854
  if (!tilmeld.config.unverifiedAccess) {
1844
1855
  this.$data.enabled = false;
@@ -1907,7 +1918,8 @@ export default class User extends AbleObject<UserData> {
1907
1918
  if (
1908
1919
  tilmeld.config.verifyEmail &&
1909
1920
  !tilmeld.config.unverifiedAccess &&
1910
- !madeAdmin
1921
+ !madeAdmin &&
1922
+ tilmeld.config.userFields.includes('email')
1911
1923
  ) {
1912
1924
  message +=
1913
1925
  `Almost there. An email has been sent to ${this.$data.email} ` +
@@ -1915,7 +1927,8 @@ export default class User extends AbleObject<UserData> {
1915
1927
  } else if (
1916
1928
  tilmeld.config.verifyEmail &&
1917
1929
  tilmeld.config.unverifiedAccess &&
1918
- !madeAdmin
1930
+ !madeAdmin &&
1931
+ tilmeld.config.userFields.includes('email')
1919
1932
  ) {
1920
1933
  if (!(await tilmeld.login(this, true))) {
1921
1934
  throw new Error('An error occurred trying to log you in.');
@@ -2006,49 +2019,88 @@ export default class User extends AbleObject<UserData> {
2006
2019
  this.$data.enabled = !!this.$data.enabled;
2007
2020
 
2008
2021
  // Clear empty values.
2009
- if (this.$data.nameFirst === '') {
2022
+ if (
2023
+ this.$data.nameFirst === '' ||
2024
+ !tilmeld.config.userFields.includes('name')
2025
+ ) {
2010
2026
  delete this.$data.nameFirst;
2011
2027
  }
2012
- if (this.$data.nameMiddle === '') {
2028
+ if (
2029
+ this.$data.nameMiddle === '' ||
2030
+ !tilmeld.config.userFields.includes('name')
2031
+ ) {
2013
2032
  delete this.$data.nameMiddle;
2014
2033
  }
2015
- if (this.$data.nameLast === '') {
2034
+ if (
2035
+ this.$data.nameLast === '' ||
2036
+ !tilmeld.config.userFields.includes('name')
2037
+ ) {
2016
2038
  delete this.$data.nameLast;
2017
2039
  }
2018
- if (this.$data.name === '') {
2040
+ if (this.$data.name === '' || !tilmeld.config.userFields.includes('name')) {
2019
2041
  delete this.$data.name;
2020
2042
  }
2021
- if (this.$data.email === '') {
2043
+ if (
2044
+ this.$data.email === '' ||
2045
+ !tilmeld.config.userFields.includes('email')
2046
+ ) {
2022
2047
  delete this.$data.email;
2023
2048
  }
2024
2049
  if (this.$data.avatar === '') {
2025
2050
  delete this.$data.avatar;
2026
2051
  }
2027
- if (this.$data.phone === '') {
2052
+ if (
2053
+ this.$data.phone === '' ||
2054
+ !tilmeld.config.userFields.includes('phone')
2055
+ ) {
2028
2056
  delete this.$data.phone;
2029
2057
  }
2030
- if (this.$data.secret === '') {
2058
+ if (
2059
+ this.$data.secret === '' ||
2060
+ !tilmeld.config.userFields.includes('email')
2061
+ ) {
2031
2062
  delete this.$data.secret;
2032
2063
  }
2033
- if (this.$data.emailChangeDate === 0) {
2064
+ if (
2065
+ this.$data.emailChangeDate === 0 ||
2066
+ !tilmeld.config.userFields.includes('email')
2067
+ ) {
2034
2068
  delete this.$data.emailChangeDate;
2035
2069
  }
2036
- if (this.$data.newEmailSecret === '') {
2070
+ if (
2071
+ this.$data.newEmailSecret === '' ||
2072
+ !tilmeld.config.userFields.includes('email')
2073
+ ) {
2037
2074
  delete this.$data.newEmailSecret;
2038
2075
  }
2039
- if (this.$data.newEmailAddress === '') {
2076
+ if (
2077
+ this.$data.newEmailAddress === '' ||
2078
+ !tilmeld.config.userFields.includes('email')
2079
+ ) {
2040
2080
  delete this.$data.newEmailAddress;
2041
2081
  }
2042
- if (this.$data.cancelEmailSecret === '') {
2082
+ if (
2083
+ this.$data.cancelEmailSecret === '' ||
2084
+ !tilmeld.config.userFields.includes('email')
2085
+ ) {
2043
2086
  delete this.$data.cancelEmailSecret;
2044
2087
  }
2045
- if (this.$data.cancelEmailAddress === '') {
2088
+ if (
2089
+ this.$data.cancelEmailAddress === '' ||
2090
+ !tilmeld.config.userFields.includes('email')
2091
+ ) {
2046
2092
  delete this.$data.cancelEmailAddress;
2047
2093
  }
2048
- if (this.$data.recoverSecret === '') {
2094
+ if (
2095
+ this.$data.recoverSecret === '' ||
2096
+ !tilmeld.config.userFields.includes('email')
2097
+ ) {
2049
2098
  delete this.$data.recoverSecret;
2050
2099
  }
2051
- if (this.$data.recoverSecretDate === 0) {
2100
+ if (
2101
+ this.$data.recoverSecretDate === 0 ||
2102
+ !tilmeld.config.userFields.includes('email')
2103
+ ) {
2052
2104
  delete this.$data.recoverSecretDate;
2053
2105
  }
2054
2106
  if (this.$data.password === '') {
@@ -2066,7 +2118,10 @@ export default class User extends AbleObject<UserData> {
2066
2118
  if (!unCheck.result) {
2067
2119
  throw new BadUsernameError(unCheck.message);
2068
2120
  }
2069
- if (!tilmeld.config.emailUsernames) {
2121
+ if (
2122
+ !tilmeld.config.emailUsernames &&
2123
+ tilmeld.config.userFields.includes('email')
2124
+ ) {
2070
2125
  const emCheck = await this.$checkEmail();
2071
2126
  if (!emCheck.result) {
2072
2127
  throw new BadEmailError(emCheck.message);
@@ -2074,7 +2129,10 @@ export default class User extends AbleObject<UserData> {
2074
2129
  }
2075
2130
 
2076
2131
  // Email changes.
2077
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
2132
+ if (
2133
+ !tilmeld.gatekeeper('tilmeld/admin') &&
2134
+ tilmeld.config.userFields.includes('email')
2135
+ ) {
2078
2136
  // The user isn't an admin, so email address changes should contain some
2079
2137
  // security measures.
2080
2138
  if (tilmeld.config.verifyEmail) {
@@ -2082,7 +2140,7 @@ export default class User extends AbleObject<UserData> {
2082
2140
  if (this.guid == null) {
2083
2141
  // If this is the first user, they'll be made an admin, so they don't
2084
2142
  // need to verify.
2085
- if ((this.$data.abilities?.indexOf('system/admin') ?? -1) === -1) {
2143
+ if (!this.$data.abilities?.includes('system/admin')) {
2086
2144
  this.$data.secret = nanoid();
2087
2145
  sendVerification = true;
2088
2146
  }
@@ -2161,7 +2219,7 @@ export default class User extends AbleObject<UserData> {
2161
2219
  }
2162
2220
 
2163
2221
  try {
2164
- tilmeld.config.validatorUser(this);
2222
+ tilmeld.config.validatorUser(tilmeld, this);
2165
2223
  } catch (e: any) {
2166
2224
  throw new BadDataError(e?.message);
2167
2225
  }
@@ -2180,9 +2238,15 @@ export default class User extends AbleObject<UserData> {
2180
2238
  // Update the user's generated primary group.
2181
2239
  group.groupname = this.$data.username;
2182
2240
  group.avatar = this.$data.avatar;
2183
- group.email = this.$data.email;
2184
- group.name = this.$data.name;
2185
- group.phone = this.$data.phone;
2241
+ if (tilmeld.config.userFields.includes('email')) {
2242
+ group.email = this.$data.email;
2243
+ }
2244
+ if (tilmeld.config.userFields.includes('name')) {
2245
+ group.name = this.$data.name;
2246
+ }
2247
+ if (tilmeld.config.userFields.includes('phone')) {
2248
+ group.phone = this.$data.phone;
2249
+ }
2186
2250
  await group.$saveSkipAC();
2187
2251
  this.$data.group = group;
2188
2252
  } catch (e: any) {
package/src/conf/d.ts CHANGED
@@ -308,9 +308,9 @@ export interface Config {
308
308
  /**
309
309
  * The validator used to check groups before saving.
310
310
  */
311
- validatorGroup: (group: Group & GroupData) => void;
311
+ validatorGroup: (tilmeld: Tilmeld, group: Group & GroupData) => void;
312
312
  /**
313
313
  * The validator used to check users before saving.
314
314
  */
315
- validatorUser: (user: User & UserData) => void;
315
+ validatorUser: (tilmeld: Tilmeld, user: User & UserData) => void;
316
316
  }
@@ -170,25 +170,43 @@ export default {
170
170
  }
171
171
  },
172
172
  userRegisteredRecipient: null,
173
- validatorGroup: (group) => {
173
+ validatorGroup: (tilmeld, group) => {
174
+ const nameFields = tilmeld.config.userFields.includes('name')
175
+ ? {
176
+ name: Joi.string()
177
+ .trim(false)
178
+ .pattern(/[\x01-\x1F\x7F]/, {
179
+ name: 'control characters',
180
+ invert: true,
181
+ })
182
+ .max(512)
183
+ .required(),
184
+ }
185
+ : {};
186
+ const emailFields = tilmeld.config.userFields.includes('email')
187
+ ? {
188
+ email: Joi.string()
189
+ .trim(false)
190
+ .email({ minDomainSegments: 1, tlds: false })
191
+ .required(),
192
+ }
193
+ : {};
194
+ const phoneFields = tilmeld.config.userFields.includes('phone')
195
+ ? {
196
+ phone: Joi.string()
197
+ .trim(false)
198
+ .pattern(/^[0-9]+$/, 'numbers'),
199
+ }
200
+ : {};
174
201
  Joi.attempt(
175
202
  group.$getValidatable(),
176
203
  Joi.object().keys({
177
204
  ...nymphJoiProps,
205
+ ...nameFields,
206
+ ...emailFields,
207
+ ...phoneFields,
178
208
  groupname: Joi.string().trim(false).required(),
179
209
  enabled: Joi.boolean().required(),
180
- email: Joi.string()
181
- .trim(false)
182
- .email({ minDomainSegments: 1, tlds: false })
183
- .required(),
184
- name: Joi.string()
185
- .trim(false)
186
- .pattern(/[\x01-\x1F\x7F]/, {
187
- name: 'control characters',
188
- invert: true,
189
- })
190
- .max(512)
191
- .required(),
192
210
  avatar: Joi.string()
193
211
  .trim(false)
194
212
  .uri()
@@ -197,9 +215,6 @@ export default {
197
215
  invert: true,
198
216
  })
199
217
  .max(1024),
200
- phone: Joi.string()
201
- .trim(false)
202
- .pattern(/^[0-9]+$/, 'numbers'),
203
218
  parent: Joi.object().instance(Group),
204
219
  user: Joi.object().instance(User),
205
220
  abilities: Joi.array().items(
@@ -217,47 +232,101 @@ export default {
217
232
  'Invalid Group: ',
218
233
  );
219
234
  },
220
- validatorUser: (user) => {
235
+ validatorUser: (tilmeld, user) => {
236
+ const nameFields = tilmeld.config.userFields.includes('name')
237
+ ? {
238
+ nameFirst: Joi.string()
239
+ .trim(false)
240
+ .pattern(/[\x01-\x1F\x7F]/, {
241
+ name: 'control characters',
242
+ invert: true,
243
+ })
244
+ .max(512)
245
+ .required(),
246
+ nameMiddle: Joi.string()
247
+ .trim(false)
248
+ .pattern(/[\x01-\x1F\x7F]/, {
249
+ name: 'control characters',
250
+ invert: true,
251
+ })
252
+ .max(512),
253
+ nameLast: Joi.string()
254
+ .trim(false)
255
+ .pattern(/[\x01-\x1F\x7F]/, {
256
+ name: 'control characters',
257
+ invert: true,
258
+ })
259
+ .max(512),
260
+ name: Joi.string()
261
+ .trim(false)
262
+ .pattern(/[\x01-\x1F\x7F]/, {
263
+ name: 'control characters',
264
+ invert: true,
265
+ })
266
+ .max(512)
267
+ .required(),
268
+ }
269
+ : {};
270
+ const emailFields = tilmeld.config.userFields.includes('email')
271
+ ? {
272
+ email: Joi.string()
273
+ .trim(false)
274
+ .email({ minDomainSegments: 1, tlds: false })
275
+ .required(),
276
+ secret: Joi.string()
277
+ .trim(false)
278
+ .pattern(/[\x01-\x1F\x7F]/, {
279
+ name: 'control characters',
280
+ invert: true,
281
+ })
282
+ .length(21),
283
+ emailChangeDate: Joi.number(),
284
+ newEmailSecret: Joi.string()
285
+ .trim(false)
286
+ .pattern(/[\x01-\x1F\x7F]/, {
287
+ name: 'control characters',
288
+ invert: true,
289
+ })
290
+ .length(21),
291
+ newEmailAddress: Joi.string()
292
+ .trim(false)
293
+ .email({ minDomainSegments: 1, tlds: false }),
294
+ cancelEmailSecret: Joi.string()
295
+ .trim(false)
296
+ .pattern(/[\x01-\x1F\x7F]/, {
297
+ name: 'control characters',
298
+ invert: true,
299
+ })
300
+ .length(21),
301
+ cancelEmailAddress: Joi.string()
302
+ .trim(false)
303
+ .email({ minDomainSegments: 1, tlds: false }),
304
+ recoverSecret: Joi.string()
305
+ .trim(false)
306
+ .pattern(/[\x01-\x1F\x7F]/, {
307
+ name: 'control characters',
308
+ invert: true,
309
+ })
310
+ .length(10),
311
+ recoverSecretDate: Joi.number(),
312
+ }
313
+ : {};
314
+ const phoneFields = tilmeld.config.userFields.includes('phone')
315
+ ? {
316
+ phone: Joi.string()
317
+ .trim(false)
318
+ .pattern(/^[0-9]+$/, 'numbers'),
319
+ }
320
+ : {};
221
321
  Joi.attempt(
222
322
  user.$getValidatable(),
223
323
  Joi.object().keys({
224
324
  ...nymphJoiProps,
325
+ ...nameFields,
326
+ ...emailFields,
327
+ ...phoneFields,
225
328
  username: Joi.string().trim(false).required(),
226
329
  enabled: Joi.boolean().required(),
227
- email: Joi.string()
228
- .trim(false)
229
- .email({ minDomainSegments: 1, tlds: false })
230
- .required(),
231
- nameFirst: Joi.string()
232
- .trim(false)
233
- .pattern(/[\x01-\x1F\x7F]/, {
234
- name: 'control characters',
235
- invert: true,
236
- })
237
- .max(512)
238
- .required(),
239
- nameMiddle: Joi.string()
240
- .trim(false)
241
- .pattern(/[\x01-\x1F\x7F]/, {
242
- name: 'control characters',
243
- invert: true,
244
- })
245
- .max(512),
246
- nameLast: Joi.string()
247
- .trim(false)
248
- .pattern(/[\x01-\x1F\x7F]/, {
249
- name: 'control characters',
250
- invert: true,
251
- })
252
- .max(512),
253
- name: Joi.string()
254
- .trim(false)
255
- .pattern(/[\x01-\x1F\x7F]/, {
256
- name: 'control characters',
257
- invert: true,
258
- })
259
- .max(512)
260
- .required(),
261
330
  avatar: Joi.string()
262
331
  .trim(false)
263
332
  .uri()
@@ -266,9 +335,6 @@ export default {
266
335
  invert: true,
267
336
  })
268
337
  .max(1024),
269
- phone: Joi.string()
270
- .trim(false)
271
- .pattern(/^[0-9]+$/, 'numbers'),
272
338
  group: Joi.object().instance(Group),
273
339
  groups: Joi.array().items(Joi.object().instance(Group)).required(),
274
340
  abilities: Joi.array().items(
@@ -280,42 +346,6 @@ export default {
280
346
  .max(256),
281
347
  ),
282
348
  inheritAbilities: Joi.boolean(),
283
- secret: Joi.string()
284
- .trim(false)
285
- .pattern(/[\x01-\x1F\x7F]/, {
286
- name: 'control characters',
287
- invert: true,
288
- })
289
- .length(21),
290
- emailChangeDate: Joi.number(),
291
- newEmailSecret: Joi.string()
292
- .trim(false)
293
- .pattern(/[\x01-\x1F\x7F]/, {
294
- name: 'control characters',
295
- invert: true,
296
- })
297
- .length(21),
298
- newEmailAddress: Joi.string()
299
- .trim(false)
300
- .email({ minDomainSegments: 1, tlds: false }),
301
- cancelEmailSecret: Joi.string()
302
- .trim(false)
303
- .pattern(/[\x01-\x1F\x7F]/, {
304
- name: 'control characters',
305
- invert: true,
306
- })
307
- .length(21),
308
- cancelEmailAddress: Joi.string()
309
- .trim(false)
310
- .email({ minDomainSegments: 1, tlds: false }),
311
- recoverSecret: Joi.string()
312
- .trim(false)
313
- .pattern(/[\x01-\x1F\x7F]/, {
314
- name: 'control characters',
315
- invert: true,
316
- })
317
- .length(10),
318
- recoverSecretDate: Joi.number(),
319
349
  salt: Joi.string().trim(false),
320
350
  password: Joi.string().trim(false).required(),
321
351
  revokeTokenDate: Joi.number(),