@stevenkellner/team-conduct-api 1.0.27 → 1.0.28

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.
@@ -1,2 +1,6 @@
1
1
  import { Team, User, UserRole } from './types';
2
- export declare function checkAuthentication(rawUserId: string | null, teamId: Team.Id, ...roles: UserRole[]): Promise<User.Id>;
2
+ type ExpectedUserRoles = UserRole | ExpectedUserRoles[] | {
3
+ anyOf: ExpectedUserRoles[];
4
+ };
5
+ export declare function checkAuthentication(rawUserId: string | null, teamId: Team.Id, roles: ExpectedUserRoles): Promise<User.Id>;
6
+ export {};
@@ -4,10 +4,15 @@ exports.checkAuthentication = checkAuthentication;
4
4
  const firebase_function_1 = require("@stevenkellner/firebase-function");
5
5
  const types_1 = require("./types");
6
6
  const Firestore_1 = require("./Firestore");
7
- function includesAll(array, ...values) {
8
- return values.every(value => array.includes(value));
7
+ function hasUserRoles(userRoles, expectedRoles) {
8
+ if (Array.isArray(expectedRoles))
9
+ return expectedRoles.every(expectedRole => hasUserRoles(userRoles, expectedRole));
10
+ else if (typeof expectedRoles === 'object' && 'anyOf' in expectedRoles)
11
+ return expectedRoles.anyOf.some(role => hasUserRoles(userRoles, role));
12
+ else
13
+ return userRoles.includes(expectedRoles);
9
14
  }
10
- async function checkAuthentication(rawUserId, teamId, ...roles) {
15
+ async function checkAuthentication(rawUserId, teamId, roles) {
11
16
  if (rawUserId === null)
12
17
  throw new firebase_function_1.FunctionsError('unauthenticated', 'User is not authenticated');
13
18
  const userId = types_1.User.Id.builder.build(rawUserId);
@@ -24,7 +29,7 @@ async function checkAuthentication(rawUserId, teamId, ...roles) {
24
29
  const person = types_1.Person.builder.build(personSnapshot.data);
25
30
  if (person.signInProperties === null)
26
31
  throw new firebase_function_1.FunctionsError('permission-denied', 'Person is not signed in');
27
- const userHasRoles = includesAll(person.signInProperties.roles, ...roles);
32
+ const userHasRoles = hasUserRoles(person.signInProperties.roles, roles);
28
33
  if (!userHasRoles)
29
34
  throw new firebase_function_1.FunctionsError('permission-denied', 'User does not have the required roles');
30
35
  return userId;
@@ -16,7 +16,9 @@ class FineAddFunction extends firebase_function_1.FirebaseFunction {
16
16
  });
17
17
  returnTypeBuilder = new typescript_common_functionality_1.ValueTypeBuilder();
18
18
  async execute(parameters) {
19
- await (0, checkAuthentication_1.checkAuthentication)(this.userId, parameters.teamId, 'fine-manager');
19
+ await (0, checkAuthentication_1.checkAuthentication)(this.userId, parameters.teamId, {
20
+ anyOf: ['fine-manager', 'fine-can-add']
21
+ });
20
22
  const fineSnapshot = await Firestore_1.Firestore.shared.fine(parameters.teamId, parameters.fine.id).snapshot();
21
23
  if (fineSnapshot.exists)
22
24
  throw new firebase_function_1.FunctionsError('already-exists', 'Fine already exists');
@@ -61,6 +61,7 @@ export declare const localizationDE: {
61
61
  personManager: string;
62
62
  fineTemplateManager: string;
63
63
  fineManager: string;
64
+ fineCanAdd: string;
64
65
  teamManager: string;
65
66
  };
66
67
  };
@@ -94,6 +94,7 @@ exports.localizationDE = {
94
94
  personManager: 'Personenmanager',
95
95
  fineTemplateManager: 'Strafvorlagenmanager',
96
96
  fineManager: 'Strafenmanager',
97
+ fineCanAdd: 'Kann Strafen hinzufügen',
97
98
  teamManager: 'Teammanager'
98
99
  }
99
100
  };
@@ -61,6 +61,7 @@ export declare const localizationEN: {
61
61
  personManager: string;
62
62
  fineTemplateManager: string;
63
63
  fineManager: string;
64
+ fineCanAdd: string;
64
65
  teamManager: string;
65
66
  };
66
67
  };
@@ -94,6 +94,7 @@ exports.localizationEN = {
94
94
  personManager: 'Person Manager',
95
95
  fineTemplateManager: 'Fine Template Manager',
96
96
  fineManager: 'Fine Manager',
97
+ fineCanAdd: 'Can add fines',
97
98
  teamManager: 'Team Manager'
98
99
  }
99
100
  };
