Skip to content

Commit f4481f5

Browse files
committed
fix(frontend): update of guards
1 parent 2e46f81 commit f4481f5

File tree

20 files changed

+104
-189
lines changed

20 files changed

+104
-189
lines changed

packages/backend/src/_migrations/1756117243336-manual-migration.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

packages/backend/src/_migrations/1756117243337-manual-migration.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/backend/src/_migrations/1757532174131-auto-migration.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

packages/backend/src/modules/users/controllers/users.public.controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const userProfile: UserProfile = "structure";
2525
@Controller("users")
2626
@ApiTags("users")
2727
export class UsersPublicController {
28-
// TODO: add a limit for this endpoint by ip
2928
@Post("validate-email")
3029
public async validateEmail(
3130
@Body() emailDto: EmailDto,

packages/backend/src/usagers/controllers/usagers-decision.controller.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ export class UsagersDecisionController {
6060
@Param("usagerRef", new ParseIntPipe()) _usagerRef: number
6161
): Promise<Usager> {
6262
if (
63-
decision.statut !== "ATTENTE_DECISION" &&
64-
decision.statut !== "INSTRUCTION" &&
65-
user.role !== "responsable" &&
66-
user.role !== "admin"
63+
!["ATTENTE_DECISION", "INSTRUCTION"].includes(decision.statut) &&
64+
!["reposable", "admmin"].includes(user.role)
6765
) {
6866
throw new Error("CANNOT_SET_DECISION");
6967
}

packages/frontend/src/app/guards/auth.guard.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ export class AuthGuard {
2424

2525
return this.authService.isAuth().pipe(
2626
map((isAuth: boolean) => {
27-
console.log("XXXX");
2827
if (!isAuth) {
29-
this.authService.logoutAndRedirect(state);
28+
this.authService.logout(state);
3029
return false;
3130
}
3231

@@ -35,7 +34,6 @@ export class AuthGuard {
3534
}
3635

3736
if (this.authService.currentUserValue !== null) {
38-
console.log(this.authService.currentUserValue);
3937
const userRole = this.authService.currentUserValue.role;
4038

4139
if (allowedRoles.includes(userRole)) {
@@ -51,7 +49,7 @@ export class AuthGuard {
5149
return false;
5250
}),
5351
catchError(() => {
54-
this.authService.logoutAndRedirect(state);
52+
this.authService.logout(state);
5553
return of(false);
5654
})
5755
);

packages/frontend/src/app/interceptors/server-error.interceptor.ts

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { Injectable, Injector } from "@angular/core";
1111
import { Observable, throwError, timer } from "rxjs";
1212
import { catchError, retry } from "rxjs/operators";
1313
import { AuthService } from "../modules/shared/services/auth.service";
14-
import { getCurrentScope } from "@sentry/angular";
1514
import { CustomToastService } from "../modules/shared/services";
1615

1716
const MAX_RETRIES = 2;
@@ -28,77 +27,70 @@ export class ServerErrorInterceptor implements HttpInterceptor {
2827
request: HttpRequest<any>,
2928
next: HttpHandler
3029
): Observable<HttpEvent<any>> {
31-
const authService = this.injector.get(AuthService);
32-
const toastr = this.injector.get(CustomToastService);
33-
34-
if (authService?.currentUserValue) {
35-
const user = authService.currentUserValue;
36-
getCurrentScope().setTag("structure", user?.structureId?.toString());
37-
getCurrentScope().setUser({
38-
email: user.email,
39-
username: `STRUCTURE ${user?.structureId?.toString()} : ${
40-
user?.prenom
41-
}`,
42-
});
43-
}
44-
4530
return next.handle(request).pipe(
4631
retry({
4732
count: MAX_RETRIES,
48-
delay: (error, retryCount) => {
49-
if (this.isRetryable(error)) {
50-
console.log(error);
51-
console.log(`Tentative de nouvelle requête ${retryCount}`);
33+
delay: (error: HttpErrorResponse, retryCount: number) => {
34+
if (this.shouldRetry(error)) {
35+
console.warn(
36+
`Retry attempt ${retryCount} for ${request.url}`,
37+
error
38+
);
5239
return timer(RETRY_DELAY);
5340
}
54-
return throwError(() => error);
41+
throw error; // Pas de retry, on passe au catchError
5542
},
5643
}),
57-
catchError((error: HttpErrorResponse) => {
58-
if (error.error instanceof ErrorEvent) {
59-
if (!navigator.onLine) {
60-
toastr.error(
61-
"Vous êtes actuellement hors-ligne. Veuillez vérifier votre connexion internet"
62-
);
63-
return throwError(() => "NAVIGATOR_OFFLINE");
64-
}
65-
return throwError(() => error.error);
66-
} else if (error instanceof HttpErrorResponse) {
67-
if (error.status === 0) {
68-
console.warn("Erreur de connexion:", error.message);
69-
toastr.error(
70-
"Problème de connexion au serveur. Veuillez réessayer plus tard."
71-
);
72-
}
73-
if (error.status === 401) {
74-
authService.logoutAndRedirect(undefined, true);
75-
toastr.error(
76-
"Votre session a expiré, merci de vous connecter à nouveau"
77-
);
78-
}
79-
} else {
80-
toastr.error(
81-
"Une erreur serveur est survenue. Nos équipes ont été notifiées."
82-
);
83-
}
84-
this.logError(request, error);
85-
return throwError(() => error);
86-
})
44+
catchError((error: HttpErrorResponse) => this.handleError(error))
8745
);
8846
}
8947

90-
private isRetryable(error: HttpErrorResponse): boolean {
91-
return !error.status || ERROR_STATUS_CODES_TO_RETRY.includes(error.status);
48+
private shouldRetry(error: HttpErrorResponse): boolean {
49+
if (error.status >= 400 && error.status < 500) {
50+
return false;
51+
}
52+
return ERROR_STATUS_CODES_TO_RETRY.includes(error.status);
53+
}
54+
55+
private handleError(error: HttpErrorResponse): Observable<never> {
56+
const authService = this.injector.get(AuthService);
57+
const toastr = this.injector.get(CustomToastService);
58+
59+
if (error.error instanceof ErrorEvent) {
60+
if (!navigator.onLine) {
61+
toastr.error(
62+
"Vous êtes actuellement hors-ligne. Veuillez vérifier votre connexion internet"
63+
);
64+
return throwError(() => new Error("NAVIGATOR_OFFLINE"));
65+
}
66+
toastr.error("Erreur de connexion réseau");
67+
return throwError(() => error);
68+
}
69+
70+
if (error.status === 0) {
71+
toastr.error(
72+
"Problème de connexion au serveur. Veuillez réessayer plus tard."
73+
);
74+
} else if (error.status === 401) {
75+
authService.logout(undefined, true);
76+
} else if (error.status === 403) {
77+
toastr.error("Vous n'avez pas les droits pour effectuer cette action");
78+
} else if (error.status >= 500 && error.status <= 504) {
79+
toastr.error(
80+
"Une erreur serveur est survenue. Nos équipes ont été notifiées."
81+
);
82+
}
83+
84+
this.logError(error);
85+
return throwError(() => error);
9286
}
9387

94-
private logError(request: HttpRequest<any>, error: HttpErrorResponse): void {
95-
console.warn(error.message, {
88+
private logError(error: HttpErrorResponse): void {
89+
console.error("HTTP Error:", {
9690
status: error.status,
9791
statusText: error.statusText,
9892
url: error.url,
9993
message: error.message,
100-
error: error.error,
101-
request,
10294
});
10395
}
10496
}

packages/frontend/src/app/modules/general/components/static-modals/idle-manager/idle-manager.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class IdleManagerComponent implements OnInit, OnDestroy {
9393

9494
public logout(): void {
9595
this.closeModals();
96-
this.authService.logoutAndRedirect();
96+
this.authService.logout();
9797
}
9898

9999
public openIdleModal(): void {

packages/frontend/src/app/modules/shared/services/auth.service.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,20 @@ export class AuthService {
8181
);
8282
}
8383

84-
public logoutFromBackend = async () => {
84+
public logoutFromBackend = async (
85+
state?: RouterStateSnapshot,
86+
sessionExpired?: boolean
87+
) => {
8588
if (this.currentUserValue?.access_token) {
8689
await firstValueFrom(this.http.get(`${this.endPoint}/logout`));
8790
}
88-
this.logout();
91+
await this.logout(state, sessionExpired);
8992
};
9093

91-
public async logout(): Promise<void> {
94+
public async logout(
95+
state?: RouterStateSnapshot,
96+
sessionExpired?: boolean
97+
): Promise<void> {
9298
this.currentUserSubject.next(null);
9399
this.store.dispatch(usagerActions.clearCache());
94100
localStorage.removeItem("currentUser");
@@ -97,29 +103,24 @@ export class AuthService {
97103
getCurrentScope().setTag("structure", "none");
98104
getCurrentScope().setUser({});
99105

100-
this.router.navigate(["/connexion"]);
101-
}
102-
103-
public logoutAndRedirect(
104-
state?: RouterStateSnapshot,
105-
sessionExpired?: boolean
106-
): void {
107106
if (sessionExpired) {
108107
this.toastr.warning("Votre session a expiré, merci de vous reconnecter");
109108
}
110109

111-
this.logout();
110+
// Navigation avec query params si nécessaire
111+
if (state?.url) {
112+
const cleanPath = state.url.split("?")[0];
113+
const matomoParams = this.getMatomoParams();
114+
const queryParams: Record<string, string> = { ...matomoParams };
112115

113-
const cleanPath = state?.url?.split("?")[0] || "/";
114-
const matomoParams = this.getMatomoParams();
116+
if (cleanPath !== "/") {
117+
queryParams.returnUrl = cleanPath;
118+
}
115119

116-
const queryParams: Record<string, string> = { ...matomoParams };
117-
118-
if (cleanPath !== "/") {
119-
queryParams.returnUrl = cleanPath;
120+
this.router.navigate(["/connexion"], { queryParams });
121+
} else {
122+
this.router.navigate(["/connexion"]);
120123
}
121-
122-
this.router.navigate(["/connexion"], { queryParams });
123124
}
124125

125126
private getMatomoParams(): Record<string, string> {
@@ -136,11 +137,11 @@ export class AuthService {
136137
localStorage.setItem("currentUser", JSON.stringify(user));
137138
this.currentUserSubject.next(user);
138139

140+
// Configuration Sentry centralisée ici
139141
getCurrentScope().setTag("structure", user.structureId?.toString());
140142
getCurrentScope().setUser({
141143
email: user.email,
142-
username:
143-
"STRUCTURE " + user.structureId?.toString() + " : " + user.prenom,
144+
username: `STRUCTURE ${user.structureId?.toString()}`,
144145
});
145146
}
146147
}

packages/frontend/src/app/modules/usager-dossier/components/step-footer/step-footer.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ <h2 class="form-title">Commentaires privés</h2>
66
</div>
77

88
<app-delete-usager-menu
9-
*ngIf="usager && me?.role !== 'facteur' && me?.role !== 'agent'"
9+
*ngIf="displayDeleteButton"
1010
class="my-4"
1111
[usager]="usager"
1212
context="INSTRUCTION_FORM"

0 commit comments

Comments
 (0)