From d07aa01ea1c9d64399cfd1fee7a456c8028ebcb7 Mon Sep 17 00:00:00 2001
From: Jeremie BRISON <ext.sopra.jbrison@grandlyon.com>
Date: Fri, 8 Jan 2021 15:28:37 +0100
Subject: [PATCH] feat(admin) : add guard + service

---
 src/app/app-routing.module.ts               |  7 +++++++
 src/app/app.module.ts                       |  5 ++++-
 src/app/guards/admin.guard.ts               | 22 +++++++++++++++++++++
 src/app/profile/profile.component.html      |  3 ++-
 src/app/profile/services/profile.service.ts |  7 +++++++
 5 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 src/app/guards/admin.guard.ts

diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts
index efacd3c84..1d6b6760a 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 b412fc8ac..6f992a733 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 000000000..1a5533252
--- /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 0216130a4..37ec8af99 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 78049a5f1..38679bbba 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 });
   }
-- 
GitLab