Vérification des droits d'admin pour ajouter

avions/dz/type de sauts
This commit is contained in:
Sébastien André
2021-03-17 17:21:23 +01:00
parent ba268e739f
commit 76d3943151
15 changed files with 76 additions and 100 deletions

View File

@@ -18,14 +18,10 @@ export class AppComponent implements OnInit {
public currentUser: User; public currentUser: User;
public version: string; public version: string;
constructor( constructor(private router: Router,
private router: Router,
private authenticationService: AuthenticationService, private authenticationService: AuthenticationService,
private serviceComm: ServiceComm private serviceComm: ServiceComm) {
) { this.authenticationService.currentUser.subscribe(user => { this.currentUser = user; });
this.authenticationService.currentUser.subscribe(
x => (this.currentUser = x)
);
ConfigurationHelper.settings.subscribe(settings => ConfigurationHelper.settings.subscribe(settings =>
{ {

View File

@@ -68,42 +68,42 @@ const appRoutes: Routes = [
{ {
path: "summary", path: "summary",
component: SummaryComponent, component: SummaryComponent,
canActivate: [AuthGuardService], canActivate: [AuthGuardService]
}, },
{ {
path: "jumps", path: "jumps",
component: ListOfJumpsComponent, component: ListOfJumpsComponent,
canActivate: [AuthGuardService], canActivate: [AuthGuardService]
}, },
{ {
path: "dzs", path: "dzs",
component: ListOfDzsComponent, component: ListOfDzsComponent,
canActivate: [AuthGuardService], canActivate: [AuthGuardService]
}, },
{ {
path: "newjump", path: "newjump",
component: NewJumpComponent, component: NewJumpComponent,
canActivate: [AuthGuardService], canActivate: [AuthGuardService]
}, },
{ {
path: "aircrafts", path: "aircrafts",
component: ListOfAircraftsComponent, component: ListOfAircraftsComponent,
canActivate: [AuthGuardService], canActivate: [AuthGuardService]
}, },
{ {
path: "jumpTypes", path: "jumpTypes",
component: ListOfJumpTypesComponent, component: ListOfJumpTypesComponent,
canActivate: [AuthGuardService], canActivate: [AuthGuardService]
}, },
{ {
path: "gears", path: "gears",
component: ListOfGearsComponent, component: ListOfGearsComponent,
canActivate: [AuthGuardService], canActivate: [AuthGuardService]
}, },
{ {
path: "user", path: "user",
component: UserProfileComponent, component: UserProfileComponent,
canActivate: [AuthGuardService], canActivate: [AuthGuardService]
}, },
{ path: "login", component: LoginComponent }, { path: "login", component: LoginComponent },

View File

@@ -58,7 +58,6 @@ export class CreateUserComponent implements OnInit {
this.invalidForm = false; this.invalidForm = false;
this.submitted = true; this.submitted = true;
// stop here if form is invalid
if (this.createForm.invalid) { if (this.createForm.invalid) {
this.invalidForm = true; this.invalidForm = true;
return; return;

View File

@@ -1,6 +1,6 @@
<div class="content"> <div class="content">
<div *ngIf="dataSourceTable != null else loading"> <div *ngIf="dataSourceTable != null else loading">
<button mat-raised-button color="accent" (click)="openDialogToAdd()">Add a aircraft</button> <button mat-raised-button color="accent" (click)="openDialogToAdd()" *ngIf="isUserAdmin == true">Add a aircraft</button>
<table mat-table [dataSource]="dataSourceTable"> <table mat-table [dataSource]="dataSourceTable">
<ng-container matColumnDef="id"> <ng-container matColumnDef="id">

View File

@@ -3,11 +3,12 @@ import { MatPaginator } from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table'; import { MatTableDataSource } from '@angular/material/table';
import { MatDialog } from '@angular/material/dialog'; import { MatDialog } from '@angular/material/dialog';
import { AircraftResp } from '../../models/aircraft';
import { AircraftService } from '../../services/aircraft.service'; import { AircraftService } from '../../services/aircraft.service';
import { ServiceComm } from '../../services/service-comm.service'; import { ServiceComm } from '../../services/service-comm.service';
import { AuthenticationService } from '../../services/authentication.service';
import { NewAircraftComponent } from '../new-aircraft/new-aircraft.component'; import { NewAircraftComponent } from '../new-aircraft/new-aircraft.component';
import { AddAction } from '../../models/add-action.enum'; import { AddAction } from '../../models/add-action.enum';
import { AircraftResp } from '../../models/aircraft';
@Component({ @Component({
selector: 'app-list-of-aircrafts', selector: 'app-list-of-aircrafts',
@@ -18,13 +19,15 @@ export class ListOfAircraftsComponent implements OnInit {
public displayedColumns: Array<string> = ['id', 'name', 'imageData']; public displayedColumns: Array<string> = ['id', 'name', 'imageData'];
public dataSourceTable: MatTableDataSource<AircraftResp>; public dataSourceTable: MatTableDataSource<AircraftResp>;
public resultsLength = 0; public resultsLength = 0;
public isUserAdmin: boolean;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator; @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
constructor( constructor(private serviceApi: AircraftService,
private serviceApi: AircraftService,
private serviceComm: ServiceComm, private serviceComm: ServiceComm,
public dialog: MatDialog private authenticationService: AuthenticationService,
) { } public dialog: MatDialog) {
this.isUserAdmin = this.authenticationService.currentUserValue.IsAdmin;
}
ngOnInit() { ngOnInit() {
this.serviceComm.refreshRequest.subscribe(action => { this.serviceComm.refreshRequest.subscribe(action => {

View File

@@ -1,6 +1,6 @@
<div class="content"> <div class="content">
<div *ngIf="dataSourceTable != null else loading"> <div *ngIf="dataSourceTable != null else loading">
<button mat-raised-button color="accent" (click)="openDialogToAdd()">Add a drop zone</button> <button mat-raised-button color="accent" (click)="openDialogToAdd()" *ngIf="isUserAdmin == true">Add a drop zone</button>
<table mat-table [dataSource]="dataSourceTable"> <table mat-table [dataSource]="dataSourceTable">
<ng-container matColumnDef="isfavorite"> <ng-container matColumnDef="isfavorite">

View File

@@ -7,6 +7,7 @@ import { AddAction } from '../../models/add-action.enum';
import { DropZoneResp } from '../../models/dropzone'; import { DropZoneResp } from '../../models/dropzone';
import { DropzoneService } from '../../services/dropzone.service'; import { DropzoneService } from '../../services/dropzone.service';
import { ServiceComm } from '../../services/service-comm.service'; import { ServiceComm } from '../../services/service-comm.service';
import { AuthenticationService } from '../../services/authentication.service';
import { NewDropZoneComponent } from '../new-drop-zone/new-drop-zone.component'; import { NewDropZoneComponent } from '../new-drop-zone/new-drop-zone.component';
@Component({ @Component({
@@ -23,14 +24,16 @@ export class ListOfDzsComponent implements OnInit {
'type', 'type',
]; ];
public dataSourceTable: MatTableDataSource<DropZoneResp>; public dataSourceTable: MatTableDataSource<DropZoneResp>;
public isUserAdmin: boolean;
public resultsLength = 0; public resultsLength = 0;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator; @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
constructor( constructor(private serviceApi: DropzoneService,
private serviceApi: DropzoneService,
private serviceComm: ServiceComm, private serviceComm: ServiceComm,
public dialog: MatDialog private authenticationService: AuthenticationService,
) { } public dialog: MatDialog) {
this.isUserAdmin = this.authenticationService.currentUserValue.IsAdmin;
}
ngOnInit() { ngOnInit() {
this.serviceComm.refreshRequest.subscribe((action) => { this.serviceComm.refreshRequest.subscribe((action) => {

View File

@@ -1,6 +1,6 @@
<div class="content"> <div class="content">
<div *ngIf="dataSourceTable != null else loading"> <div *ngIf="dataSourceTable != null else loading">
<button mat-raised-button color="accent" (click)="openDialogToAdd()">Add a jump type</button> <button mat-raised-button color="accent" (click)="openDialogToAdd()" *ngIf="isUserAdmin == true">Add a jump type</button>
<table mat-table [dataSource]="dataSourceTable"> <table mat-table [dataSource]="dataSourceTable">
<ng-container matColumnDef="id"> <ng-container matColumnDef="id">

View File

@@ -7,6 +7,7 @@ import { AddAction } from "../../models/add-action.enum";
import { JumpTypeResp } from "../../models/jumpType"; import { JumpTypeResp } from "../../models/jumpType";
import { JumpTypeService } from "../../services/jump-type.service"; import { JumpTypeService } from "../../services/jump-type.service";
import { ServiceComm } from "../../services/service-comm.service"; import { ServiceComm } from "../../services/service-comm.service";
import { AuthenticationService } from '../../services/authentication.service';
import { NewJumpTypeComponent } from "../new-jump-type/new-jump-type.component"; import { NewJumpTypeComponent } from "../new-jump-type/new-jump-type.component";
@Component({ @Component({
@@ -17,14 +18,16 @@ import { NewJumpTypeComponent } from "../new-jump-type/new-jump-type.component";
export class ListOfJumpTypesComponent implements OnInit { export class ListOfJumpTypesComponent implements OnInit {
public displayedColumns: Array<string> = ["id", "name"]; public displayedColumns: Array<string> = ["id", "name"];
public dataSourceTable: MatTableDataSource<JumpTypeResp>; public dataSourceTable: MatTableDataSource<JumpTypeResp>;
public isUserAdmin: boolean;
public resultsLength = 0; public resultsLength = 0;
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator; @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
constructor( constructor(private serviceApi: JumpTypeService,
private serviceApi: JumpTypeService,
private serviceComm: ServiceComm, private serviceComm: ServiceComm,
public dialog: MatDialog private authenticationService: AuthenticationService,
) {} public dialog: MatDialog) {
this.isUserAdmin = this.authenticationService.currentUserValue.IsAdmin;
}
ngOnInit() { ngOnInit() {
this.serviceComm.refreshRequest.subscribe(action => { this.serviceComm.refreshRequest.subscribe(action => {

View File

@@ -43,7 +43,7 @@ export class LoginUserComponent implements OnInit, AfterViewInit {
password: [ password: [
'', '',
[ [
Validators.required /*, Validators.pattern("^[A-Za-z0-9_-]{8,15}$")*/ Validators.required
] ]
] ]
}, },

View File

@@ -85,10 +85,8 @@ export class StatsByYearResp {
} }
export class StatsForLastYearResp { export class StatsForLastYearResp {
constructor( constructor(dataByDz: Array<StatsByDzResp>,
dataByDz: Array<StatsByDzResp>, dataByJumpType: Array<StatsByJumpTypeResp>) {
dataByJumpType: Array<StatsByJumpTypeResp>
) {
this.byDz = new Array<StatsByDzResp>(); this.byDz = new Array<StatsByDzResp>();
this.byJumpType = new Array<StatsByJumpTypeResp>(); this.byJumpType = new Array<StatsByJumpTypeResp>();
@@ -101,10 +99,8 @@ export class StatsForLastYearResp {
} }
export class StatsForLastMonthResp { export class StatsForLastMonthResp {
constructor( constructor(dataByDz: Array<StatsByDzResp>,
dataByDz: Array<StatsByDzResp>, dataByJumpType: Array<StatsByJumpTypeResp>) {
dataByJumpType: Array<StatsByJumpTypeResp>
) {
this.byDz = new Array<StatsByDzResp>(); this.byDz = new Array<StatsByDzResp>();
this.byJumpType = new Array<StatsByJumpTypeResp>(); this.byJumpType = new Array<StatsByJumpTypeResp>();

View File

@@ -5,6 +5,9 @@ export class User {
firstName: string; firstName: string;
lastName: string; lastName: string;
email: string; email: string;
roles: string;
authdata?: string; authdata?: string;
token?: string; token?: string;
get IsAdmin() { return this.roles === "admin"; }
} }

View File

@@ -1,23 +1,14 @@
import { Injectable } from "@angular/core"; import { Injectable } from "@angular/core";
import { import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router";
CanActivate,
Router,
ActivatedRouteSnapshot,
RouterStateSnapshot
} from "@angular/router";
import { AuthenticationService } from "./authentication.service"; import { AuthenticationService } from "./authentication.service";
@Injectable({ providedIn: "root" }) @Injectable({ providedIn: "root" })
export class AuthGuardService implements CanActivate { export class AuthGuardService implements CanActivate {
constructor( constructor(private router: Router,
private router: Router, private authenticationService: AuthenticationService) {}
private authenticationService: AuthenticationService
) {}
public canActivate( public canActivate(route: ActivatedRouteSnapshot,
route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
state: RouterStateSnapshot
) {
const currentUser = this.authenticationService.currentUserValue; const currentUser = this.authenticationService.currentUserValue;
if (currentUser) { if (currentUser) {
// logged in so return true // logged in so return true
@@ -25,10 +16,9 @@ export class AuthGuardService implements CanActivate {
} }
// not logged in so redirect to login page with the return url // not logged in so redirect to login page with the return url
this.router.navigate(["/login"], { this.router.navigate(["/login"],
skipLocationChange: true, {skipLocationChange: true, queryParams: { returnUrl: state.url }});
queryParams: { returnUrl: state.url }
});
return false; return false;
} }
} }

View File

@@ -34,37 +34,32 @@ export class AuthenticationService extends BaseService {
password: password password: password
}; };
return this.http return this.http.post<User>(`${this.apiUrl}/User/Authenticate`,
.post<User>(`${this.apiUrl}/User/Authenticate`, bodyLogin, { bodyLogin,
headers: this.headers { headers: this.headers })
}) .pipe(map(user => {
.pipe(
map(user => {
this.pushUserToken(username, password, user); this.pushUserToken(username, password, user);
return user; return user;
}) }));
);
} }
public create(newUser: User) { public create(newUser: User) {
return this.http return this.http.post<User>(`${this.apiUrl}/User`,
.post<any>(`${this.apiUrl}/User`, newUser, { newUser,
headers: this.headers { headers: this.headers })
}) .pipe(map(user => {
.pipe(
map(user => {
this.pushUserToken(newUser.login, newUser.password, user); this.pushUserToken(newUser.login, newUser.password, user);
return user; return user;
}) }));
);
} }
private pushUserToken(login: string, password: string, user: any){ private pushUserToken(login: string, password: string, user: User){
// store user details and basic auth credentials in local storage to keep user logged in between page refreshes if (user && user.token) {
user.authdata = window.btoa(login + ":" + password); user.authdata = window.btoa(login + ":" + password);
localStorage.setItem("currentUser", JSON.stringify(user)); localStorage.setItem("currentUser", JSON.stringify(user));
this.currentUserSubject.next(user); this.currentUserSubject.next(user);
} }
}
private alwaysLogin() { private alwaysLogin() {
return this.http.get(`${this.apiUrl}/User/AlwayLogin`, { return this.http.get(`${this.apiUrl}/User/AlwayLogin`, {
@@ -73,7 +68,6 @@ export class AuthenticationService extends BaseService {
} }
logout() { logout() {
// remove user from local storage to log user out
localStorage.removeItem("currentUser"); localStorage.removeItem("currentUser");
this.currentUserSubject.next(null); this.currentUserSubject.next(null);
} }

View File

@@ -18,16 +18,5 @@ export class BaseService {
this.apiUrl = tmpApiUrl + '/api'; this.apiUrl = tmpApiUrl + '/api';
} }
}); });
// const config: ConfigurationHelper = injector.get(ConfigurationHelper);
// config.load(environment.env)
// .then(() => {
// let tmpApiUrl : string = ConfigurationHelper.settings.apiUrl;
// this.headers = new HttpHeaders({
// 'Access-Control-Allow-Origin': tmpApiUrl
// });
// this.apiUrl = tmpApiUrl + '/api';
// });
} }
} }