@tech4good/angular-schematics 21.4.0 → 21.6.0
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/CHANGELOG.md +33 -0
- package/package.json +1 -1
- package/src/entity/files/__name@dasherize__/__name@dasherize__.mock.ts.template +2 -1
- package/src/entity/files/__name@dasherize__/__name@dasherize__.model.ts.template +2 -1
- package/src/entity/index.js +57 -0
- package/src/entity/index.js.map +1 -1
- package/src/entity/schema.json +4 -0
- package/src/ng-add/files/.firebaserc.template +2 -1
- package/src/ng-add/files/.gitignore.template +2 -0
- package/src/ng-add/files/bigquery/queries/active_time_by_route.sql.template +34 -0
- package/src/ng-add/files/bigquery/scripts/export_entities.py.template +143 -0
- package/src/ng-add/files/bigquery/scripts/run_query.py.template +167 -0
- package/src/ng-add/files/bigquery/views/events_flat.sql.template +60 -0
- package/src/ng-add/files/bigquery/views/logs_latest.sql.template +34 -0
- package/src/ng-add/files/docs-infra/analytics-guide.md.template +170 -0
- package/src/ng-add/files/docs-infra/authentication-architecture.md.template +270 -0
- package/src/ng-add/files/docs-infra/infrastructure-guide.md.template +16 -10
- package/src/ng-add/files/firebase.json.template +6 -0
- package/src/ng-add/files/firebase.logs.json.template +5 -0
- package/src/ng-add/files/firestore.indexes.json.template +112 -0
- package/src/ng-add/files/firestore.logs.rules.spec.ts.template +100 -0
- package/src/ng-add/files/firestore.logs.rules.template +8 -0
- package/src/ng-add/files/firestore.rules.spec.ts.template +84 -15
- package/src/ng-add/files/firestore.rules.template +29 -17
- package/src/ng-add/files/functions/.gitignore.template +3 -0
- package/src/ng-add/files/functions/package.json.template +12 -0
- package/src/ng-add/files/functions/src/index.ts.template +44 -0
- package/src/ng-add/files/functions/tsconfig.json.template +13 -0
- package/src/ng-add/files/jest.config.ts.template +1 -1
- package/src/ng-add/files/package.json.template +7 -4
- package/src/ng-add/files/src/app/admin/admin-panel/admin-panel.component.html.template +3 -1
- package/src/ng-add/files/src/app/admin/admin-panel/admin-panel.component.spec.ts.template +6 -8
- package/src/ng-add/files/src/app/admin/admin-panel/admin-panel.component.ts.template +2 -0
- package/src/ng-add/files/src/app/app.component.html.template +2 -2
- package/src/ng-add/files/src/app/app.component.ts.template +2 -0
- package/src/ng-add/files/src/app/app.routes.ts.template +6 -0
- package/src/ng-add/files/src/app/auth-routing.spec.ts.template +9 -4
- package/src/ng-add/files/src/app/core/analytics/log.service.ts.template +26 -4
- package/src/ng-add/files/src/app/core/analytics/time-analytics.service.spec.ts.template +145 -2
- package/src/ng-add/files/src/app/core/analytics/time-analytics.service.ts.template +355 -149
- package/src/ng-add/files/src/app/core/auth/auth.service.ts.template +6 -1
- package/src/ng-add/files/src/app/core/auth/guards/require-admin.guard.spec.ts.template +38 -0
- package/src/ng-add/files/src/app/core/auth/guards/require-admin.guard.ts.template +3 -2
- package/src/ng-add/files/src/app/core/auth/guards/require-auth-and-consent.guard.ts.template +10 -3
- package/src/ng-add/files/src/app/core/firebase/cached-listeners.service.ts.template +11 -5
- package/src/ng-add/files/src/app/core/firebase/database.service.ts.template +34 -17
- package/src/ng-add/files/src/app/core/firebase/email-link-context.service.spec.ts.template +59 -0
- package/src/ng-add/files/src/app/core/firebase/email-link-context.service.ts.template +147 -0
- package/src/ng-add/files/src/app/core/firebase/firebase.mock.service.ts.template +104 -17
- package/src/ng-add/files/src/app/core/firebase/firebase.service.spec.ts.template +116 -0
- package/src/ng-add/files/src/app/core/firebase/firebase.service.ts.template +367 -70
- package/src/ng-add/files/src/app/core/store/app.store.ts.template +4 -1
- package/src/ng-add/files/src/app/core/store/auth/auth.mock.ts.template +52 -66
- package/src/ng-add/files/src/app/core/store/auth/auth.store.spec.ts.template +127 -0
- package/src/ng-add/files/src/app/core/store/auth/auth.store.ts.template +216 -147
- package/src/ng-add/files/src/app/core/store/user/user.mock.ts.template +2 -4
- package/src/ng-add/files/src/app/core/store/user/user.model.ts.template +1 -1
- package/src/ng-add/files/src/app/core/store/user-private/user-private.model.ts.template +1 -0
- package/src/ng-add/files/src/app/first-time/auth-action/auth-action.component.html.template +4 -0
- package/src/ng-add/files/src/app/first-time/auth-action/auth-action.component.scss.template +1 -0
- package/src/ng-add/files/src/app/first-time/auth-action/auth-action.component.ts.template +38 -0
- package/src/ng-add/files/src/app/first-time/consent/consent.component.html.template +172 -48
- package/src/ng-add/files/src/app/first-time/consent/consent.component.scss.template +31 -0
- package/src/ng-add/files/src/app/first-time/consent/consent.component.spec.ts.template +31 -3
- package/src/ng-add/files/src/app/first-time/consent/consent.component.ts.template +12 -11
- package/src/ng-add/files/src/app/first-time/consent/consent.guard.ts.template +5 -7
- package/src/ng-add/files/src/app/first-time/early-access/early-access.component.spec.ts.template +12 -1
- package/src/ng-add/files/src/app/first-time/early-access/early-access.component.ts.template +93 -48
- package/src/ng-add/files/src/app/first-time/early-access/early-access.guard.ts.template +12 -2
- package/src/ng-add/files/src/app/first-time/email-link/email-link.component.html.template +18 -0
- package/src/ng-add/files/src/app/first-time/email-link/email-link.component.scss.template +2 -0
- package/src/ng-add/files/src/app/first-time/email-link/email-link.component.spec.ts.template +40 -0
- package/src/ng-add/files/src/app/first-time/email-link/email-link.component.ts.template +76 -0
- package/src/ng-add/files/src/app/first-time/first-time-routing.spec.ts.template +14 -8
- package/src/ng-add/files/src/app/first-time/landing/hero/hero.component.html.template +19 -7
- package/src/ng-add/files/src/app/first-time/landing/hero/hero.component.scss.template +51 -0
- package/src/ng-add/files/src/app/first-time/landing/hero/hero.component.ts.template +29 -5
- package/src/ng-add/files/src/app/first-time/landing/landing.component.html.template +2 -1
- package/src/ng-add/files/src/app/first-time/landing/landing.component.scss.template +12 -8
- package/src/ng-add/files/src/app/first-time/landing/landing.component.spec.ts.template +31 -10
- package/src/ng-add/files/src/app/first-time/landing/landing.component.ts.template +6 -19
- package/src/ng-add/files/src/app/first-time/login/login.component.html.template +58 -6
- package/src/ng-add/files/src/app/first-time/login/login.component.scss.template +151 -22
- package/src/ng-add/files/src/app/first-time/login/login.component.spec.ts.template +18 -38
- package/src/ng-add/files/src/app/first-time/login/login.component.ts.template +59 -31
- package/src/ng-add/files/src/app/main/account-profile/account-profile.component.html.template +12 -0
- package/src/ng-add/files/src/app/main/account-profile/account-profile.component.scss.template +39 -0
- package/src/ng-add/files/src/app/main/account-profile/account-profile.component.spec.ts.template +68 -0
- package/src/ng-add/files/src/app/main/account-profile/account-profile.component.ts.template +20 -0
- package/src/ng-add/files/src/app/main/account-profile/delete-guest-card/delete-guest-card.component.html.template +24 -0
- package/src/ng-add/files/src/app/main/account-profile/delete-guest-card/delete-guest-card.component.scss.template +50 -0
- package/src/ng-add/files/src/app/main/account-profile/delete-guest-card/delete-guest-card.component.spec.ts.template +65 -0
- package/src/ng-add/files/src/app/main/account-profile/delete-guest-card/delete-guest-card.component.ts.template +59 -0
- package/src/ng-add/files/src/app/main/account-profile/guest-deletion-dialog.component.ts.template +51 -0
- package/src/ng-add/files/src/app/main/account-profile/profile-details-card/profile-details-card.component.html.template +50 -0
- package/src/ng-add/files/src/app/main/account-profile/profile-details-card/profile-details-card.component.scss.template +137 -0
- package/src/ng-add/files/src/app/main/account-profile/profile-details-card/profile-details-card.component.spec.ts.template +57 -0
- package/src/ng-add/files/src/app/main/account-profile/profile-details-card/profile-details-card.component.ts.template +59 -0
- package/src/ng-add/files/src/app/main/account-profile/sign-in-methods-card/sign-in-methods-card.component.html.template +133 -0
- package/src/ng-add/files/src/app/main/account-profile/sign-in-methods-card/sign-in-methods-card.component.scss.template +207 -0
- package/src/ng-add/files/src/app/main/account-profile/sign-in-methods-card/sign-in-methods-card.component.spec.ts.template +74 -0
- package/src/ng-add/files/src/app/main/account-profile/sign-in-methods-card/sign-in-methods-card.component.ts.template +130 -0
- package/src/ng-add/files/src/app/main/home/home.component.ts.template +0 -5
- package/src/ng-add/files/src/app/shared/guest-warning-bar/guest-warning-bar.component.html.template +15 -0
- package/src/ng-add/files/src/app/shared/guest-warning-bar/guest-warning-bar.component.scss.template +79 -0
- package/src/ng-add/files/src/app/shared/guest-warning-bar/guest-warning-bar.component.spec.ts.template +59 -0
- package/src/ng-add/files/src/app/shared/guest-warning-bar/guest-warning-bar.component.ts.template +44 -0
- package/src/ng-add/files/src/app/shared/navbar/navbar.component.html.template +45 -15
- package/src/ng-add/files/src/app/shared/navbar/navbar.component.scss.template +103 -19
- package/src/ng-add/files/src/app/shared/navbar/navbar.component.spec.ts.template +73 -9
- package/src/ng-add/files/src/app/shared/navbar/navbar.component.ts.template +46 -18
- package/src/ng-add/files/src/environments/environment.prod.ts.template +1 -0
- package/src/ng-add/files/src/environments/environment.test.ts.template +1 -0
- package/src/ng-add/files/src/environments/environment.ts.template +1 -1
- package/src/ng-add/index_spec.d.ts +1 -0
- package/src/ng-add/index_spec.js +108 -0
- package/src/ng-add/index_spec.js.map +1 -0
- package/tsconfig.json +2 -2
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
assertFails,
|
|
6
|
+
assertSucceeds,
|
|
7
|
+
initializeTestEnvironment,
|
|
8
|
+
RulesTestEnvironment,
|
|
9
|
+
} from '@firebase/rules-unit-testing';
|
|
10
|
+
import { readFileSync } from 'fs';
|
|
11
|
+
import { doc, setDoc, updateDoc, getDoc, deleteDoc } from 'firebase/firestore';
|
|
12
|
+
|
|
13
|
+
let testEnv: RulesTestEnvironment;
|
|
14
|
+
|
|
15
|
+
beforeAll(async () => {
|
|
16
|
+
testEnv = await initializeTestEnvironment({
|
|
17
|
+
projectId: 'demo-logs-rules-test',
|
|
18
|
+
firestore: {
|
|
19
|
+
rules: readFileSync('firestore.logs.rules', 'utf8'),
|
|
20
|
+
host: '127.0.0.1',
|
|
21
|
+
port: 8080,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
await testEnv.clearFirestore();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
afterAll(async () => {
|
|
31
|
+
await testEnv.cleanup();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('Firestore Logs Security Rules', () => {
|
|
35
|
+
it('should allow anyone (unauthenticated) to create and update logs', async () => {
|
|
36
|
+
const unauthedDb = testEnv.unauthenticatedContext().firestore();
|
|
37
|
+
const logDoc = doc(unauthedDb, 'logs/log_123');
|
|
38
|
+
|
|
39
|
+
// Create log
|
|
40
|
+
await assertSucceeds(
|
|
41
|
+
setDoc(logDoc, {
|
|
42
|
+
__sessionId: 'session_abc',
|
|
43
|
+
route: '/s/session_1',
|
|
44
|
+
startTime: Date.now(),
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// Update log
|
|
49
|
+
await assertSucceeds(
|
|
50
|
+
updateDoc(logDoc, {
|
|
51
|
+
_updatedAt: Date.now(),
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should allow authenticated users to create and update logs', async () => {
|
|
57
|
+
const authDb = testEnv.authenticatedContext('student_1').firestore();
|
|
58
|
+
const logDoc = doc(authDb, 'logs/log_456');
|
|
59
|
+
|
|
60
|
+
// Create log
|
|
61
|
+
await assertSucceeds(
|
|
62
|
+
setDoc(logDoc, {
|
|
63
|
+
__sessionId: 'session_abc',
|
|
64
|
+
route: '/s/session_1',
|
|
65
|
+
startTime: Date.now(),
|
|
66
|
+
})
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// Update log
|
|
70
|
+
await assertSucceeds(
|
|
71
|
+
updateDoc(logDoc, {
|
|
72
|
+
_updatedAt: Date.now(),
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should deny anyone from reading logs', async () => {
|
|
78
|
+
const unauthedDb = testEnv.unauthenticatedContext().firestore();
|
|
79
|
+
const authDb = testEnv.authenticatedContext('student_1').firestore();
|
|
80
|
+
|
|
81
|
+
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
82
|
+
await setDoc(doc(context.firestore(), 'logs/log_123'), { data: 'private' });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await assertFails(getDoc(doc(unauthedDb, 'logs/log_123')));
|
|
86
|
+
await assertFails(getDoc(doc(authDb, 'logs/log_123')));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should deny anyone from deleting logs', async () => {
|
|
90
|
+
const unauthedDb = testEnv.unauthenticatedContext().firestore();
|
|
91
|
+
const authDb = testEnv.authenticatedContext('student_1').firestore();
|
|
92
|
+
|
|
93
|
+
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
94
|
+
await setDoc(doc(context.firestore(), 'logs/log_123'), { data: 'private' });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
await assertFails(deleteDoc(doc(unauthedDb, 'logs/log_123')));
|
|
98
|
+
await assertFails(deleteDoc(doc(authDb, 'logs/log_123')));
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -6,9 +6,10 @@ import {
|
|
|
6
6
|
assertSucceeds,
|
|
7
7
|
initializeTestEnvironment,
|
|
8
8
|
RulesTestEnvironment,
|
|
9
|
+
TokenOptions,
|
|
9
10
|
} from '@firebase/rules-unit-testing';
|
|
10
11
|
import { readFileSync } from 'fs';
|
|
11
|
-
import { doc, setDoc, updateDoc, getDoc, deleteDoc, collection, addDoc, query, where, getDocs } from 'firebase/firestore';
|
|
12
|
+
import { doc, setDoc, updateDoc, getDoc, deleteDoc, collection, addDoc, query, where, getDocs, writeBatch } from 'firebase/firestore';
|
|
12
13
|
|
|
13
14
|
let testEnv: RulesTestEnvironment;
|
|
14
15
|
|
|
@@ -21,6 +22,12 @@ beforeAll(async () => {
|
|
|
21
22
|
port: 8080,
|
|
22
23
|
},
|
|
23
24
|
});
|
|
25
|
+
const authenticatedContext = testEnv.authenticatedContext.bind(testEnv);
|
|
26
|
+
testEnv.authenticatedContext = (uid: string, options: TokenOptions = {}) => authenticatedContext(uid, {
|
|
27
|
+
email_verified: true,
|
|
28
|
+
firebase: { sign_in_provider: 'password' },
|
|
29
|
+
...options,
|
|
30
|
+
});
|
|
24
31
|
});
|
|
25
32
|
|
|
26
33
|
afterEach(async () => {
|
|
@@ -45,18 +52,34 @@ describe('Firestore Security Rules', () => {
|
|
|
45
52
|
|
|
46
53
|
it('should allow user to create their own profile', async () => {
|
|
47
54
|
const aliceDb = testEnv.authenticatedContext('alice').firestore();
|
|
55
|
+
await assertSucceeds(setDoc(doc(aliceDb, 'users/alice'), { name: 'Alice', isAdmin: false }));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should allow anonymous users to provision their own profile', async () => {
|
|
59
|
+
const guestDb = testEnv.authenticatedContext('guest', {
|
|
60
|
+
email_verified: false,
|
|
61
|
+
firebase: { sign_in_provider: 'anonymous' },
|
|
62
|
+
}).firestore();
|
|
48
63
|
await assertSucceeds(
|
|
49
|
-
setDoc(doc(
|
|
64
|
+
setDoc(doc(guestDb, 'users/guest'), { name: 'Guest', isAdmin: false }),
|
|
50
65
|
);
|
|
51
66
|
});
|
|
52
67
|
|
|
53
|
-
it('should deny user from
|
|
54
|
-
const
|
|
68
|
+
it('should deny an unverified password user from provisioning a profile', async () => {
|
|
69
|
+
const unverifiedDb = testEnv.authenticatedContext('unverified', {
|
|
70
|
+
email_verified: false,
|
|
71
|
+
firebase: { sign_in_provider: 'password' },
|
|
72
|
+
}).firestore();
|
|
55
73
|
await assertFails(
|
|
56
|
-
setDoc(doc(
|
|
74
|
+
setDoc(doc(unverifiedDb, 'users/unverified'), { name: 'Unverified', isAdmin: false }),
|
|
57
75
|
);
|
|
58
76
|
});
|
|
59
77
|
|
|
78
|
+
it('should deny user from creating profile with isAdmin=true', async () => {
|
|
79
|
+
const aliceDb = testEnv.authenticatedContext('alice').firestore();
|
|
80
|
+
await assertFails(setDoc(doc(aliceDb, 'users/alice'), { name: 'Alice', isAdmin: true }));
|
|
81
|
+
});
|
|
82
|
+
|
|
60
83
|
it('should deny user from updating their own isAdmin flag', async () => {
|
|
61
84
|
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
62
85
|
await setDoc(doc(context.firestore(), 'users/alice'), { name: 'Alice', isAdmin: false });
|
|
@@ -67,18 +90,30 @@ describe('Firestore Security Rules', () => {
|
|
|
67
90
|
|
|
68
91
|
it('should deny user from modifying adminOverridePaths', async () => {
|
|
69
92
|
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
70
|
-
await setDoc(doc(context.firestore(), 'users/alice'), {
|
|
93
|
+
await setDoc(doc(context.firestore(), 'users/alice'), {
|
|
94
|
+
name: 'Alice',
|
|
95
|
+
adminOverridePaths: [],
|
|
96
|
+
});
|
|
71
97
|
});
|
|
72
98
|
const aliceDb = testEnv.authenticatedContext('alice').firestore();
|
|
73
|
-
await assertFails(
|
|
99
|
+
await assertFails(
|
|
100
|
+
updateDoc(doc(aliceDb, 'users/alice'), { adminOverridePaths: ['some/path'] }),
|
|
101
|
+
);
|
|
74
102
|
});
|
|
75
103
|
|
|
76
104
|
it('should allow user to transition accessState from 01 to 02 and set joinedWaitlistAt', async () => {
|
|
77
105
|
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
78
|
-
await setDoc(doc(context.firestore(), 'users/alice'), {
|
|
106
|
+
await setDoc(doc(context.firestore(), 'users/alice'), {
|
|
107
|
+
accessState: '01 - submit interest',
|
|
108
|
+
});
|
|
79
109
|
});
|
|
80
110
|
const aliceDb = testEnv.authenticatedContext('alice').firestore();
|
|
81
|
-
await assertSucceeds(
|
|
111
|
+
await assertSucceeds(
|
|
112
|
+
updateDoc(doc(aliceDb, 'users/alice'), {
|
|
113
|
+
accessState: '02 - waiting',
|
|
114
|
+
joinedWaitlistAt: 123456789,
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
82
117
|
});
|
|
83
118
|
|
|
84
119
|
it('should deny user from transitioning accessState from 02 to 03', async () => {
|
|
@@ -108,7 +143,10 @@ describe('Firestore Security Rules', () => {
|
|
|
108
143
|
|
|
109
144
|
it('should deny user from modifying joinedWaitlistAt after it is set', async () => {
|
|
110
145
|
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
111
|
-
await setDoc(doc(context.firestore(), 'users/alice'), {
|
|
146
|
+
await setDoc(doc(context.firestore(), 'users/alice'), {
|
|
147
|
+
accessState: '02 - waiting',
|
|
148
|
+
joinedWaitlistAt: 123,
|
|
149
|
+
});
|
|
112
150
|
});
|
|
113
151
|
const aliceDb = testEnv.authenticatedContext('alice').firestore();
|
|
114
152
|
await assertFails(updateDoc(doc(aliceDb, 'users/alice'), { joinedWaitlistAt: 456 }));
|
|
@@ -151,12 +189,45 @@ describe('Firestore Security Rules', () => {
|
|
|
151
189
|
});
|
|
152
190
|
|
|
153
191
|
describe('UserContexts Collection', () => {
|
|
192
|
+
it('should allow the no-waitlist interest submission as one atomic batch', async () => {
|
|
193
|
+
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
194
|
+
await setDoc(doc(context.firestore(), 'users/alice'), {
|
|
195
|
+
accessState: '01 - submit interest',
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
const aliceDb = testEnv.authenticatedContext('alice').firestore();
|
|
199
|
+
const batch = writeBatch(aliceDb);
|
|
200
|
+
batch.update(doc(aliceDb, 'users/alice'), {
|
|
201
|
+
accessState: '03 - consent',
|
|
202
|
+
joinedWaitlistAt: 123456789,
|
|
203
|
+
});
|
|
204
|
+
batch.set(doc(aliceDb, 'userContexts/context-alice'), {
|
|
205
|
+
__userId: 'alice',
|
|
206
|
+
questionOne: 'Example context',
|
|
207
|
+
questionTwo: 'Example needs',
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
await assertSucceeds(batch.commit());
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('should deny unverified non-anonymous users from app data', async () => {
|
|
214
|
+
const unverifiedDb = testEnv.authenticatedContext('unverified', {
|
|
215
|
+
email_verified: false,
|
|
216
|
+
firebase: { sign_in_provider: 'password' },
|
|
217
|
+
}).firestore();
|
|
218
|
+
await assertFails(
|
|
219
|
+
setDoc(doc(unverifiedDb, 'userContexts/context-unverified'), {
|
|
220
|
+
__userId: 'unverified',
|
|
221
|
+
}),
|
|
222
|
+
);
|
|
223
|
+
});
|
|
224
|
+
|
|
154
225
|
it('should allow user to create their own context', async () => {
|
|
155
226
|
const aliceDb = testEnv.authenticatedContext('alice').firestore();
|
|
156
227
|
await assertSucceeds(
|
|
157
228
|
setDoc(doc(aliceDb, 'userContexts/ctx1'), {
|
|
158
229
|
__userId: 'alice',
|
|
159
|
-
})
|
|
230
|
+
}),
|
|
160
231
|
);
|
|
161
232
|
});
|
|
162
233
|
|
|
@@ -165,7 +236,7 @@ describe('Firestore Security Rules', () => {
|
|
|
165
236
|
await assertFails(
|
|
166
237
|
setDoc(doc(aliceDb, 'userContexts/ctx1'), {
|
|
167
238
|
__userId: 'bob',
|
|
168
|
-
})
|
|
239
|
+
}),
|
|
169
240
|
);
|
|
170
241
|
});
|
|
171
242
|
|
|
@@ -199,9 +270,7 @@ describe('Firestore Security Rules', () => {
|
|
|
199
270
|
await setDoc(doc(context.firestore(), 'userContexts/ctx1'), { __userId: 'alice' });
|
|
200
271
|
});
|
|
201
272
|
const aliceDb = testEnv.authenticatedContext('alice').firestore();
|
|
202
|
-
await assertFails(
|
|
203
|
-
updateDoc(doc(aliceDb, 'userContexts/ctx1'), { __userId: 'bob' })
|
|
204
|
-
);
|
|
273
|
+
await assertFails(updateDoc(doc(aliceDb, 'userContexts/ctx1'), { __userId: 'bob' }));
|
|
205
274
|
});
|
|
206
275
|
|
|
207
276
|
it('should allow user to delete their own context', async () => {
|
|
@@ -3,7 +3,16 @@ service cloud.firestore {
|
|
|
3
3
|
match /databases/{database}/documents {
|
|
4
4
|
|
|
5
5
|
function isAdmin() {
|
|
6
|
-
return
|
|
6
|
+
return exists(/databases/$(database)/documents/users/$(request.auth.uid)) &&
|
|
7
|
+
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.isAdmin == true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function isVerifiedOrAnonymous() {
|
|
11
|
+
return request.auth != null && (
|
|
12
|
+
request.auth.token.firebase.sign_in_provider == 'anonymous' ||
|
|
13
|
+
request.auth.token.firebase.sign_in_provider == 'google.com' ||
|
|
14
|
+
('email_verified' in request.auth.token && request.auth.token.email_verified == true)
|
|
15
|
+
);
|
|
7
16
|
}
|
|
8
17
|
|
|
9
18
|
// ─── Users ───────────────────────────────────────────────────────────
|
|
@@ -12,40 +21,43 @@ service cloud.firestore {
|
|
|
12
21
|
// Update: only the user themselves or admins. Regular users are restricted from modifying state bypasses or sensitive lists.
|
|
13
22
|
// Delete: only the user themselves or admins.
|
|
14
23
|
match /users/{userId} {
|
|
15
|
-
allow create: if
|
|
24
|
+
allow create: if isVerifiedOrAnonymous()
|
|
16
25
|
&& request.auth.uid == userId
|
|
17
26
|
&& (!('isAdmin' in request.resource.data) || request.resource.data.isAdmin == false || isAdmin());
|
|
18
|
-
allow read: if
|
|
19
|
-
allow update: if
|
|
27
|
+
allow read: if isVerifiedOrAnonymous();
|
|
28
|
+
allow update: if isVerifiedOrAnonymous()
|
|
20
29
|
&& (isAdmin() || (
|
|
21
30
|
request.auth.uid == userId
|
|
22
31
|
// Cannot grant themselves admin privileges
|
|
23
|
-
&& (!('isAdmin' in request.resource.data) || request.resource.data.isAdmin == resource.data.isAdmin)
|
|
32
|
+
&& (!('isAdmin' in request.resource.data) || !('isAdmin' in resource.data) || request.resource.data.isAdmin == resource.data.isAdmin)
|
|
24
33
|
// Cannot alter adminOverridePaths
|
|
25
|
-
&& (!('adminOverridePaths' in request.resource.data) || request.resource.data.adminOverridePaths == resource.data.adminOverridePaths)
|
|
34
|
+
&& (!('adminOverridePaths' in request.resource.data) || !('adminOverridePaths' in resource.data) || request.resource.data.adminOverridePaths == resource.data.adminOverridePaths)
|
|
26
35
|
// Must follow accessState transitions if changing accessState
|
|
27
36
|
&& (!('accessState' in request.resource.data)
|
|
37
|
+
|| !('accessState' in resource.data)
|
|
28
38
|
|| request.resource.data.accessState == resource.data.accessState
|
|
29
39
|
|| (resource.data.accessState == '01 - submit interest' && request.resource.data.accessState == '02 - waiting')
|
|
40
|
+
|| (resource.data.accessState == '01 - submit interest' && request.resource.data.accessState == '03 - consent')
|
|
30
41
|
|| (resource.data.accessState == '03 - consent' && request.resource.data.accessState == 'done')
|
|
31
42
|
)
|
|
32
|
-
// Cannot modify joinedWaitlistAt unless transitioning
|
|
43
|
+
// Cannot modify joinedWaitlistAt unless transitioning from submit interest
|
|
33
44
|
&& (!('joinedWaitlistAt' in request.resource.data)
|
|
45
|
+
|| !('joinedWaitlistAt' in resource.data)
|
|
34
46
|
|| request.resource.data.joinedWaitlistAt == resource.data.joinedWaitlistAt
|
|
35
|
-
|| (resource.data.accessState == '01 - submit interest' && request.resource.data.accessState == '02 - waiting')
|
|
47
|
+
|| (resource.data.accessState == '01 - submit interest' && (request.resource.data.accessState == '02 - waiting' || request.resource.data.accessState == '03 - consent'))
|
|
36
48
|
)
|
|
37
49
|
));
|
|
38
|
-
allow delete: if
|
|
50
|
+
allow delete: if isVerifiedOrAnonymous()
|
|
39
51
|
&& (request.auth.uid == userId || isAdmin());
|
|
40
52
|
}
|
|
41
53
|
|
|
42
54
|
// ─── User Privates ───────────────────────────────────────────────────
|
|
43
55
|
// Create/Read/Update/Delete: only the user themselves (document ID == auth.uid) or admins
|
|
44
56
|
match /userPrivates/{userId} {
|
|
45
|
-
allow create: if
|
|
46
|
-
allow read: if
|
|
47
|
-
allow update: if
|
|
48
|
-
allow delete: if
|
|
57
|
+
allow create: if isVerifiedOrAnonymous() && request.auth.uid == userId;
|
|
58
|
+
allow read: if isVerifiedOrAnonymous() && (request.auth.uid == userId || isAdmin());
|
|
59
|
+
allow update: if isVerifiedOrAnonymous() && (request.auth.uid == userId || isAdmin());
|
|
60
|
+
allow delete: if isVerifiedOrAnonymous() && (request.auth.uid == userId || isAdmin());
|
|
49
61
|
}
|
|
50
62
|
|
|
51
63
|
// ─── User Contexts ───────────────────────────────────────────────────
|
|
@@ -54,17 +66,17 @@ service cloud.firestore {
|
|
|
54
66
|
// Update: only the user themselves or admins. Prevent transferring context.
|
|
55
67
|
// Delete: only the user themselves or admins
|
|
56
68
|
match /userContexts/{contextId} {
|
|
57
|
-
allow create: if
|
|
69
|
+
allow create: if isVerifiedOrAnonymous()
|
|
58
70
|
&& request.resource.data.__userId == request.auth.uid;
|
|
59
|
-
allow read: if
|
|
71
|
+
allow read: if isVerifiedOrAnonymous()
|
|
60
72
|
&& (resource.data.__userId == request.auth.uid || isAdmin());
|
|
61
|
-
allow update: if
|
|
73
|
+
allow update: if isVerifiedOrAnonymous()
|
|
62
74
|
&& (isAdmin() || (
|
|
63
75
|
resource.data.__userId == request.auth.uid
|
|
64
76
|
// Prevent users from transferring their userContext to another user
|
|
65
77
|
&& (!('__userId' in request.resource.data) || request.resource.data.__userId == resource.data.__userId)
|
|
66
78
|
));
|
|
67
|
-
allow delete: if
|
|
79
|
+
allow delete: if isVerifiedOrAnonymous()
|
|
68
80
|
&& (resource.data.__userId == request.auth.uid || isAdmin());
|
|
69
81
|
}
|
|
70
82
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "<%= dasherize(project) %>-functions",
|
|
3
|
+
"private": true,
|
|
4
|
+
"main": "lib/index.js",
|
|
5
|
+
"engines": { "node": "22" },
|
|
6
|
+
"scripts": { "build": "tsc", "test": "npm run build" },
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"firebase-admin": "^13.10.0",
|
|
9
|
+
"firebase-functions": "^7.3.0"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": { "typescript": "~5.8.3" }
|
|
12
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { initializeApp } from 'firebase-admin/app';
|
|
2
|
+
import { getAuth } from 'firebase-admin/auth';
|
|
3
|
+
import { getFirestore } from 'firebase-admin/firestore';
|
|
4
|
+
import { HttpsError, onCall } from 'firebase-functions/v2/https';
|
|
5
|
+
|
|
6
|
+
initializeApp();
|
|
7
|
+
|
|
8
|
+
// Add application-owned collections keyed by __userId when the project introduces them.
|
|
9
|
+
const QUERY_COLLECTIONS = ['userContexts'] as const;
|
|
10
|
+
|
|
11
|
+
async function deleteQueryInBatches(collectionName: typeof QUERY_COLLECTIONS[number], uid: string): Promise<void> {
|
|
12
|
+
const firestore = getFirestore();
|
|
13
|
+
const snapshot = await firestore.collection(collectionName).where('__userId', '==', uid).get();
|
|
14
|
+
for (let start = 0; start < snapshot.docs.length; start += 450) {
|
|
15
|
+
const batch = firestore.batch();
|
|
16
|
+
snapshot.docs.slice(start, start + 450).forEach((document) => batch.delete(document.ref));
|
|
17
|
+
await batch.commit();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Permanently removes an anonymous account and its currently-owned application data. */
|
|
22
|
+
export const deleteAnonymousAccount = onCall(async (request) => {
|
|
23
|
+
if (!request.auth || request.auth.token.firebase?.sign_in_provider !== 'anonymous') {
|
|
24
|
+
throw new HttpsError('permission-denied', 'Only an anonymous account can delete itself here.');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const uid = request.auth.uid;
|
|
28
|
+
const firestore = getFirestore();
|
|
29
|
+
for (const collectionName of QUERY_COLLECTIONS) await deleteQueryInBatches(collectionName, uid);
|
|
30
|
+
|
|
31
|
+
const batch = firestore.batch();
|
|
32
|
+
[
|
|
33
|
+
firestore.doc(`users/${uid}`),
|
|
34
|
+
firestore.doc(`userPrivates/${uid}`),
|
|
35
|
+
].forEach((reference) => batch.delete(reference));
|
|
36
|
+
await batch.commit();
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
await getAuth().deleteUser(uid);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
if ((error as { code?: string }).code !== 'auth/user-not-found') throw error;
|
|
42
|
+
}
|
|
43
|
+
return { deleted: true };
|
|
44
|
+
});
|
|
@@ -60,7 +60,7 @@ const config: Config = {
|
|
|
60
60
|
'/node_modules/',
|
|
61
61
|
'/dist/',
|
|
62
62
|
'<rootDir>/src/environments/environment.test.ts',
|
|
63
|
-
...(process.env['JEST_RULES'] ? [] : ['<rootDir>/firestore.rules.spec.ts'])
|
|
63
|
+
...(process.env['JEST_RULES'] ? [] : ['<rootDir>/firestore.rules.spec.ts', '<rootDir>/firestore.logs.rules.spec.ts'])
|
|
64
64
|
],
|
|
65
65
|
collectCoverageFrom: [
|
|
66
66
|
'src/app/**/*.ts',
|
|
@@ -10,13 +10,16 @@
|
|
|
10
10
|
"test": "jest",
|
|
11
11
|
"test:watch": "jest --watch",
|
|
12
12
|
"test:coverage": "jest --coverage",
|
|
13
|
-
"test:rules": "firebase emulators:exec --only firestore 'JEST_RULES=1 jest firestore.rules.spec.ts'",
|
|
13
|
+
"test:rules": "firebase emulators:exec --only firestore 'JEST_RULES=1 jest firestore.rules.spec.ts firestore.logs.rules.spec.ts'",
|
|
14
14
|
"lint": "ng lint",
|
|
15
15
|
"lintfix": "ng lint --fix",
|
|
16
16
|
"code-check": "ngc --noEmit && sass src:dist/css-check --no-source-map && rm -rf dist/css-check && npm run lintfix",
|
|
17
17
|
"analyze": "ng build --stats-json && esbuild-visualizer --metadata dist/<%= dasherize(project) %>/stats.json --filename dist/<%= dasherize(project) %>/bundle-analysis.html && open dist/<%= dasherize(project) %>/bundle-analysis.html",
|
|
18
|
-
"deploy:dev": "ng build --configuration development && firebase deploy --project dev",
|
|
19
|
-
"deploy:prod": "ng build --configuration production && firebase deploy --project prod"
|
|
18
|
+
"deploy:dev": "npm --prefix functions run build && ng build --configuration development && firebase deploy --project dev",
|
|
19
|
+
"deploy:prod": "npm --prefix functions run build && ng build --configuration production && firebase deploy --project prod",
|
|
20
|
+
"deploy:functions:dev": "npm --prefix functions run build && firebase deploy --only functions --project dev",
|
|
21
|
+
"deploy:functions:prod": "npm --prefix functions run build && firebase deploy --only functions --project prod",
|
|
22
|
+
"deploy:logs": "firebase deploy --only firestore:rules --project log --config firebase.logs.json"
|
|
20
23
|
},
|
|
21
24
|
"private": true,
|
|
22
25
|
"dependencies": {
|
|
@@ -55,7 +58,7 @@
|
|
|
55
58
|
"@firebase/rules-unit-testing": "^5.0.1",
|
|
56
59
|
"firebase-tools": "^14.27.0",
|
|
57
60
|
"@ngx-env/builder": "^21.0.1",
|
|
58
|
-
"@tech4good/angular-schematics": "^21.
|
|
61
|
+
"@tech4good/angular-schematics": "^21.6.0",
|
|
59
62
|
"@types/estree": "^1.0.0",
|
|
60
63
|
"@types/jest": "^30.0.0",
|
|
61
64
|
"@types/node": "^18.19.47",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { render, screen } from '@testing-library/angular';
|
|
2
2
|
import userEvent from '@testing-library/user-event';
|
|
3
|
-
import { Router, ActivatedRoute } from '@angular/router';
|
|
3
|
+
import { provideRouter, Router, ActivatedRoute } from '@angular/router';
|
|
4
4
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
5
5
|
import { Provider } from '@angular/core';
|
|
6
6
|
|
|
@@ -17,11 +17,6 @@ import { UserContextStore } from 'src/app/core/store/user-context/user-context.s
|
|
|
17
17
|
/** Sets up the testing environment for AdminPanelComponent using render. */
|
|
18
18
|
async function setup(providers: Provider[] = [], componentInputs: any = {}) {
|
|
19
19
|
const user = userEvent.setup();
|
|
20
|
-
const routerMock = {
|
|
21
|
-
navigate: jest.fn(),
|
|
22
|
-
url: '/admin',
|
|
23
|
-
events: { pipe: () => ({ subscribe: () => {} }) },
|
|
24
|
-
};
|
|
25
20
|
const activatedRouteMock = {
|
|
26
21
|
snapshot: {
|
|
27
22
|
paramMap: { get: () => null },
|
|
@@ -34,7 +29,7 @@ async function setup(providers: Provider[] = [], componentInputs: any = {}) {
|
|
|
34
29
|
|
|
35
30
|
const view = await render(AdminPanelComponent, {
|
|
36
31
|
providers: [
|
|
37
|
-
|
|
32
|
+
provideRouter([]),
|
|
38
33
|
{ provide: ActivatedRoute, useValue: activatedRouteMock },
|
|
39
34
|
{ provide: MatSnackBar, useValue: snackBarMock },
|
|
40
35
|
{ provide: DATABASE_SERVICE, useClass: FirebaseMockService },
|
|
@@ -47,10 +42,13 @@ async function setup(providers: Provider[] = [], componentInputs: any = {}) {
|
|
|
47
42
|
componentInputs,
|
|
48
43
|
});
|
|
49
44
|
|
|
45
|
+
const router = view.fixture.debugElement.injector.get(Router);
|
|
46
|
+
const navigateSpy = jest.spyOn(router, 'navigate').mockImplementation(async () => true);
|
|
47
|
+
|
|
50
48
|
return {
|
|
51
49
|
user,
|
|
52
50
|
fixture: view.fixture,
|
|
53
|
-
routerMock,
|
|
51
|
+
routerMock: { navigate: navigateSpy },
|
|
54
52
|
snackBarMock,
|
|
55
53
|
view,
|
|
56
54
|
};
|
|
@@ -3,6 +3,7 @@ import { ActivatedRoute, Router } from '@angular/router';
|
|
|
3
3
|
import { MatIcon } from '@angular/material/icon';
|
|
4
4
|
import { AdminUserListComponent } from './admin-user-list/admin-user-list.component';
|
|
5
5
|
import { AdminSettingsComponent } from './admin-settings/admin-settings.component';
|
|
6
|
+
import { NavbarComponent } from 'src/app/shared/navbar/navbar.component';
|
|
6
7
|
|
|
7
8
|
/** The available tabs in the admin panel. */
|
|
8
9
|
type AdminTab = 'users' | 'settings';
|
|
@@ -18,6 +19,7 @@ type AdminTab = 'users' | 'settings';
|
|
|
18
19
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
19
20
|
imports: [
|
|
20
21
|
MatIcon,
|
|
22
|
+
NavbarComponent,
|
|
21
23
|
AdminUserListComponent,
|
|
22
24
|
AdminSettingsComponent,
|
|
23
25
|
],
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
<app-dev-status-bar></app-dev-status-bar>
|
|
2
1
|
<div class="content-container">
|
|
2
|
+
<app-dev-status-bar></app-dev-status-bar>
|
|
3
|
+
<app-guest-warning-bar></app-guest-warning-bar>
|
|
3
4
|
<app-redeployment-warning></app-redeployment-warning>
|
|
4
5
|
<router-outlet></router-outlet>
|
|
5
6
|
</div>
|
|
6
|
-
|
|
@@ -6,6 +6,7 @@ import { AuthStore } from './core/store/auth/auth.store';
|
|
|
6
6
|
import { DetectRedeploymentService } from './shared/redeployment-warning/redeployment-warning.service';
|
|
7
7
|
import { RedeploymentWarningComponent } from './shared/redeployment-warning/redeployment-warning.component';
|
|
8
8
|
import { DevStatusBarComponent } from './shared/dev-status-bar/dev-status-bar.component';
|
|
9
|
+
import { GuestWarningBarComponent } from './shared/guest-warning-bar/guest-warning-bar.component';
|
|
9
10
|
|
|
10
11
|
@Component({
|
|
11
12
|
selector: 'app-root',
|
|
@@ -14,6 +15,7 @@ import { DevStatusBarComponent } from './shared/dev-status-bar/dev-status-bar.co
|
|
|
14
15
|
imports: [
|
|
15
16
|
RedeploymentWarningComponent,
|
|
16
17
|
DevStatusBarComponent,
|
|
18
|
+
GuestWarningBarComponent,
|
|
17
19
|
RouterOutlet,
|
|
18
20
|
],
|
|
19
21
|
})
|
|
@@ -12,6 +12,10 @@ export const routes: Routes = [
|
|
|
12
12
|
.then((mod) => mod.LandingComponent), canActivate: [redirectIfAuthenticated] },
|
|
13
13
|
{ path: 'login', loadComponent: () => import('./first-time/login/login.component')
|
|
14
14
|
.then((mod) => mod.LoginComponent), canActivate: [redirectIfAuthenticated] },
|
|
15
|
+
{ path: 'auth/action', loadComponent: () => import('./first-time/auth-action/auth-action.component')
|
|
16
|
+
.then((mod) => mod.AuthActionComponent) },
|
|
17
|
+
{ path: 'email-link', loadComponent: () => import('./first-time/email-link/email-link.component')
|
|
18
|
+
.then((mod) => mod.EmailLinkComponent) },
|
|
15
19
|
// First-Time User Experience Routes (gated pages)
|
|
16
20
|
{ path: 'consent', loadComponent: () => import('./first-time/consent/consent.component')
|
|
17
21
|
.then((mod) => mod.ConsentComponent), canActivate: [requireAuth, consentGuard] },
|
|
@@ -20,6 +24,8 @@ export const routes: Routes = [
|
|
|
20
24
|
{ path: 'onboarding', loadComponent: () => import('./first-time/onboarding/onboarding.component')
|
|
21
25
|
.then((mod) => mod.OnboardingComponent), canActivate: [requireAuthAndConsent] },
|
|
22
26
|
// Main Routes
|
|
27
|
+
{ path: 'account', loadComponent: () => import('./main/account-profile/account-profile.component')
|
|
28
|
+
.then((mod) => mod.AccountProfileComponent), canActivate: [requireAuth] },
|
|
23
29
|
{ path: 'home', loadComponent: () => import('./main/home/home.component')
|
|
24
30
|
.then((mod) => mod.HomeComponent), canActivate: [requireAuthAndConsent] },
|
|
25
31
|
{ path: '', loadComponent: () => import('./first-time/landing/landing.component')
|