@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/User.ts CHANGED
@@ -312,21 +312,19 @@ export default class User extends AbleObject<UserData> {
312
312
  return entity;
313
313
  }
314
314
 
315
- static factorySync(guid?: string): User & UserData {
316
- return super.factorySync(guid) as User & UserData;
315
+ static factorySync(): User & UserData {
316
+ return super.factorySync() as User & UserData;
317
317
  }
318
318
 
319
- constructor(guid?: string) {
320
- super(guid);
319
+ constructor() {
320
+ super();
321
321
 
322
- if (this.guid == null) {
323
- // Defaults.
324
- this.$data.enabled = true;
325
- this.$data.abilities = [];
326
- this.$data.groups = [];
327
- this.$data.inheritAbilities = true;
328
- this.$updateDataProtection();
329
- }
322
+ // Defaults.
323
+ this.$data.enabled = true;
324
+ this.$data.abilities = [];
325
+ this.$data.groups = [];
326
+ this.$data.inheritAbilities = true;
327
+ this.$updateDataProtection();
330
328
  }
331
329
 
332
330
  public static current(returnObjectIfNotExist: true): User & UserData;
@@ -544,7 +542,7 @@ export default class User extends AbleObject<UserData> {
544
542
  if (!this.$data.enabled) {
545
543
  return { result: false, message: 'This user is disabled.' };
546
544
  }
547
- if (this.$gatekeeper()) {
545
+ if (await this.$gatekeeper()) {
548
546
  return { result: true, message: 'You are already logged in.' };
549
547
  }
550
548
  if (!this.$checkPassword(data.password)) {
@@ -692,7 +690,7 @@ export default class User extends AbleObject<UserData> {
692
690
  };
693
691
  }
694
692
 
695
- tilmeld.logout();
693
+ await tilmeld.logout();
696
694
 
697
695
  for (let callback of (this.constructor as typeof User)
698
696
  .afterLogoutCallbacks) {
@@ -718,36 +716,36 @@ export default class User extends AbleObject<UserData> {
718
716
  );
719
717
  }
720
718
 
721
- /**
722
- * Get the user's group descendants.
723
- */
724
- public async $getDescendantGroups(): Promise<(Group & GroupData)[]> {
725
- if (this.$descendantGroups == null) {
726
- this.$descendantGroups = [];
727
- if (this.$data.group != null) {
728
- this.$descendantGroups = await this.$data.group.$getDescendants();
729
- }
730
- for (let curGroup of this.$data.groups ?? []) {
731
- this.$descendantGroups = this.$descendantGroups?.concat(
732
- await curGroup.$getDescendants()
733
- );
734
- }
719
+ public $getGid() {
720
+ if (this.$data.group) {
721
+ return this.$data.group.guid;
735
722
  }
736
- return this.$descendantGroups;
723
+ return null;
724
+ }
725
+
726
+ public $getGids() {
727
+ if (this.$data.groups) {
728
+ return this.$data.groups
729
+ .map((group) => group.guid)
730
+ .filter((guid) => guid != null) as string[];
731
+ }
732
+ return null;
737
733
  }
738
734
 
739
735
  /**
740
736
  * Get the user's group descendants.
741
737
  */
742
- public $getDescendantGroupsSync(): (Group & GroupData)[] {
738
+ public async $getDescendantGroups(): Promise<(Group & GroupData)[]> {
743
739
  if (this.$descendantGroups == null) {
744
740
  this.$descendantGroups = [];
745
741
  if (this.$data.group != null) {
746
- this.$descendantGroups = this.$data.group.$getDescendantsSync();
742
+ await this.$data.group.$wake();
743
+ this.$descendantGroups = await this.$data.group.$getDescendants();
747
744
  }
745
+ await Promise.all(this.$data.groups?.map((e) => e.$wake()) || []);
748
746
  for (let curGroup of this.$data.groups ?? []) {
749
747
  this.$descendantGroups = this.$descendantGroups?.concat(
750
- curGroup.$getDescendantsSync()
748
+ await curGroup.$getDescendants()
751
749
  );
752
750
  }
753
751
  }
@@ -756,13 +754,14 @@ export default class User extends AbleObject<UserData> {
756
754
 
757
755
  public $jsonAcceptData(input: EntityJson, allowConflict = false) {
758
756
  const tilmeld = enforceTilmeld(this);
759
- this.$referenceWake();
757
+ this.$check();
760
758
 
761
759
  if (
762
760
  'abilities' in input.data &&
763
761
  input.data.abilities?.includes('system/admin') !==
764
762
  this.$data.abilities?.includes('system/admin') &&
765
- !tilmeld.gatekeeper('system/admin')
763
+ (!tilmeld.currentUser ||
764
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
766
765
  ) {
767
766
  throw new BadDataError(
768
767
  "You don't have the authority to grant or revoke system/admin."
@@ -773,7 +772,8 @@ export default class User extends AbleObject<UserData> {
773
772
  'abilities' in input.data &&
774
773
  input.data.abilities?.includes('tilmeld/admin') !==
775
774
  this.$data.abilities?.includes('tilmeld/admin') &&
776
- !tilmeld.gatekeeper('system/admin')
775
+ (!tilmeld.currentUser ||
776
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
777
777
  ) {
778
778
  throw new BadDataError(
779
779
  "You don't have the authority to grant or revoke tilmeld/admin."
@@ -784,7 +784,8 @@ export default class User extends AbleObject<UserData> {
784
784
  'abilities' in input.data &&
785
785
  input.data.abilities?.includes('tilmeld/switch') !==
786
786
  this.$data.abilities?.includes('tilmeld/switch') &&
787
- !tilmeld.gatekeeper('system/admin')
787
+ (!tilmeld.currentUser ||
788
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
788
789
  ) {
789
790
  throw new BadDataError(
790
791
  "You don't have the authority to grant or revoke tilmeld/switch."
@@ -797,13 +798,14 @@ export default class User extends AbleObject<UserData> {
797
798
 
798
799
  public $jsonAcceptPatch(patch: EntityPatch, allowConflict = false) {
799
800
  const tilmeld = enforceTilmeld(this);
800
- this.$referenceWake();
801
+ this.$check();
801
802
 
802
803
  if (
803
804
  'abilities' in patch.set &&
804
805
  patch.set.abilities?.includes('system/admin') !==
805
806
  this.$data.abilities?.includes('system/admin') &&
806
- !tilmeld.gatekeeper('system/admin')
807
+ (!tilmeld.currentUser ||
808
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
807
809
  ) {
808
810
  throw new BadDataError(
809
811
  "You don't have the authority to grant or revoke system/admin."
@@ -814,7 +816,8 @@ export default class User extends AbleObject<UserData> {
814
816
  'abilities' in patch.set &&
815
817
  patch.set.abilities?.includes('tilmeld/admin') !==
816
818
  this.$data.abilities?.includes('tilmeld/admin') &&
817
- !tilmeld.gatekeeper('system/admin')
819
+ (!tilmeld.currentUser ||
820
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
818
821
  ) {
819
822
  throw new BadDataError(
820
823
  "You don't have the authority to grant or revoke tilmeld/admin."
@@ -825,7 +828,8 @@ export default class User extends AbleObject<UserData> {
825
828
  'abilities' in patch.set &&
826
829
  patch.set.abilities?.includes('tilmeld/switch') !==
827
830
  this.$data.abilities?.includes('tilmeld/switch') &&
828
- !tilmeld.gatekeeper('system/admin')
831
+ (!tilmeld.currentUser ||
832
+ !tilmeld.currentUser.abilities?.includes('system/admin'))
829
833
  ) {
830
834
  throw new BadDataError(
831
835
  "You don't have the authority to grant or revoke tilmeld/switch."
@@ -948,6 +952,17 @@ export default class User extends AbleObject<UserData> {
948
952
  }
949
953
  }
950
954
 
955
+ public $updateGroupDataProtection() {
956
+ if (this.$data.group != null && !this.$data.group.$asleep()) {
957
+ this.$data.group.$updateDataProtection();
958
+ }
959
+ for (let group of this.$data.groups ?? []) {
960
+ if (!group.$asleep()) {
961
+ group.$updateDataProtection();
962
+ }
963
+ }
964
+ }
965
+
951
966
  /**
952
967
  * Check to see if a user has an ability.
953
968
  *
@@ -962,7 +977,7 @@ export default class User extends AbleObject<UserData> {
962
977
  * @param ability The ability.
963
978
  * @returns True or false.
964
979
  */
965
- public $gatekeeper(ability?: string): boolean {
980
+ public async $gatekeeper(ability?: string): Promise<boolean> {
966
981
  const tilmeld = enforceTilmeld(this);
967
982
  if (ability == null) {
968
983
  return tilmeld.User.current()?.$is(this) ?? false;
@@ -971,17 +986,16 @@ export default class User extends AbleObject<UserData> {
971
986
  if (this.$gatekeeperCache == null) {
972
987
  let abilities = this.$data.abilities ?? [];
973
988
  if (this.$data.inheritAbilities) {
989
+ await Promise.all(this.$data.groups?.map((e) => e.$wake()) || []);
974
990
  for (let curGroup of this.$data.groups ?? []) {
975
991
  if (curGroup.enabled) {
976
992
  abilities = abilities.concat(curGroup.abilities ?? []);
977
993
  }
978
994
  }
979
- if (
980
- this.$data.group != null &&
981
- this.$data.group.cdate != null &&
982
- this.$data.group.enabled
983
- ) {
984
- abilities = abilities.concat(this.$data.group.abilities ?? []);
995
+ await this.$data.group?.$wake();
996
+ const group = this.$data.group;
997
+ if (group != null && group.cdate != null && group.enabled) {
998
+ abilities = abilities.concat(group.abilities ?? []);
985
999
  }
986
1000
  }
987
1001
  this.$gatekeeperCache = Object.fromEntries(
@@ -1102,13 +1116,16 @@ export default class User extends AbleObject<UserData> {
1102
1116
  * @param group The group.
1103
1117
  * @returns True if the user is already in the group. The resulting array of groups if the user was not.
1104
1118
  */
1105
- public $addGroup(group: Group & GroupData) {
1106
- if (this.$data.groups == null) {
1107
- this.$data.groups = [];
1119
+ public async $addGroup(group: Group & GroupData) {
1120
+ await Promise.all(this.$data.groups?.map((e) => e.$wake()) || []);
1121
+ let groups = this.$data.groups;
1122
+ if (groups == null) {
1123
+ groups = [];
1108
1124
  }
1109
- if (!group.$inArray(this.$data.groups)) {
1110
- this.$data.groups.push(group);
1111
- return this.$data.groups;
1125
+ if (!group.$inArray(groups)) {
1126
+ groups.push(group);
1127
+ this.$data.groups = groups;
1128
+ return groups;
1112
1129
  }
1113
1130
  return true;
1114
1131
  }
@@ -1141,10 +1158,12 @@ export default class User extends AbleObject<UserData> {
1141
1158
  * @param group The group.
1142
1159
  * @returns True if the user wasn't in the group. The resulting array of groups if the user was.
1143
1160
  */
1144
- public $delGroup(group: Group & GroupData) {
1145
- if (this.$data.groups != null && group.$inArray(this.$data.groups)) {
1161
+ public async $delGroup(group: Group & GroupData) {
1162
+ await Promise.all(this.$data.groups?.map((e) => e.$wake()) || []);
1163
+ let groups = this.$data.groups || [];
1164
+ if (this.$data.groups != null && group.$inArray(groups)) {
1146
1165
  const newGroups: (Group & GroupData)[] = [];
1147
- for (let curGroup of this.$data.groups) {
1166
+ for (let curGroup of groups) {
1148
1167
  if (!group.$is(curGroup)) {
1149
1168
  newGroups.push(curGroup);
1150
1169
  }
@@ -1161,7 +1180,7 @@ export default class User extends AbleObject<UserData> {
1161
1180
  * @param group The group, or the group's GUID.
1162
1181
  * @returns True or false.
1163
1182
  */
1164
- public $inGroup(group: (Group & GroupData) | string) {
1183
+ public async $inGroup(group: (Group & GroupData) | string) {
1165
1184
  if (typeof group === 'string') {
1166
1185
  if (this.$data.group?.guid === group) {
1167
1186
  return true;
@@ -1184,16 +1203,19 @@ export default class User extends AbleObject<UserData> {
1184
1203
  * @param group The group, or the group's GUID.
1185
1204
  * @returns True or false.
1186
1205
  */
1187
- public $isDescendant(group: (Group & GroupData) | string) {
1206
+ public async $isDescendant(group: (Group & GroupData) | string) {
1188
1207
  // Check to see if the user is in a descendant group of the given group.
1208
+ await this.$data.group?.$wake();
1189
1209
  if (
1210
+ this.$data.group != null &&
1190
1211
  this.$data.group?.cdate != null &&
1191
- this.$data.group.$isDescendant(group)
1212
+ (await this.$data.group?.$isDescendant(group))
1192
1213
  ) {
1193
1214
  return true;
1194
1215
  }
1216
+ await Promise.all(this.$data.groups?.map((e) => e.$wake()) || []);
1195
1217
  for (let curGroup of this.$data.groups ?? []) {
1196
- if (curGroup.$isDescendant(group)) {
1218
+ if (await curGroup.$isDescendant(group)) {
1197
1219
  return true;
1198
1220
  }
1199
1221
  }
@@ -1925,8 +1947,8 @@ export default class User extends AbleObject<UserData> {
1925
1947
  }
1926
1948
 
1927
1949
  if (
1928
- tilmeld.gatekeeper('tilmeld/admin') &&
1929
- !tilmeld.gatekeeper('system/admin') &&
1950
+ (await tilmeld.gatekeeper('tilmeld/admin')) &&
1951
+ !(await tilmeld.gatekeeper('system/admin')) &&
1930
1952
  this.$data.abilities?.includes('system/admin')
1931
1953
  ) {
1932
1954
  throw new BadDataError(
@@ -2020,7 +2042,7 @@ export default class User extends AbleObject<UserData> {
2020
2042
  }
2021
2043
 
2022
2044
  // Email changes.
2023
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
2045
+ if (!(await tilmeld.gatekeeper('tilmeld/admin'))) {
2024
2046
  // The user isn't an admin, so email address changes should contain some
2025
2047
  // security measures.
2026
2048
  if (tilmeld.config.verifyEmail) {
@@ -2119,19 +2141,18 @@ export default class User extends AbleObject<UserData> {
2119
2141
  this.$nymph = tnymph;
2120
2142
  tilmeld = enforceTilmeld(this);
2121
2143
 
2122
- if (
2123
- this.$data.group != null &&
2124
- this.$data.group.user != null &&
2125
- this.$is(this.$data.group.user)
2126
- ) {
2144
+ const group = this.$data.group;
2145
+ await group?.$wake();
2146
+ if (group != null && group.user != null && this.$is(group.user)) {
2127
2147
  try {
2128
2148
  // Update the user's generated primary group.
2129
- this.$data.group.groupname = this.$data.username;
2130
- this.$data.group.avatar = this.$data.avatar;
2131
- this.$data.group.email = this.$data.email;
2132
- this.$data.group.name = this.$data.name;
2133
- this.$data.group.phone = this.$data.phone;
2134
- await this.$data.group.$saveSkipAC();
2149
+ group.groupname = this.$data.username;
2150
+ group.avatar = this.$data.avatar;
2151
+ group.email = this.$data.email;
2152
+ group.name = this.$data.name;
2153
+ group.phone = this.$data.phone;
2154
+ await group.$saveSkipAC();
2155
+ this.$data.group = group;
2135
2156
  } catch (e: any) {
2136
2157
  await tnymph.rollback(transaction);
2137
2158
  this.$nymph = nymph;
@@ -2197,11 +2218,11 @@ export default class User extends AbleObject<UserData> {
2197
2218
 
2198
2219
  public async $delete() {
2199
2220
  const tilmeld = enforceTilmeld(this);
2200
- if (!tilmeld.gatekeeper('tilmeld/admin')) {
2221
+ if (!(await tilmeld.gatekeeper('tilmeld/admin'))) {
2201
2222
  throw new BadDataError("You don't have the authority to delete users.");
2202
2223
  }
2203
2224
  if (
2204
- !tilmeld.gatekeeper('system/admin') &&
2225
+ !(await tilmeld.gatekeeper('system/admin')) &&
2205
2226
  this.$data.abilities?.includes('system/admin')
2206
2227
  ) {
2207
2228
  throw new BadDataError(