This commit is contained in:
Yûki VACHOT 2022-01-31 16:15:40 +01:00
parent bc62ef4f0d
commit a18333c2c3
101 changed files with 54 additions and 59 deletions

View file

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { AdminGuard } from './admin.guard';
describe('AdminGuard', () => {
let guard: AdminGuard;
beforeEach(() => {
TestBed.configureTestingModule({});
guard = TestBed.inject(AdminGuard);
});
it('should be created', () => {
expect(guard).toBeTruthy();
});
});

View file

@ -0,0 +1,31 @@
import { Injectable } from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import { Observable } from 'rxjs';
import {ProfilService} from "../../services/profil/profil.service";
@Injectable({
providedIn: 'root'
})
export class AdminGuard implements CanActivate
{
constructor(private profilService: ProfilService, private router: Router) {}
canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
{
if(this.profilService.getId() === -1) // si non connecté
{
this.router.navigateByUrl("/login");
return false;
}
else {
if(this.profilService.isAdmin()) return true;
else {
this.router.navigateByUrl("/login");
return false;
}
}
}
}

View file

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { UserGuard } from './user.guard';
describe('UserGuard', () => {
let guard: UserGuard;
beforeEach(() => {
TestBed.configureTestingModule({});
guard = TestBed.inject(UserGuard);
});
it('should be created', () => {
expect(guard).toBeTruthy();
});
});

View file

@ -0,0 +1,31 @@
import { Injectable } from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
import { Observable } from 'rxjs';
import {ProfilService} from "../../services/profil/profil.service";
@Injectable({
providedIn: 'root'
})
export class UserGuard implements CanActivate
{
constructor(private profilService: ProfilService, private router: Router) {}
canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree
{
if(this.profilService.getId() === -1) // si non connecté
{
this.router.navigateByUrl("/login");
return false;
}
else {
if(!this.profilService.isAdmin()) return true;
else {
this.router.navigateByUrl("/login");
return false;
}
}
}
}