@@ -62,6 +62,7 @@ export declare const localizations: {
62
62
  personManager: string;
63
63
  fineTemplateManager: string;
64
64
  fineManager: string;
65
+ fineCanAdd: string;
65
66
  teamManager: string;
66
67
  };
67
68
  };
@@ -127,6 +128,7 @@ export declare const localizations: {
127
128
  personManager: string;
128
129
  fineTemplateManager: string;
129
130
  fineManager: string;
131
+ fineCanAdd: string;
130
132
  teamManager: string;
131
133
  };
132
134
  };
@@ -213,6 +215,7 @@ export declare class Localization {
213
215
  personManager: ValueLocalization;
214
216
  fineTemplateManager: ValueLocalization;
215
217
  fineManager: ValueLocalization;
218
+ fineCanAdd: ValueLocalization;
216
219
  teamManager: ValueLocalization;
217
220
  };
218
221
  };
@@ -1,5 +1,5 @@
1
1
  import { ValueTypeBuilder } from '@stevenkellner/typescript-common-functionality';
2
- export type UserRole = 'person-manager' | 'fineTemplate-manager' | 'fine-manager' | 'team-manager';
2
+ export type UserRole = 'person-manager' | 'fineTemplate-manager' | 'fine-manager' | 'fine-can-add' | 'team-manager';
3
3
  export declare namespace UserRole {
4
4
  const all: UserRole[];
5
5
  function formatted(role: UserRole): string;
@@ -9,6 +9,7 @@ var UserRole;
9
9
  'person-manager',
10
10
  'fineTemplate-manager',
11
11
  'fine-manager',
12
+ 'fine-can-add',
12
13
  'team-manager'
13
14
  ];
14
15
  function formatted(role) {
@@ -16,6 +17,7 @@ var UserRole;
16
17
  'person-manager': 'personManager',
17
18
  'fineTemplate-manager': 'fineTemplateManager',
18
19
  'fine-manager': 'fineManager',
20
+ 'fine-can-add': 'fineCanAdd',
19
21
  'team-manager': 'teamManager'
20
22
  };
21
23
  return Localization_1.Localization.shared.userRole[localizationKeyMap[role]].value();
