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

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/Group.ts CHANGED
@@ -11,6 +11,7 @@ import md5 from 'crypto-js/md5';
11
11
  import { difference } from 'lodash';
12
12
 
13
13
  import type Tilmeld from './Tilmeld';
14
+ import { enforceTilmeld } from './enforceTilmeld';
14
15
  import AbleObject from './AbleObject';
15
16
  import {
16
17
  BadDataError,
@@ -245,17 +246,39 @@ export default class Group extends AbleObject<GroupData> {
245
246
  }
246
247
 
247
248
  public $jsonAcceptData(input: EntityJson, allowConflict = false) {
248
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
249
+ const tilmeld = enforceTilmeld(this);
249
250
  this.$referenceWake();
250
251
 
251
252
  if (
252
- input.data.abilities?.indexOf('system/admin') !== -1 &&
253
- this.$data.abilities?.indexOf('system/admin') === -1 &&
254
- tilmeld.gatekeeper('tilmeld/admin') &&
253
+ 'abilities' in input.data &&
254
+ input.data.abilities?.includes('system/admin') !==
255
+ this.$data.abilities?.includes('system/admin') &&
255
256
  !tilmeld.gatekeeper('system/admin')
256
257
  ) {
257
258
  throw new BadDataError(
258
- "You don't have the authority to make this group a system admin."
259
+ "You don't have the authority to grant or revoke system/admin."
260
+ );
261
+ }
262
+
263
+ if (
264
+ 'abilities' in input.data &&
265
+ input.data.abilities?.includes('tilmeld/admin') !==
266
+ this.$data.abilities?.includes('tilmeld/admin') &&
267
+ !tilmeld.gatekeeper('system/admin')
268
+ ) {
269
+ throw new BadDataError(
270
+ "You don't have the authority to grant or revoke tilmeld/admin."
271
+ );
272
+ }
273
+
274
+ if (
275
+ 'abilities' in input.data &&
276
+ input.data.abilities?.includes('tilmeld/switch') !==
277
+ this.$data.abilities?.includes('tilmeld/switch') &&
278
+ !tilmeld.gatekeeper('system/admin')
279
+ ) {
280
+ throw new BadDataError(
281
+ "You don't have the authority to grant or revoke tilmeld/switch."
259
282
  );
260
283
  }
261
284
 
@@ -263,17 +286,39 @@ export default class Group extends AbleObject<GroupData> {
263
286
  }
264
287
 
265
288
  public $jsonAcceptPatch(patch: EntityPatch, allowConflict = false) {
266
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
289
+ const tilmeld = enforceTilmeld(this);
267
290
  this.$referenceWake();
268
291
 
269
292
  if (
270
- patch.set.abilities?.indexOf('system/admin') !== -1 &&
271
- this.$data.abilities?.indexOf('system/admin') === -1 &&
272
- tilmeld.gatekeeper('tilmeld/admin') &&
293
+ 'abilities' in patch.set &&
294
+ patch.set.abilities?.includes('system/admin') !==
295
+ this.$data.abilities?.includes('system/admin') &&
296
+ !tilmeld.gatekeeper('system/admin')
297
+ ) {
298
+ throw new BadDataError(
299
+ "You don't have the authority to grant or revoke system/admin."
300
+ );
301
+ }
302
+
303
+ if (
304
+ 'abilities' in patch.set &&
305
+ patch.set.abilities?.includes('tilmeld/admin') !==
306
+ this.$data.abilities?.includes('tilmeld/admin') &&
307
+ !tilmeld.gatekeeper('system/admin')
308
+ ) {
309
+ throw new BadDataError(
310
+ "You don't have the authority to grant or revoke tilmeld/admin."
311
+ );
312
+ }
313
+
314
+ if (
315
+ 'abilities' in patch.set &&
316
+ patch.set.abilities?.includes('tilmeld/switch') !==
317
+ this.$data.abilities?.includes('tilmeld/switch') &&
273
318
  !tilmeld.gatekeeper('system/admin')
274
319
  ) {
275
320
  throw new BadDataError(
276
- "You don't have the authority to make this group a system admin."
321
+ "You don't have the authority to grant or revoke tilmeld/switch."
277
322
  );
278
323
  }
279
324
 
@@ -291,7 +336,7 @@ export default class Group extends AbleObject<GroupData> {
291
336
  * @param givenUser User to update protection for. If undefined, will use the currently logged in user.
292
337
  */
293
338
  public $updateDataProtection(givenUser?: User & UserData) {
294
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
339
+ const tilmeld = enforceTilmeld(this);
295
340
  let user = givenUser ?? tilmeld.User.current();
296
341
 
297
342
  this.$privateData = [...Group.DEFAULT_PRIVATE_DATA];
@@ -326,7 +371,7 @@ export default class Group extends AbleObject<GroupData> {
326
371
  * @returns True or false.
327
372
  */
328
373
  public $isDescendant(givenGroup: (Group & GroupData) | string): boolean {
329
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
374
+ const tilmeld = enforceTilmeld(this);
330
375
  let group: Group & GroupData;
331
376
  if (typeof givenGroup === 'string') {
332
377
  group = tilmeld.Group.factorySync(givenGroup);
@@ -355,7 +400,7 @@ export default class Group extends AbleObject<GroupData> {
355
400
  * @returns An array of groups.
356
401
  */
357
402
  public async $getChildren() {
358
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
403
+ const tilmeld = enforceTilmeld(this);
359
404
  return await this.$nymph.getEntities(
360
405
  { class: tilmeld.Group },
361
406
  {
@@ -375,7 +420,7 @@ export default class Group extends AbleObject<GroupData> {
375
420
  public async $getDescendants(
376
421
  andSelf = false
377
422
  ): Promise<(Group & GroupData)[]> {
378
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
423
+ const tilmeld = enforceTilmeld(this);
379
424
  let groups: (Group & GroupData)[] = [];
380
425
  const entities = await this.$nymph.getEntities(
381
426
  { class: tilmeld.Group },
@@ -401,7 +446,7 @@ export default class Group extends AbleObject<GroupData> {
401
446
  * @returns An array of groups.
402
447
  */
403
448
  public $getDescendantsSync(andSelf = false): (Group & GroupData)[] {
404
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
449
+ const tilmeld = enforceTilmeld(this);
405
450
  let groups: (Group & GroupData)[] = [];
406
451
  let entity: EntityInterface | null;
407
452
  let offset = 0;
@@ -437,7 +482,7 @@ export default class Group extends AbleObject<GroupData> {
437
482
  * @returns The level of the group.
438
483
  */
439
484
  public $getLevel() {
440
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
485
+ const tilmeld = enforceTilmeld(this);
441
486
  let group = tilmeld.Group.factorySync(this.guid ?? undefined);
442
487
  let level = 0;
443
488
  while (group.parent != null && group.parent.cdate != null && level < 1024) {
@@ -460,7 +505,7 @@ export default class Group extends AbleObject<GroupData> {
460
505
  limit?: number,
461
506
  offset?: number
462
507
  ): Promise<(User & UserData)[]> {
463
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
508
+ const tilmeld = enforceTilmeld(this);
464
509
  let groups: (Group & GroupData)[] = [];
465
510
  if (descendants) {
466
511
  groups = await this.$getDescendants();
@@ -502,7 +547,7 @@ export default class Group extends AbleObject<GroupData> {
502
547
  result: boolean;
503
548
  message: string;
504
549
  }> {
505
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
550
+ const tilmeld = enforceTilmeld(this);
506
551
  if (!tilmeld.config.emailUsernames) {
507
552
  if (this.$data.groupname == null || !this.$data.groupname.length) {
508
553
  return { result: false, message: 'Please specify a groupname.' };
@@ -588,7 +633,7 @@ export default class Group extends AbleObject<GroupData> {
588
633
  * @returns An object with a boolean 'result' entry and a 'message' entry.
589
634
  */
590
635
  public async $checkEmail(): Promise<{ result: boolean; message: string }> {
591
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
636
+ const tilmeld = enforceTilmeld(this);
592
637
  if (this.$data.email === '') {
593
638
  return { result: true, message: '' };
594
639
  }
@@ -627,7 +672,7 @@ export default class Group extends AbleObject<GroupData> {
627
672
  }
628
673
 
629
674
  public async $save() {
630
- const tilmeld = this.$nymph.tilmeld as Tilmeld;
675
+ const tilmeld = enforceTilmeld(this);
631
676
  if (this.$data.groupname == null || !this.$data.groupname.trim().length) {
632
677
  return false;
633
678
  }
@@ -671,10 +716,12 @@ export default class Group extends AbleObject<GroupData> {
671
716
  delete this.$data.unverifiedSecondary;
672
717
  }
673
718
 
674
- // Groups should never have 'system/admin' or 'tilmeld/admin' abilities.
719
+ // Groups should never have 'system/admin', 'tilmeld/admin', or
720
+ // 'tilmeld/switch' abilities.
675
721
  this.$data.abilities = difference(this.$data.abilities ?? [], [
676
722
  'system/admin',
677
723
  'tilmeld/admin',
724
+ 'tilmeld/switch',
678
725
  null,
679
726
  undefined,
680
727
  ]) as string[];
@@ -746,7 +793,7 @@ export default class Group extends AbleObject<GroupData> {
746
793
  }
747
794
 
748
795
  public async $delete() {
749
- let tilmeld = this.$nymph.tilmeld as Tilmeld;
796
+ let tilmeld = enforceTilmeld(this);
750
797
  if (!tilmeld.gatekeeper('tilmeld/admin')) {
751
798
  throw new BadDataError("You don't have the authority to delete groups.");
752
799
  }
@@ -755,7 +802,7 @@ export default class Group extends AbleObject<GroupData> {
755
802
  const nymph = this.$nymph;
756
803
  const tnymph = await nymph.startTransaction(transaction);
757
804
  this.$nymph = tnymph;
758
- tilmeld = this.$nymph.tilmeld as Tilmeld;
805
+ tilmeld = enforceTilmeld(this);
759
806
 
760
807
  // Delete descendants.
761
808
  const descendants = await this.$getDescendants();