From ecc8a61102ef1e68c6bcfe609135d66388ec9a7e Mon Sep 17 00:00:00 2001 From: FlayInAHook Date: Tue, 1 Dec 2020 21:14:51 +0100 Subject: [PATCH] Added basic auth guard --- src/app/app-routing.module.ts | 19 ++++++++------- src/app/helper/auth.guard.ts | 30 +++++++++++++++++++++++ src/app/services/role.service.ts | 41 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 src/app/helper/auth.guard.ts create mode 100644 src/app/services/role.service.ts diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 55b8e96..946fdb0 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -10,18 +10,19 @@ import { EquipmentComponent } from './pages/tables/equipment/equipment.component import { LendingStationsComponent } from './pages/tables/lending-stations/lending-stations.component'; import { ParticipantsComponent } from './pages/tables/participants/participants.component'; import { TimeFramesComponent } from './pages/tables/time-frames/time-frames.component'; +import {AuthGuard} from './helper/auth.guard'; const routes: Routes = [ { path: 'login', component: LoginComponent }, - { path: 'tableOverview', component: TableOverviewComponent }, - { path: 'table/bikes', component: BikesComponent }, - { path: 'bike/:id', component: BikeComponent }, - { path: 'table/participants', component: ParticipantsComponent }, - { path: 'table/lendingStations', component: LendingStationsComponent }, - { path: 'table/equipmentTypes', component: EquipmentTypesComponent }, - { path: 'table/engagementTypes', component: EngagementTypesComponent }, - { path: 'table/equipment', component: EquipmentComponent }, - { path: 'table/timeFrames', component: TimeFramesComponent }, + { path: 'tableOverview', component: TableOverviewComponent, canActivate: [AuthGuard]}, + { path: 'table/bikes', component: BikesComponent, canActivate: [AuthGuard] }, + { path: 'bike/:id', component: BikeComponent, canActivate: [AuthGuard] }, + { path: 'table/participants', component: ParticipantsComponent, canActivate: [AuthGuard] }, + { path: 'table/lendingStations', component: LendingStationsComponent, canActivate: [AuthGuard] }, + { path: 'table/equipmentTypes', component: EquipmentTypesComponent, canActivate: [AuthGuard] }, + { path: 'table/engagementTypes', component: EngagementTypesComponent, canActivate: [AuthGuard] }, + { path: 'table/equipment', component: EquipmentComponent, canActivate: [AuthGuard] }, + { path: 'table/timeFrames', component: TimeFramesComponent, canActivate: [AuthGuard] }, { path: '', redirectTo: 'tableOverview', pathMatch: 'full' }, { path: 'table', redirectTo: 'tableOverview', pathMatch: 'full' }, { path: '**', redirectTo: 'tableOverview' }, diff --git a/src/app/helper/auth.guard.ts b/src/app/helper/auth.guard.ts new file mode 100644 index 0000000..a05beec --- /dev/null +++ b/src/app/helper/auth.guard.ts @@ -0,0 +1,30 @@ +import { Injectable } from "@angular/core"; +import { + Router, + CanActivate, + ActivatedRouteSnapshot, + RouterStateSnapshot +} from "@angular/router"; +import { AuthUser } from '../models/user'; + +import { AuthService } from "../services/auth.service"; + +@Injectable({ providedIn: "root" }) +export class AuthGuard implements CanActivate { + constructor( + private router: Router, + private authService: AuthService + ) {} + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + const currentUser = this.authService.getCurrentUserValue; + if (Object.keys(currentUser).length != 0) { + // authorised so return true + return true; + } + + // not logged in so redirect to login page with the return url + this.router.navigate(["/login"], { queryParams: { returnUrl: state.url } }); + return false; + } +} \ No newline at end of file diff --git a/src/app/services/role.service.ts b/src/app/services/role.service.ts new file mode 100644 index 0000000..ce96cb4 --- /dev/null +++ b/src/app/services/role.service.ts @@ -0,0 +1,41 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { catchError, finalize, map, tap } from 'rxjs/operators'; +import { environment } from '../../environments/environment'; +import { Observable, BehaviorSubject, of } from 'rxjs'; +import { User } from "../models/user"; +import { AuthService} from "./auth.service"; +import { ObserveOnSubscriber } from 'rxjs/internal/operators/observeOn'; + + +@Injectable({ + providedIn: 'root', +}) +export class RoleService { + + constructor(private http: HttpClient, private authService: AuthService) { + + } + + + public getAllUsers(): Observable { + return this.http.get(`${environment.authUrl}/users`); + } + + public getUser(email: string): Observable { + return this.http.get(`${environment.authUrl}/users/${email}`); + } + + public getUserPermissions(email: string): Observable { + return this.http.get(`${environment.authUrl}/users/${email}/permissions`) + } + + public updateUser(user: User): Observable { + return this.http.post(`${environment.authUrl}/users/${user.email}/update`, user); + } + + public deleteUser(email: string): Observable { + return this.http.delete(`${environment.authUrl}/users/` + email + "/delete"); + } + +}