Skip to content

Commit 28804b3

Browse files
committed
fix(backend): add 'structureId' by default
1 parent 08c0cd8 commit 28804b3

File tree

9 files changed

+80
-36
lines changed

9 files changed

+80
-36
lines changed
16 Bytes
Binary file not shown.

_scripts/db/dumps/domifa_test.postgres.restore-data-only.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,16 @@ c9192746-debf-48de-bd13-6fb5b2ee866b 2022-03-08 23:22:50.29536+01 2022-03-08 23:
301301
--
302302
303303
COPY public.user_structure_security (uuid, "createdAt", "updatedAt", version, "userId", "temporaryTokens", "eventsHistory", "structureId") FROM stdin;
304-
7aef1d02-3021-4988-937c-f18fa6244b14 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 2 \N [] \N
305-
0068d982-390a-4c42-9b63-55f5c78c6cfd 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 5 \N [] \N
306-
e931e0d8-ecbb-478a-97a3-a01eac88e24f 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 4 \N [] \N
307-
3e2118c1-1e0b-4ce7-bc85-22001eebc8ee 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 7 \N [] \N
308-
8dddd469-7b63-44f3-8166-15830e80ee7b 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 6 \N [] \N
309-
759bdc26-be9d-4ffb-95c9-8c2d7a06a63b 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 8 \N [] \N
310-
d84e9cc2-5c52-4a02-880e-bcfb27180594 2021-03-15 16:53:55.74+01 2021-12-06 16:26:00.681872+01 2 10 \N [{"date": "2021-12-06T15:26:00.672Z", "type": "login-success"}] \N
311-
9fe998b5-7178-44b5-9c40-2e18ba233f1d 2021-03-15 16:53:55.740856+01 2022-03-09 00:20:17.365808+01 3 3 \N [{"date": "2022-03-08T23:20:17.329Z", "type": "login-success"}] \N
312-
80e97a87-9d11-4487-a3c3-411bbdf42a2c 2022-03-17 17:25:53.809705+01 2022-03-23 22:07:14.867562+01 3 11 \N [{"date": "2022-03-17T16:33:43.464Z", "type": "login-success"}, {"date": "2022-03-23T21:07:14.852Z", "type": "login-success"}] \N
313-
e0bcefc6-f1be-4c83-ac9d-6ea47335a9c3 2021-03-15 16:53:55.740856+01 2025-03-06 00:34:34.261953+01 10 1 \N [{"date": "2025-03-05T23:34:34.260Z", "type": "login-error"}] \N
304+
7aef1d02-3021-4988-937c-f18fa6244b14 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 2 \N [] 1
305+
0068d982-390a-4c42-9b63-55f5c78c6cfd 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 5 \N [] 3
306+
e931e0d8-ecbb-478a-97a3-a01eac88e24f 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 4 \N [] 3
307+
3e2118c1-1e0b-4ce7-bc85-22001eebc8ee 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 7 \N [] 4
308+
8dddd469-7b63-44f3-8166-15830e80ee7b 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 6 \N [] 1
309+
759bdc26-be9d-4ffb-95c9-8c2d7a06a63b 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 8 \N [] 3
310+
d84e9cc2-5c52-4a02-880e-bcfb27180594 2021-03-15 16:53:55.74+01 2021-12-06 16:26:00.681872+01 2 10 \N [{"date": "2021-12-06T15:26:00.672Z", "type": "login-success"}] 3
311+
9fe998b5-7178-44b5-9c40-2e18ba233f1d 2021-03-15 16:53:55.740856+01 2022-03-09 00:20:17.365808+01 3 3 \N [{"date": "2022-03-08T23:20:17.329Z", "type": "login-success"}] 1
312+
80e97a87-9d11-4487-a3c3-411bbdf42a2c 2022-03-17 17:25:53.809705+01 2022-03-23 22:07:14.867562+01 3 11 \N [{"date": "2022-03-17T16:33:43.464Z", "type": "login-success"}, {"date": "2022-03-23T21:07:14.852Z", "type": "login-success"}] 5
313+
e0bcefc6-f1be-4c83-ac9d-6ea47335a9c3 2021-03-15 16:53:55.740856+01 2025-03-06 00:34:34.261953+01 10 1 \N [{"date": "2025-03-05T23:34:34.260Z", "type": "login-error"}] 1
314314
\.
315315
316316