@@ -135,6 +135,12 @@ describe('Localization for de', () => {
135
135
  it('userRole.fineManager should be formatted correctly', () => {
136
136
  (0, core_1.expect)(types_1.UserRole.formatted('fine-manager')).toBeEqual('Strafenmanager');
137
137
  });
138
+ it('userRole.fineCanAdd should be tested', () => {
139
+ (0, core_1.expect)(types_1.Localization.shared.userRole.fineCanAdd.value()).toBeEqual('Kann Strafen hinzufügen');
140
+ });
141
+ it('userRole.fineCanAdd should be formatted correctly', () => {
142
+ (0, core_1.expect)(types_1.UserRole.formatted('fine-can-add')).toBeEqual('Kann Strafen hinzufügen');
143
+ });
138
144
  it('userRole.teamManager should be tested', () => {
139
145
  (0, core_1.expect)(types_1.Localization.shared.userRole.teamManager.value()).toBeEqual('Teammanager');
140
146
  });
@@ -129,6 +129,12 @@ describe('Localization for en', () => {
129
129
  it('userRole.fineManager should be formatted correctly', () => {
130
130
  (0, core_1.expect)(types_1.UserRole.formatted('fine-manager')).toBeEqual('Fine Manager');
131
131
  });
132
+ it('userRole.fineCanAdd should be tested', () => {
133
+ (0, core_1.expect)(types_1.Localization.shared.userRole.fineCanAdd.value()).toBeEqual('Can add fines');
134
+ });
135
+ it('userRole.fineCanAdd should be formatted correctly', () => {
136
+ (0, core_1.expect)(types_1.UserRole.formatted('fine-can-add')).toBeEqual('Can add fines');
137
+ });
132
138
  it('userRole.teamManager should be tested', () => {
133
139
  (0, core_1.expect)(types_1.Localization.shared.userRole.teamManager.value()).toBeEqual('Team Manager');
134
140
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stevenkellner/team-conduct-api",
3
- "version": "1.0.27",
3
+ "version": "1.0.28",
4
4
  "description": "Firebase API for Team Conduct",
5
5
  "license": "MIT",
6
6
  "author": "Steven Kellner",
@@ -2,11 +2,24 @@ import { FunctionsError } from '@stevenkellner/firebase-function';
2
2
  import { Person, Team, User, UserRole } from './types';
3
3
  import { Firestore } from './Firestore';
4
4
 
5
- function includesAll<T>(array: T[], ...values: T[]): boolean {
6
- return values.every(value => array.includes(value));
5
+ type ExpectedUserRoles =
6
+ | UserRole
7
+ | ExpectedUserRoles[]
8
+ | {
9
+ anyOf: ExpectedUserRoles[];
10
+ }
11
+
12
+ function hasUserRoles(userRoles: UserRole[], expectedRoles: ExpectedUserRoles): boolean {
13
+ if (Array.isArray(expectedRoles))
14
+ return expectedRoles.every(expectedRole => hasUserRoles(userRoles, expectedRole));
15
+ else if (typeof expectedRoles === 'object' && 'anyOf' in expectedRoles)
16
+ return expectedRoles.anyOf.some(role => hasUserRoles(userRoles, role));
17
+ else
18
+ return userRoles.includes(expectedRoles);
19
+
7
20
  }
8
21
 
9
- export async function checkAuthentication(rawUserId: string | null, teamId: Team.Id, ...roles: UserRole[]): Promise<User.Id> {
22
+ export async function checkAuthentication(rawUserId: string | null, teamId: Team.Id, roles: ExpectedUserRoles): Promise<User.Id> {
10
23
  if (rawUserId === null)
11
24
  throw new FunctionsError('unauthenticated', 'User is not authenticated');
12
25
  const userId = User.Id.builder.build(rawUserId);
@@ -28,7 +41,7 @@ export async function checkAuthentication(rawUserId: string | null, teamId: Team
28
41
  if (person.signInProperties === null)
29
42
  throw new FunctionsError('permission-denied', 'Person is not signed in');
30
43
 
31
- const userHasRoles = includesAll(person.signInProperties.roles, ...roles);
44
+ const userHasRoles = hasUserRoles(person.signInProperties.roles, roles);
32
45
  if (!userHasRoles)
33
46
  throw new FunctionsError('permission-denied', 'User does not have the required roles');
34
47
 
@@ -29,7 +29,9 @@ export class FineAddFunction extends FirebaseFunction<FineAddFunction.Parameters
29
29
 
30
30
  public async execute(parameters: FineAddFunction.Parameters): Promise<void> {
31
31
 
32
- await checkAuthentication(this.userId, parameters.teamId, 'fine-manager');
32
+ await checkAuthentication(this.userId, parameters.teamId, {
33
+ anyOf: ['fine-manager', 'fine-can-add']
34
+ });
33
35
 
34
36
  const fineSnapshot = await Firestore.shared.fine(parameters.teamId, parameters.fine.id).snapshot();
35
37
  if (fineSnapshot.exists)
package/src/locales/de.ts CHANGED
@@ -92,6 +92,7 @@ export const localizationDE = {
92
92
  personManager: 'Personenmanager',
93
93
  fineTemplateManager: 'Strafvorlagenmanager',
94
94
  fineManager: 'Strafenmanager',
95
+ fineCanAdd: 'Kann Strafen hinzufügen',
95
96
  teamManager: 'Teammanager'
96
97
  }
97
98
  }
package/src/locales/en.ts CHANGED
@@ -92,6 +92,7 @@ export const localizationEN = {
92
92
  personManager: 'Person Manager',
93
93
  fineTemplateManager: 'Fine Template Manager',
94
94
  fineManager: 'Fine Manager',
95
+ fineCanAdd: 'Can add fines',
95
96
  teamManager: 'Team Manager'
96
97
  }
97
98
  }
@@ -5,6 +5,7 @@ export type UserRole =
5
5
  | 'person-manager'
6
6
  | 'fineTemplate-manager'
7
7
  | 'fine-manager'
8
+ | 'fine-can-add'
8
9
  | 'team-manager';
9
10
 
10
11
  export namespace UserRole {
@@ -13,6 +14,7 @@ export namespace UserRole {
13
14
  'person-manager',
14
15
  'fineTemplate-manager',
15
16
  'fine-manager',
17
+ 'fine-can-add',
16
18
  'team-manager'
17
19
  ];
18
20
 
@@ -21,6 +23,7 @@ export namespace UserRole {
21
23
  'person-manager': 'personManager',
22
24
  'fineTemplate-manager': 'fineTemplateManager',
23
25
  'fine-manager': 'fineManager',
26
+ 'fine-can-add': 'fineCanAdd',
24
27
  'team-manager': 'teamManager'
25
28
  }
26
29
  return Localization.shared.userRole[localizationKeyMap[role]].value();