diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index efacd3c848b600e4743d252025c6d9bee9c3fe48..1d6b6760ad7989923bc0cf9a5f89d37c0e70199e 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -1,6 +1,8 @@ import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { AboutComponent } from './about/about.component'; +import { PanelComponent } from './admin/panel/panel.component'; +import { AdminGuard } from './guards/admin.guard'; import { AuthGuard } from './guards/auth.guard'; import { HomeComponent } from './home/home.component'; import { LegalNoticeComponent } from './legal-notice/legal-notice.component'; @@ -58,6 +60,11 @@ const routes: Routes = [ path: 'reset-password', component: ResetPasswordComponent, }, + { + path: 'admin', + canActivate: [AdminGuard], + component: PanelComponent, + }, { path: '**', redirectTo: 'home', diff --git a/src/app/app.module.ts b/src/app/app.module.ts index b412fc8acc7babcee6a1dff913ead669fb3e960e..6f992a7337d57f46b9f2d345f280099e96471490 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -27,6 +27,8 @@ import { CustomHttpInterceptor } from './config/http-interceptor'; import { ProfileModule } from './profile/profile.module'; import { ResetEmailComponent } from './reset-email/reset-email.component'; import { ResetPasswordComponent } from './reset-password/reset-password.component'; +import { AdminModule } from './admin/admin.module'; +import { AdminGuard } from './guards/admin.guard'; @NgModule({ declarations: [ @@ -47,12 +49,13 @@ import { ResetPasswordComponent } from './reset-password/reset-password.componen ResetEmailComponent, ResetPasswordComponent, ], - imports: [BrowserModule, HttpClientModule, AppRoutingModule, SharedModule, MapModule, ProfileModule], + imports: [BrowserModule, HttpClientModule, AppRoutingModule, SharedModule, MapModule, ProfileModule, AdminModule], providers: [ { provide: LOCALE_ID, useValue: 'fr' }, { provide: HTTP_INTERCEPTORS, useClass: CustomHttpInterceptor, multi: true }, CustomBreakPointsProvider, AuthGuard, + AdminGuard, ], bootstrap: [AppComponent], }) diff --git a/src/app/guards/admin.guard.ts b/src/app/guards/admin.guard.ts new file mode 100644 index 0000000000000000000000000000000000000000..1a5533252f784e855050e8d112871ff7c29455c4 --- /dev/null +++ b/src/app/guards/admin.guard.ts @@ -0,0 +1,22 @@ +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; +import { Injectable } from '@angular/core'; +import { AuthService } from '../services/auth.service'; +import { ProfileService } from '../profile/services/profile.service'; + +/** + * Guard to assert that we are logged in admin. Otherwise redirect to home + */ +@Injectable() +export class AdminGuard implements CanActivate { + constructor(private authService: AuthService, private router: Router, private profileService: ProfileService) {} + + canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): UrlTree | boolean { + if (this.authService.isLoggedIn()) { + if (this.profileService.isAdmin()) { + return true; + } + return this.router.parseUrl('/profile'); + } + return this.router.parseUrl('/home'); + } +} diff --git a/src/app/profile/profile.component.html b/src/app/profile/profile.component.html index 0216130a4cb92db883e763248ed71fa004b0525c..37ec8af994c57c2315f3a3c5bf3d256c42da786c 100644 --- a/src/app/profile/profile.component.html +++ b/src/app/profile/profile.component.html @@ -7,9 +7,10 @@ <p> Mes structures : <span *ngFor="let structureId of userProfile.structuresLink"> - <b>{{ structureId }}</b> + <strong>{{ structureId }}</strong> </span> </p> + <button routerLink="/admin">Accèder au panel d'administration</button> <button (click)="toogleAddStructure()">Ajouter une structure</button> <button (click)="toogleChangeEmail()">Changer d'email</button> <form diff --git a/src/app/profile/services/profile.service.ts b/src/app/profile/services/profile.service.ts index 78049a5f10c0a161562a6918fed5426385aebcf9..38679bbba4742c90eabf6c83da8c0f592839d5b8 100644 --- a/src/app/profile/services/profile.service.ts +++ b/src/app/profile/services/profile.service.ts @@ -40,6 +40,13 @@ export class ProfileService { return this.http.post<any>(`${this.baseUrl}`, body); } + public isAdmin(): boolean { + if (this.currentProfile) { + return this.currentProfile.role == 1; + } + return false; + } + public changePassword(newPassword: string, oldPassword: string): Observable<User> { return this.http.post<any>(`${this.baseUrl}/change-password`, { newPassword, oldPassword }); }