_scripts/db/dumps/domifa_test.postgres.truncate-restore-data-only.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,16 @@ c9192746-debf-48de-bd13-6fb5b2ee866b 2022-03-08 23:22:50.29536+01 2022-03-08 23:
337337
--
338338
339339
COPY public.user_structure_security (uuid, "createdAt", "updatedAt", version, "userId", "temporaryTokens", "eventsHistory", "structureId") FROM stdin;
340-
7aef1d02-3021-4988-937c-f18fa6244b14 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 2 \N [] \N
341-
0068d982-390a-4c42-9b63-55f5c78c6cfd 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 5 \N [] \N
342-
e931e0d8-ecbb-478a-97a3-a01eac88e24f 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 4 \N [] \N
343-
3e2118c1-1e0b-4ce7-bc85-22001eebc8ee 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 7 \N [] \N
344-
8dddd469-7b63-44f3-8166-15830e80ee7b 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 6 \N [] \N
345-
759bdc26-be9d-4ffb-95c9-8c2d7a06a63b 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 8 \N [] \N
346-
d84e9cc2-5c52-4a02-880e-bcfb27180594 2021-03-15 16:53:55.74+01 2021-12-06 16:26:00.681872+01 2 10 \N [{"date": "2021-12-06T15:26:00.672Z", "type": "login-success"}] \N
347-
9fe998b5-7178-44b5-9c40-2e18ba233f1d 2021-03-15 16:53:55.740856+01 2022-03-09 00:20:17.365808+01 3 3 \N [{"date": "2022-03-08T23:20:17.329Z", "type": "login-success"}] \N
348-
80e97a87-9d11-4487-a3c3-411bbdf42a2c 2022-03-17 17:25:53.809705+01 2022-03-23 22:07:14.867562+01 3 11 \N [{"date": "2022-03-17T16:33:43.464Z", "type": "login-success"}, {"date": "2022-03-23T21:07:14.852Z", "type": "login-success"}] \N
349-
e0bcefc6-f1be-4c83-ac9d-6ea47335a9c3 2021-03-15 16:53:55.740856+01 2025-03-06 00:34:34.261953+01 10 1 \N [{"date": "2025-03-05T23:34:34.260Z", "type": "login-error"}] \N
340+
7aef1d02-3021-4988-937c-f18fa6244b14 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 2 \N [] 1
341+
0068d982-390a-4c42-9b63-55f5c78c6cfd 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 5 \N [] 3
342+
e931e0d8-ecbb-478a-97a3-a01eac88e24f 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 4 \N [] 3
343+
3e2118c1-1e0b-4ce7-bc85-22001eebc8ee 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 7 \N [] 4
344+
8dddd469-7b63-44f3-8166-15830e80ee7b 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 6 \N [] 1
345+
759bdc26-be9d-4ffb-95c9-8c2d7a06a63b 2021-03-15 16:53:55.740856+01 2021-03-15 16:53:55.740856+01 1 8 \N [] 3
346+
d84e9cc2-5c52-4a02-880e-bcfb27180594 2021-03-15 16:53:55.74+01 2021-12-06 16:26:00.681872+01 2 10 \N [{"date": "2021-12-06T15:26:00.672Z", "type": "login-success"}] 3
347+
9fe998b5-7178-44b5-9c40-2e18ba233f1d 2021-03-15 16:53:55.740856+01 2022-03-09 00:20:17.365808+01 3 3 \N [{"date": "2022-03-08T23:20:17.329Z", "type": "login-success"}] 1
348+
80e97a87-9d11-4487-a3c3-411bbdf42a2c 2022-03-17 17:25:53.809705+01 2022-03-23 22:07:14.867562+01 3 11 \N [{"date": "2022-03-17T16:33:43.464Z", "type": "login-success"}, {"date": "2022-03-23T21:07:14.852Z", "type": "login-success"}] 5
349+
e0bcefc6-f1be-4c83-ac9d-6ea47335a9c3 2021-03-15 16:53:55.740856+01 2025-03-06 00:34:34.261953+01 10 1 \N [{"date": "2025-03-05T23:34:34.260Z", "type": "login-error"}] 1
350350
\.
351351
352352

