@nymphjs/tilmeld 1.0.0-beta.3 → 1.0.0-beta.31

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