@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.
- package/lib/src/checkAuthentication.d.ts +5 -1
- package/lib/src/checkAuthentication.js +9 -4
- package/lib/src/functions/fine/add.js +3 -1
- package/lib/src/locales/de.d.ts +1 -0
- package/lib/src/locales/de.js +1 -0
- package/lib/src/locales/en.d.ts +1 -0
- package/lib/src/locales/en.js +1 -0
- package/lib/src/types/Localization.d.ts +3 -0
- package/lib/src/types/UserRole.d.ts +1 -1
- package/lib/src/types/UserRole.js +2 -0
- package/lib/test/localization.de.test.js +6 -0
- package/lib/test/localization.en.test.js +6 -0
- package/package.json +1 -1
- package/src/checkAuthentication.ts +17 -4
- package/src/functions/fine/add.ts +3 -1
- package/src/locales/de.ts +1 -0
- package/src/locales/en.ts +1 -0
- package/src/types/UserRole.ts +3 -0
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
import { Team, User, UserRole } from './types';
|
|
2
|
-
|
|
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
|
|
8
|
-
|
|
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,
|
|
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 =
|
|
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,
|
|
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');
|
package/lib/src/locales/de.d.ts
CHANGED
package/lib/src/locales/de.js
CHANGED
package/lib/src/locales/en.d.ts
CHANGED
package/lib/src/locales/en.js
CHANGED
|
@@ -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
|
@@ -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
|
-
|
|
6
|
-
|
|
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,
|
|
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 =
|
|
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,
|
|
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
package/src/locales/en.ts
CHANGED
package/src/types/UserRole.ts
CHANGED
|
@@ -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();
|