_scripts/db/generate-ddl.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fi
4646

4747
# Nettoyer le fichier généré (enlever les SET et commentaires restants)
4848
echo "🧹 Nettoyage du fichier DDL..."
49-
sed -i \
49+
sed -i '' \
5050
-E \
5151
-e '/^[[:space:]]*--/d' \
5252
-e '/^[[:space:]]*SET /d' \
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { UserProfile } from "../common-user";
2+
3+
export interface UserSecurityLogError {
4+
operation: string;
5+
userId: number;
6+
userProfile?: UserProfile;
7+
}

packages/backend/src/_common/model/users/user-security/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
export * from "./UserSecurity.interface";
33
export * from "./UserSecurityEvent.interface";
44
export * from "./UserSecurityEventType.type";
5+
export * from "./UserSecurityLogError.interface";
56
export * from "./UserTokens.interface";
67
export * from "./UserTokenType.type";
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
import { domifaConfig } from "../config";
3+
4+
export class ManualMigration1754664216664 implements MigrationInterface {
5+
public async up(queryRunner: QueryRunner): Promise<void> {
6+
if (
7+
domifaConfig().envId === "prod" ||
8+
domifaConfig().envId === "preprod" ||
9+
domifaConfig().envId === "local"
10+
) {
11+
// Update user_structure_security table with structureId from user_structure
12+
await queryRunner.query(`
13+
UPDATE user_structure_security
14+
SET "structureId" = (
15+
SELECT us."structureId"
16+
FROM user_structure us
17+
WHERE us.id = user_structure_security."userId"
18+
)
19+
WHERE "structureId" IS NULL
20+
`);
21+
22+
// Update user_usager_security table with structureId from user_usager
23+
await queryRunner.query(`
24+
UPDATE user_usager_security
25+
SET "structureId" = (
26+
SELECT uu."structureId"
27+
FROM user_usager uu
28+
WHERE uu.id = user_usager_security."userId"
29+
)
30+
WHERE "structureId" IS NULL
31+
`);
32+
}
33+
}
34+
35+
public async down(queryRunner: QueryRunner): Promise<void> {
36+
// Set structureId values back to NULL
37+
await queryRunner.query(`
38+
UPDATE user_structure_security
39+
SET "structureId" = NULL
40+
`);
41+
42+
await queryRunner.query(`
43+
UPDATE user_usager_security
44+
SET "structureId" = NULL
45+
`);
46+
}
47+
}

packages/backend/src/modules/users/services/user-structure-creator.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ async function createUserWithPassword(
4040

4141
const userSecurityAttributes: UserSecurity = {
4242
userId: user.id,
43+
structureId: user.structureId,
4344
eventsHistory: [],
4445
temporaryTokens: undefined,
4546
};
@@ -62,6 +63,7 @@ async function createUserWithTmpToken(
6263

6364
const userSecurityAttributes: UserSecurity = {
6465
userId: user.id,
66+
structureId: user.structureId,
6567
temporaryTokens:
6668
userSecurityResetPasswordInitiator.generateResetPasswordTokenAndValidity({
6769
type: "create-user",

packages/backend/src/modules/users/services/userSecurityEventHistoryManager.service.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
UserSecurityEventType,
77
UserSecurityEvent,
88
UserProfile,
9+
UserSecurityLogError,
910
} from "../../../_common/model";
1011

1112
export const SECURITY_HISTORY_MAX_EVENTS_ATTEMPT = 5;
@@ -108,21 +109,7 @@ export function isAccountLockedForOperation({
108109
return false;
109110
}
110111

111-
function logOperationError({
112-
operation,
113-
userId,
114-
userProfile,
115-
}: {
116-
operation: string;
117-
userId: number;
118-
userProfile?: UserProfile;
119-
}) {
120-
const context = {
121-
operation,
122-
userId,
123-
userProfile,
124-
};
125-
112+
function logOperationError(context: UserSecurityLogError) {
126113
if (domifaConfig().envId === "dev" || domifaConfig().envId === "local") {
127114
appLogger.warn(
128115
"Operation forbidden due to excessive recent security events",

0 commit comments

Comments
 (0)