@tech4good/angular-schematics 21.5.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 +16 -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/.gitignore.template +2 -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/firestore.indexes.json.template +23 -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/package.json.template +5 -3
- 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/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 +7 -2
- 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.guard.ts.template +5 -1
- 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 +5 -1
- 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 +7 -2
- 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
|
@@ -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
|
+
});
|
|
@@ -15,8 +15,10 @@
|
|
|
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",
|
|
20
22
|
"deploy:logs": "firebase deploy --only firestore:rules --project log --config firebase.logs.json"
|
|
21
23
|
},
|
|
22
24
|
"private": true,
|
|
@@ -56,7 +58,7 @@
|
|
|
56
58
|
"@firebase/rules-unit-testing": "^5.0.1",
|
|
57
59
|
"firebase-tools": "^14.27.0",
|
|
58
60
|
"@ngx-env/builder": "^21.0.1",
|
|
59
|
-
"@tech4good/angular-schematics": "^21.
|
|
61
|
+
"@tech4good/angular-schematics": "^21.6.0",
|
|
60
62
|
"@types/estree": "^1.0.0",
|
|
61
63
|
"@types/jest": "^30.0.0",
|
|
62
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')
|
|
@@ -18,12 +18,13 @@ import { createMockUser } from './core/store/user/user.mock';
|
|
|
18
18
|
describe('Core Auth Routing', () => {
|
|
19
19
|
let harness: RouterTestingHarness;
|
|
20
20
|
let router: Router;
|
|
21
|
-
let mockAuthService: { resolveUser: jest.Mock };
|
|
21
|
+
let mockAuthService: { resolveUser: jest.Mock; resolveSession: jest.Mock };
|
|
22
22
|
let mockUserSignal: any;
|
|
23
23
|
|
|
24
24
|
beforeEach(async () => {
|
|
25
25
|
mockAuthService = {
|
|
26
26
|
resolveUser: jest.fn().mockReturnValue(of(null)),
|
|
27
|
+
resolveSession: jest.fn().mockReturnValue(of(null)),
|
|
27
28
|
};
|
|
28
29
|
mockUserSignal = signal(null);
|
|
29
30
|
|
|
@@ -53,7 +54,7 @@ describe('Core Auth Routing', () => {
|
|
|
53
54
|
expect(router.url).toEqual('/landing');
|
|
54
55
|
});
|
|
55
56
|
|
|
56
|
-
it('should intercept navigation to /admin and redirect to /
|
|
57
|
+
it('should intercept navigation to /admin and redirect through /landing to /home', async () => {
|
|
57
58
|
await harness.navigateByUrl('/admin');
|
|
58
59
|
// Wait for async routing to settle
|
|
59
60
|
await harness.fixture.whenStable();
|
|
@@ -64,6 +65,7 @@ describe('Core Auth Routing', () => {
|
|
|
64
65
|
describe('Authenticated User (Standard)', () => {
|
|
65
66
|
beforeEach(() => {
|
|
66
67
|
mockAuthService.resolveUser.mockReturnValue(of(createMockUser()));
|
|
68
|
+
mockAuthService.resolveSession.mockReturnValue(of({ isAnonymous: false, emailVerified: true }));
|
|
67
69
|
mockUserSignal.set(createMockUser());
|
|
68
70
|
});
|
|
69
71
|
|
|
@@ -73,7 +75,7 @@ describe('Core Auth Routing', () => {
|
|
|
73
75
|
expect(router.url).toEqual('/home');
|
|
74
76
|
});
|
|
75
77
|
|
|
76
|
-
it('should intercept navigation to /admin and redirect to /
|
|
78
|
+
it('should intercept navigation to /admin and redirect to /landing', async () => {
|
|
77
79
|
await harness.navigateByUrl('/admin');
|
|
78
80
|
await harness.fixture.whenStable();
|
|
79
81
|
expect(router.url).toEqual('/home');
|
|
@@ -82,7 +84,10 @@ describe('Core Auth Routing', () => {
|
|
|
82
84
|
|
|
83
85
|
describe('Admin User', () => {
|
|
84
86
|
beforeEach(() => {
|
|
85
|
-
mockAuthService.resolveUser.mockReturnValue(
|
|
87
|
+
mockAuthService.resolveUser.mockReturnValue(
|
|
88
|
+
of(createMockUser({ isAdmin: true, __id: 'admin1' })),
|
|
89
|
+
);
|
|
90
|
+
mockAuthService.resolveSession.mockReturnValue(of({ isAnonymous: false, emailVerified: true }));
|
|
86
91
|
mockUserSignal.set(createMockUser({ isAdmin: true, __id: 'admin1' }));
|
|
87
92
|
});
|
|
88
93
|
|
|
@@ -4,7 +4,7 @@ import { filter, switchMap, take } from 'rxjs/operators';
|
|
|
4
4
|
import { toObservable } from '@angular/core/rxjs-interop';
|
|
5
5
|
import { User } from '../store/user/user.model';
|
|
6
6
|
import { AuthStore } from '../store/auth/auth.store';
|
|
7
|
-
import { DATABASE_SERVICE } from '../firebase/database.service';
|
|
7
|
+
import { AuthSession, DATABASE_SERVICE } from '../firebase/database.service';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Shared authentication service used by all functional guards.
|
|
@@ -22,6 +22,11 @@ export class AuthService {
|
|
|
22
22
|
/** Firebase auth user stream (null when signed out). */
|
|
23
23
|
readonly afUser$ = this.db.afUser();
|
|
24
24
|
|
|
25
|
+
/** Resolves Firebase session metadata for guards that enforce provider state. */
|
|
26
|
+
resolveSession(): Observable<AuthSession | null> {
|
|
27
|
+
return this.afUser$.pipe(take(1));
|
|
28
|
+
}
|
|
29
|
+
|
|
25
30
|
/**
|
|
26
31
|
* Database User record stream. Only emits once the AuthStore has loaded
|
|
27
32
|
* a non-null user value. This is safe to use in guards that already know
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
|
|
2
|
+
import { AccessState, User } from '../../store/user/user.model';
|
|
3
|
+
import { handleAccess } from './require-admin.guard';
|
|
4
|
+
|
|
5
|
+
describe('requireAdmin handleAccess', () => {
|
|
6
|
+
const route = {
|
|
7
|
+
queryParams: { source: 'admin' },
|
|
8
|
+
} as unknown as ActivatedRouteSnapshot;
|
|
9
|
+
const state = { url: '/admin' } as RouterStateSnapshot;
|
|
10
|
+
|
|
11
|
+
function createUser(overrides: Partial<User> = {}): User {
|
|
12
|
+
return {
|
|
13
|
+
__id: 'user-1',
|
|
14
|
+
name: 'Application User',
|
|
15
|
+
isAdmin: false,
|
|
16
|
+
accessState: AccessState.DONE,
|
|
17
|
+
consented: true,
|
|
18
|
+
...overrides,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
it('allows administrators without another Firestore lookup', () => {
|
|
23
|
+
const router = { navigate: jest.fn() } as unknown as Router;
|
|
24
|
+
|
|
25
|
+
expect(handleAccess(createUser({ isAdmin: true }), router, route, state)).toBe(true);
|
|
26
|
+
expect(router.navigate).not.toHaveBeenCalled();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('routes non-admin users through landing so the public guard chooses the destination', () => {
|
|
30
|
+
const router = { navigate: jest.fn() } as unknown as Router;
|
|
31
|
+
|
|
32
|
+
expect(handleAccess(createUser(), router, route, state)).toBe(false);
|
|
33
|
+
expect(router.navigate).toHaveBeenCalledWith(
|
|
34
|
+
['/landing'],
|
|
35
|
+
{ queryParams: { source: 'admin' } },
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -6,7 +6,8 @@ import { AuthService } from '../auth.service';
|
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Restricts access to admin users only. Non-admin users are redirected
|
|
9
|
-
* to `/
|
|
9
|
+
* to `/landing`, where the public-page guard can reroute an authenticated
|
|
10
|
+
* non-admin user to the normal application home.
|
|
10
11
|
*
|
|
11
12
|
* This guard should always be placed *after* `requireAuth` in the
|
|
12
13
|
* `canActivate` array, since it assumes the user is already authenticated.
|
|
@@ -18,7 +19,7 @@ export function handleAccess(
|
|
|
18
19
|
state: RouterStateSnapshot,
|
|
19
20
|
): boolean {
|
|
20
21
|
if (!user?.isAdmin) {
|
|
21
|
-
router.navigate(['/
|
|
22
|
+
router.navigate(['/landing'], { queryParams: route.queryParams });
|
|
22
23
|
return false;
|
|
23
24
|
}
|
|
24
25
|
return true;
|