@nymphjs/tilmeld 1.0.0-beta.40 → 1.0.0-beta.41

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
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  EntityData,
3
- EntityInterface,
4
3
  EntityJson,
5
4
  EntityPatch,
6
5
  Options,
@@ -140,19 +139,17 @@ export default class Group extends AbleObject<GroupData> {
140
139
  return entity;
141
140
  }
142
141
 
143
- static factorySync(guid?: string): Group & GroupData {
144
- return super.factorySync(guid) as Group & GroupData;
142
+ static factorySync(): Group & GroupData {
143
+ return super.factorySync() as Group & GroupData;
145
144
  }
146
145
 
147
- constructor(guid?: string) {
148
- super(guid);
146
+ constructor() {
147
+ super();
149
148
 
150
- if (this.guid == null) {
151
- // Defaults.
152
- this.$data.enabled = true;
153
- this.$data.abilities = [];
154
- this.$updateDataProtection();
155
- }
149
+ // Defaults.
150
+ this.$data.enabled = true;
151
+ this.$data.abilities = [];
152
+ this.$updateDataProtection();
156
153
  }
157
154
 
158
155
  /**
@@ -167,7 +164,7 @@ export default class Group extends AbleObject<GroupData> {
167
164
  selectors?: Selector[]
168
165
  ) {
169
166
  const tilmeld = enforceTilmeld(this);
170
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
167
+ if (!(await tilmeld.gatekeeper('tilmeld/admin'))) {
171
168
  throw new Error("You don't have permission to do that.");
172
169
  }
173
170
 
@@ -190,7 +187,7 @@ export default class Group extends AbleObject<GroupData> {
190
187
  selectors?: Selector[]
191
188
  ) {
192
189
  const tilmeld = enforceTilmeld(this);
193
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
190
+ if (!(await tilmeld.gatekeeper('tilmeld/admin'))) {
194
191
  throw new Error("You don't have permission to do that.");
195
192
  }
196
193
 
@@ -218,16 +215,24 @@ export default class Group extends AbleObject<GroupData> {
218
215
  ...selectors
219
216
  );
220
217
  if (highestParent !== true) {
221
- assignableGroups = assignableGroups.filter((group) => {
222
- let curGroup = group;
223
- while (curGroup.parent != null && curGroup.parent.cdate != null) {
224
- if (curGroup.parent.guid === highestParent) {
225
- return true;
226
- }
227
- curGroup = curGroup.parent;
228
- }
229
- return false;
230
- });
218
+ assignableGroups = (
219
+ await Promise.all(
220
+ assignableGroups.map(async (group) => {
221
+ let curGroup = group;
222
+ let parent = curGroup.parent;
223
+ await parent?.$wake();
224
+ while (parent != null && parent?.cdate != null) {
225
+ if (parent?.guid === highestParent) {
226
+ return group;
227
+ }
228
+ curGroup = parent;
229
+ parent = curGroup.parent;
230
+ await parent?.$wake();
231
+ }
232
+ return null;
233
+ })
234
+ )
235
+ ).filter((group) => group != null) as (Group & GroupData)[];
231
236
  }
232
237
  return assignableGroups;
233
238
  }
@@ -248,13 +253,14 @@ export default class Group extends AbleObject<GroupData> {
248
253
 
249
254
  public $jsonAcceptData(input: EntityJson, allowConflict = false) {
250
255
  const tilmeld = enforceTilmeld(this);
251
- this.$referenceWake();
256
+ this.$check();
252
257
 
253
258
  if (
254
259
  'abilities' in input.data &&
255
260
  input.data.abilities?.includes('system/admin') !==
256
261
  this.$data.abilities?.includes('system/admin') &&
257
- !tilmeld.gatekeeper('system/admin')
262
+ (!tilmeld.currentUser ||
263
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
258
264
  ) {
259
265
  throw new BadDataError(
260
266
  "You don't have the authority to grant or revoke system/admin."
@@ -265,7 +271,8 @@ export default class Group extends AbleObject<GroupData> {
265
271
  'abilities' in input.data &&
266
272
  input.data.abilities?.includes('tilmeld/admin') !==
267
273
  this.$data.abilities?.includes('tilmeld/admin') &&
268
- !tilmeld.gatekeeper('system/admin')
274
+ (!tilmeld.currentUser ||
275
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
269
276
  ) {
270
277
  throw new BadDataError(
271
278
  "You don't have the authority to grant or revoke tilmeld/admin."
@@ -276,7 +283,8 @@ export default class Group extends AbleObject<GroupData> {
276
283
  'abilities' in input.data &&
277
284
  input.data.abilities?.includes('tilmeld/switch') !==
278
285
  this.$data.abilities?.includes('tilmeld/switch') &&
279
- !tilmeld.gatekeeper('system/admin')
286
+ (!tilmeld.currentUser ||
287
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
280
288
  ) {
281
289
  throw new BadDataError(
282
290
  "You don't have the authority to grant or revoke tilmeld/switch."
@@ -288,13 +296,14 @@ export default class Group extends AbleObject<GroupData> {
288
296
 
289
297
  public $jsonAcceptPatch(patch: EntityPatch, allowConflict = false) {
290
298
  const tilmeld = enforceTilmeld(this);
291
- this.$referenceWake();
299
+ this.$check();
292
300
 
293
301
  if (
294
302
  'abilities' in patch.set &&
295
303
  patch.set.abilities?.includes('system/admin') !==
296
304
  this.$data.abilities?.includes('system/admin') &&
297
- !tilmeld.gatekeeper('system/admin')
305
+ (!tilmeld.currentUser ||
306
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
298
307
  ) {
299
308
  throw new BadDataError(
300
309
  "You don't have the authority to grant or revoke system/admin."
@@ -305,7 +314,8 @@ export default class Group extends AbleObject<GroupData> {
305
314
  'abilities' in patch.set &&
306
315
  patch.set.abilities?.includes('tilmeld/admin') !==
307
316
  this.$data.abilities?.includes('tilmeld/admin') &&
308
- !tilmeld.gatekeeper('system/admin')
317
+ (!tilmeld.currentUser ||
318
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
309
319
  ) {
310
320
  throw new BadDataError(
311
321
  "You don't have the authority to grant or revoke tilmeld/admin."
@@ -316,7 +326,8 @@ export default class Group extends AbleObject<GroupData> {
316
326
  'abilities' in patch.set &&
317
327
  patch.set.abilities?.includes('tilmeld/switch') !==
318
328
  this.$data.abilities?.includes('tilmeld/switch') &&
319
- !tilmeld.gatekeeper('system/admin')
329
+ (!tilmeld.currentUser ||
330
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
320
331
  ) {
321
332
  throw new BadDataError(
322
333
  "You don't have the authority to grant or revoke tilmeld/switch."
@@ -358,7 +369,7 @@ export default class Group extends AbleObject<GroupData> {
358
369
  } else if (
359
370
  this.$data.user != null &&
360
371
  user != null &&
361
- this.$data.user.$is(user)
372
+ this.$data.user.guid === user.guid
362
373
  ) {
363
374
  // Users can see their group's data.
364
375
  this.$privateData = [];
@@ -371,11 +382,13 @@ export default class Group extends AbleObject<GroupData> {
371
382
  * @param group The group, or the group's GUID.
372
383
  * @returns True or false.
373
384
  */
374
- public $isDescendant(givenGroup: (Group & GroupData) | string): boolean {
385
+ public async $isDescendant(
386
+ givenGroup: (Group & GroupData) | string
387
+ ): Promise<boolean> {
375
388
  const tilmeld = enforceTilmeld(this);
376
389
  let group: Group & GroupData;
377
390
  if (typeof givenGroup === 'string') {
378
- group = tilmeld.Group.factorySync(givenGroup);
391
+ group = await tilmeld.Group.factory(givenGroup);
379
392
  } else {
380
393
  group = givenGroup;
381
394
  }
@@ -383,13 +396,15 @@ export default class Group extends AbleObject<GroupData> {
383
396
  return false;
384
397
  }
385
398
  // Check to see if the group is a descendant of the given group.
386
- if (this.$data.parent == null) {
399
+ const parent = this.$data.parent;
400
+ await parent?.$wake();
401
+ if (parent == null) {
387
402
  return false;
388
403
  }
389
- if (this.$data.parent.$is(group)) {
404
+ if (parent.$is(group)) {
390
405
  return true;
391
406
  }
392
- if (this.$data.parent.$isDescendant(group)) {
407
+ if (await parent.$isDescendant(group)) {
393
408
  return true;
394
409
  }
395
410
  return false;
@@ -440,37 +455,6 @@ export default class Group extends AbleObject<GroupData> {
440
455
  return groups;
441
456
  }
442
457
 
443
- /**
444
- * Gets an array of the group's descendant groups.
445
- *
446
- * @param andSelf Include this group in the returned array.
447
- * @returns An array of groups.
448
- */
449
- public $getDescendantsSync(andSelf = false): (Group & GroupData)[] {
450
- const tilmeld = enforceTilmeld(this);
451
- let groups: (Group & GroupData)[] = [];
452
- let entity: EntityInterface | null;
453
- let offset = 0;
454
- do {
455
- entity = this.$nymph.driver.getEntitySync(
456
- { class: tilmeld.Group, offset },
457
- {
458
- type: '&',
459
- equal: ['enabled', true],
460
- ref: ['parent', this],
461
- }
462
- );
463
- if (entity != null) {
464
- groups = entity.$getDescendantsSync(true).concat(groups);
465
- }
466
- offset++;
467
- } while (entity != null);
468
- if (andSelf) {
469
- groups.push(this);
470
- }
471
- return groups;
472
- }
473
-
474
458
  /**
475
459
  * Get the number of parents the group has.
476
460
  *
@@ -482,13 +466,17 @@ export default class Group extends AbleObject<GroupData> {
482
466
  *
483
467
  * @returns The level of the group.
484
468
  */
485
- public $getLevel() {
469
+ public async $getLevel() {
486
470
  const tilmeld = enforceTilmeld(this);
487
- let group = tilmeld.Group.factorySync(this.guid ?? undefined);
471
+ let group = await tilmeld.Group.factory(this.guid ?? undefined);
472
+ let parent = group.parent;
473
+ await parent?.$wake();
488
474
  let level = 0;
489
- while (group.parent != null && group.parent.cdate != null && level < 1024) {
475
+ while (parent != null && parent.cdate != null && level < 1024) {
490
476
  level++;
491
- group = group.parent;
477
+ group = parent;
478
+ parent = group.parent;
479
+ await parent?.$wake();
492
480
  }
493
481
  return level;
494
482
  }
@@ -740,11 +728,13 @@ export default class Group extends AbleObject<GroupData> {
740
728
  }
741
729
 
742
730
  // Validate group parent. Make sure it's not a descendant of this group.
731
+ const parent = this.$data.parent;
732
+ await parent?.$wake();
743
733
  if (
744
- this.$data.parent &&
745
- (this.$data.parent.cdate == null ||
746
- this.$is(this.$data.parent) ||
747
- this.$data.parent.$isDescendant(this))
734
+ parent &&
735
+ (parent.cdate == null ||
736
+ this.$is(parent) ||
737
+ (await parent.$isDescendant(this)))
748
738
  ) {
749
739
  throw new BadDataError(
750
740
  "Group parent can't be itself or descendant of itself."
@@ -795,7 +785,7 @@ export default class Group extends AbleObject<GroupData> {
795
785
 
796
786
  public async $delete() {
797
787
  let tilmeld = enforceTilmeld(this);
798
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
788
+ if (!(await tilmeld.gatekeeper('tilmeld/admin'))) {
799
789
  throw new BadDataError("You don't have the authority to delete groups.");
800
790
  }
801
791