connexion avec le back

This commit is contained in:
MiharyR 2022-01-14 20:15:50 +01:00
parent 91e4bba793
commit 4620f0dff9
16 changed files with 178 additions and 110 deletions

View file

@ -77,6 +77,8 @@ export class PageUserListComponent implements AfterViewInit
this.snackBar.open( "Opération annulée", "", this.configSnackBar); this.snackBar.open( "Opération annulée", "", this.configSnackBar);
} }
else { else {
if(retour.data.is_admin) retour.data.role = "admin" ;
else retour.data.role = "utilisateur" ;
this.dataSource.data.push(retour.data); this.dataSource.data.push(retour.data);
this.dataSource.data = this.dataSource.data; this.dataSource.data = this.dataSource.data;
this.dataSource = this.dataSource; this.dataSource = this.dataSource;
@ -96,17 +98,17 @@ export class PageUserListComponent implements AfterViewInit
this.dialog this.dialog
.open(PopupUpdatePersonAdminComponent, config) .open(PopupUpdatePersonAdminComponent, config)
.afterClosed() .afterClosed()
.subscribe( retour => { .subscribe( is_admin => {
if((retour === null) || (retour === undefined)) if((is_admin === null) || (is_admin === undefined))
{ {
this.snackBar.open("Opération annulée", "", this.configSnackBar); this.snackBar.open("Opération annulée", "", this.configSnackBar);
} }
else { else {
const index = this.dataSource.data.findIndex( elt => (elt.id === personToUpdate.id)); const index = this.dataSource.data.findIndex(elt => (elt.id === personToUpdate.id));
this.dataSource.data.splice(index, 1, retour.data); this.dataSource.data[index].is_admin = is_admin;
this.dataSource.data = this.dataSource.data; if(is_admin) this.dataSource.data[index].role = "admin";
this.dataSource = this.dataSource; else this.dataSource.data[index].role = "utilisateur";
this.snackBar.open("L'utilisateur a bien été modifié ✔", "", this.configSnackBar); this.snackBar.open("L'utilisateur a bien été modifié ✔", "", this.configSnackBar);
} }
@ -135,7 +137,7 @@ export class PageUserListComponent implements AfterViewInit
} }
else if(retour.status === "error") else if(retour.status === "error")
{ {
this.snackBar.open(retour.message, "", this.configSnackBar); this.snackBar.open(retour.error.message, "", this.configSnackBar);
} }
else { else {
const index = this.dataSource.data.findIndex( elt => (elt.id === personToDelete.id)); const index = this.dataSource.data.findIndex( elt => (elt.id === personToDelete.id));

View file

@ -7,13 +7,13 @@
<!-- nickname --> <!-- nickname -->
<mat-form-field appearance="fill"> <mat-form-field appearance="fill">
<mat-label>Pseudo</mat-label> <mat-label>Pseudo</mat-label>
<input matInput type="text" [(ngModel)]="person.nickname" required> <input matInput type="text" [(ngModel)]="nickname" required>
</mat-form-field><br> </mat-form-field><br>
<!-- email --> <!-- email -->
<mat-form-field appearance="fill"> <mat-form-field appearance="fill">
<mat-label>Email</mat-label> <mat-label>Email</mat-label>
<input matInput type="text" [(ngModel)]="person.email" required> <input matInput type="text" [(ngModel)]="email" required>
</mat-form-field><br> </mat-form-field><br>
<!-- mot de passe --> <!-- mot de passe -->
@ -29,7 +29,7 @@
</mat-form-field><br> </mat-form-field><br>
<!-- role --> <!-- role -->
<mat-radio-group aria-labelledby="label-radio-group" [(ngModel)]="person.is_admin"> <mat-radio-group aria-labelledby="label-radio-group" [(ngModel)]="is_admin">
<mat-radio-button [value]="false" checked>Utilisateur &nbsp;</mat-radio-button> <mat-radio-button [value]="false" checked>Utilisateur &nbsp;</mat-radio-button>
<mat-radio-button [value]="true">Admin</mat-radio-button> <mat-radio-button [value]="true">Admin</mat-radio-button>
</mat-radio-group><br><br> </mat-radio-group><br><br>

View file

@ -1,7 +1,7 @@
import {Component, Inject} from '@angular/core'; import {Component, Inject} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog"; import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
import {CheckEmailService} from "../../../common/services/checkEmail/check-email.service"; import {CheckEmailService} from "../../../common/services/checkEmail/check-email.service";
import {HashageService} from "../../../common/services/hashage/hashage.service"; import {MessageService} from "../../../common/services/message/message.service";
@ -12,16 +12,12 @@ import {HashageService} from "../../../common/services/hashage/hashage.service";
}) })
export class PopupCreatePersonComponent export class PopupCreatePersonComponent
{ {
person = { nickname: string = "";
id: "", email: string = "";
nickname: "", is_admin: boolean = false;
email: "",
hash_pass: "",
is_admin: false,
};
password: string = ""; password: string = "";
confirmPassword: string = "" ; confirmPassword: string = "" ;
changePassword: boolean = false ;
hasError: boolean = false; hasError: boolean = false;
errorMessage: string = "" ; errorMessage: string = "" ;
@ -29,7 +25,7 @@ export class PopupCreatePersonComponent
constructor( public dialogRef: MatDialogRef<PopupCreatePersonComponent>, constructor( public dialogRef: MatDialogRef<PopupCreatePersonComponent>,
@Inject(MAT_DIALOG_DATA) public data: any, @Inject(MAT_DIALOG_DATA) public data: any,
private checkEmailService: CheckEmailService, private checkEmailService: CheckEmailService,
private hashageService: HashageService ) { } private messageService: MessageService ) { }
// Appuie sur le bouton "valider" // Appuie sur le bouton "valider"
@ -38,13 +34,15 @@ export class PopupCreatePersonComponent
this.checkField(); this.checkField();
if(!this.hasError) if(!this.hasError)
{ {
if(this.changePassword) this.person.hash_pass = this.hashageService.run(this.password); const data = {
const data = { user: this.person }; email: this.email,
nickname: this.nickname,
// ... password: this.password,
is_admin: this.is_admin
// Faux code };
this.onValiderCallback({ status: "success", data: {}}); this.messageService
.post("admin/create/user", data)
.subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err));
} }
} }
@ -52,19 +50,15 @@ export class PopupCreatePersonComponent
// Callback de 'onValider' // Callback de 'onValider'
onValiderCallback(retour: any) onValiderCallback(retour: any)
{ {
if(retour.status === 'success') if(retour.status !== 'success')
{
this.dialogRef.close(retour);
}
else if(retour.status === 'error')
{ {
console.log(retour); console.log(retour);
this.errorMessage = retour.message; this.errorMessage = retour.error.message;
this.hasError = true; this.hasError = true;
} }
else { else
console.log(retour); {
this.dialogRef.close(null); this.dialogRef.close(retour);
} }
} }
@ -72,15 +66,15 @@ export class PopupCreatePersonComponent
// Check les champs saisis par l'utilisateur // Check les champs saisis par l'utilisateur
checkField(): void checkField(): void
{ {
if(this.person.nickname.length === 0) { if(this.nickname.length === 0) {
this.errorMessage = "Veuillez remplir le champ 'pseudo'."; this.errorMessage = "Veuillez remplir le champ 'pseudo'.";
this.hasError = true; this.hasError = true;
} }
else if(this.person.email.length === 0) { else if(this.email.length === 0) {
this.errorMessage = "Veuillez remplir le champ 'email'."; this.errorMessage = "Veuillez remplir le champ 'email'.";
this.hasError = true; this.hasError = true;
} }
else if(!this.checkEmailService.isValidEmail(this.person.email)) { else if(!this.checkEmailService.isValidEmail(this.email)) {
this.errorMessage = "Email invalide."; this.errorMessage = "Email invalide.";
this.hasError = true; this.hasError = true;
} }

View file

@ -7,13 +7,11 @@
<mat-divider></mat-divider><br> <mat-divider></mat-divider><br>
<!-- role --> <!-- role -->
<mat-radio-group [(ngModel)]="personCopy.is_admin"> <mat-radio-group [(ngModel)]="is_admin">
<mat-radio-button [value]="false" <mat-radio-button [value]="false"
[checked]="!personCopy.is_admin" [checked]="!is_admin">Utilisateur</mat-radio-button> &nbsp;
(click)="personCopy.role = 'utilisateur'">Utilisateur</mat-radio-button> &nbsp;
<mat-radio-button [value]="true" <mat-radio-button [value]="true"
[checked]="personCopy.is_admin" [checked]="is_admin">Admin</mat-radio-button>
(click)="personCopy.role = 'admin'">Admin</mat-radio-button>
</mat-radio-group><br><br> </mat-radio-group><br><br>
<!-- divider --> <!-- divider -->

View file

@ -1,7 +1,7 @@
import {Component, Inject, OnInit} from '@angular/core'; import {Component, Inject, OnInit} from '@angular/core';
import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog"; import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog";
import {CheckEmailService} from "../../../common/services/checkEmail/check-email.service"; import {CheckEmailService} from "../../../common/services/checkEmail/check-email.service";
import {HashageService} from "../../../common/services/hashage/hashage.service"; import {MessageService} from "../../../common/services/message/message.service";
@ -12,15 +12,10 @@ import {HashageService} from "../../../common/services/hashage/hashage.service";
}) })
export class PopupUpdatePersonAdminComponent implements OnInit export class PopupUpdatePersonAdminComponent implements OnInit
{ {
personCopy: any = { id: number = 0;
id: "", is_admin: boolean = false;
nickname: "",
email: "",
hash_pass: "",
is_admin: true,
role: "utilisateur"
};
newPassword: string = ""; newPassword: string = "";
confirmNewPassword: string = "" ; confirmNewPassword: string = "" ;
changePassword: boolean = false ; changePassword: boolean = false ;
hasError: boolean = false; hasError: boolean = false;
@ -30,41 +25,28 @@ export class PopupUpdatePersonAdminComponent implements OnInit
constructor( public dialogRef: MatDialogRef<PopupUpdatePersonAdminComponent>, constructor( public dialogRef: MatDialogRef<PopupUpdatePersonAdminComponent>,
@Inject(MAT_DIALOG_DATA) public data: any, @Inject(MAT_DIALOG_DATA) public data: any,
private checkEmailService: CheckEmailService, private checkEmailService: CheckEmailService,
private hashageService: HashageService ) { } private messageService: MessageService ) { }
ngOnInit(): void ngOnInit(): void
{ {
const person = this.data.person; this.id = this.data.person.id;
this.personCopy = { this.is_admin = this.data.person.is_admin;
id: person.id,
nickname: person.nickname,
email: person.email,
hash_pass: person.hash_pass,
is_admin: person.is_admin,
role: person.role,
};
console.log("ngOnInit")
console.log(this.personCopy);
} }
// Appuie sur le bouton "valider" // Appuie sur le bouton "valider"
onValider(): void onValider(): void
{ {
console.log("onValider")
console.log(this.personCopy);
this.checkField(); this.checkField();
if(!this.hasError) if(!this.hasError)
{ {
if(this.changePassword) this.personCopy.hash_pass = this.hashageService.run(this.newPassword); let data = {};
const data = { user: this.personCopy }; if(this.changePassword) data = { id: this.id, is_admin: this.is_admin, password: this.newPassword }
else data = { id: this.id, is_admin: this.is_admin };
// ... this.messageService
.put("admin/update/user", data)
// Faux code .subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err));
this.onValiderCallback({ status: "success"});
} }
} }
@ -72,14 +54,15 @@ export class PopupUpdatePersonAdminComponent implements OnInit
// Callback de 'onValider' // Callback de 'onValider'
onValiderCallback(retour: any) onValiderCallback(retour: any)
{ {
if(retour.status === 'error') if(retour.status !== 'success')
{ {
console.log(retour); console.log(retour);
this.dialogRef.close(null); this.errorMessage = retour.error.message;
this.hasError = true;
} }
else else
{ {
this.dialogRef.close(this.personCopy); this.dialogRef.close(this.is_admin);
} }
} }

View file

@ -5,6 +5,8 @@ import {PageRegisterComponent} from "./register/page-register/page-register.comp
import {PageProfilComponent} from "./common/components/page-profil/page-profil.component"; import {PageProfilComponent} from "./common/components/page-profil/page-profil.component";
import {PageUserListComponent} from "./admin/userList/page-user-list/page-user-list.component"; import {PageUserListComponent} from "./admin/userList/page-user-list/page-user-list.component";
import {PageRegistryComponent} from "./user/page-registry/page-registry.component"; import {PageRegistryComponent} from "./user/page-registry/page-registry.component";
import {UserGuard} from "./common/guards/user/user.guard";
import {AdminGuard} from "./common/guards/admin/admin.guard";
const routes: Routes = [ const routes: Routes = [
@ -13,13 +15,13 @@ const routes: Routes = [
{ path: "register", component: PageRegisterComponent }, { path: "register", component: PageRegisterComponent },
{ path: "user", component: PageRegistryComponent }, { path: "user", component: PageRegistryComponent, canActivate: [UserGuard] },
{ path: "user/registry", component: PageRegistryComponent }, { path: "user/registry", component: PageRegistryComponent, canActivate: [UserGuard] },
{ path: "user/myProfil", component: PageProfilComponent }, { path: "user/myProfil", component: PageProfilComponent, canActivate: [UserGuard] },
{ path: "admin", component: PageUserListComponent }, { path: "admin", component: PageUserListComponent, canActivate: [AdminGuard] },
{ path: "admin/userList", component: PageUserListComponent }, { path: "admin/userList", component: PageUserListComponent, canActivate: [AdminGuard] },
{ path: "admin/myProfil", component: PageProfilComponent }, { path: "admin/myProfil", component: PageProfilComponent, canActivate: [AdminGuard] },
]; ];
@NgModule({ @NgModule({

View file

@ -40,10 +40,10 @@ export class PageProfilComponent implements OnInit
else if(this.router.url.startsWith("/admin")) this.from = "admin" ; else if(this.router.url.startsWith("/admin")) this.from = "admin" ;
let params = new HttpParams() let params = new HttpParams()
params = params.set("order", ""); params = params.set("order_by", "nickname");
params = params.set("id", this.profilService.getId()); params = params.set("id", this.profilService.getId());
this.messageService this.messageService
.get("user", params) .get("users", params)
.subscribe(ret => this.ngOnInitCallback(ret), err => this.ngOnInitCallback(err)); .subscribe(ret => this.ngOnInitCallback(ret), err => this.ngOnInitCallback(err));
} }
@ -103,7 +103,7 @@ export class PageProfilComponent implements OnInit
onSupprimerCallback(retour: any): void onSupprimerCallback(retour: any): void
{ {
if((retour === null) || (retour === undefined)) this.snackBar.open( "Opération annulé", "", this.configSnackbar); if((retour === null) || (retour === undefined)) this.snackBar.open( "Opération annulé", "", this.configSnackbar);
else if(retour.status === "error") this.snackBar.open(retour.message, "", this.configSnackbar); else if(retour.status === "error") this.snackBar.open(retour.error.message, "", this.configSnackbar);
else if(retour.status === "success") this.router.navigateByUrl("/login"); else if(retour.status === "success") this.router.navigateByUrl("/login");
} }

View file

@ -39,10 +39,9 @@ export class PopupDeleteProfilComponent implements OnInit
.subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err)); .subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err));
} }
else { else {
let params = new HttpParams(); //let params = (new HttpParams()).set("id", this.id);
params = params.set("id", this.id);
this.messageService this.messageService
.delete("admin/delete", params) .delete("admin/delete/user/"+this.id)
.subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err)); .subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err));
} }
} }

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;
}
}
}
}

View file

@ -8,8 +8,8 @@ export class ProfilService
constructor() constructor()
{ {
this.setId(-1); if(localStorage.getItem('id') === null) this.setId(-1);
this.setIsAdmin(false); if(localStorage.getItem('isAdmin') === null) this.setIsAdmin(false);
} }
getId(): number getId(): number
@ -24,7 +24,7 @@ export class ProfilService
localStorage.setItem('id', id.toString()); localStorage.setItem('id', id.toString());
} }
getIsAdmin(): boolean isAdmin(): boolean
{ {
let isAdminString = localStorage.getItem('isAdmin'); let isAdminString = localStorage.getItem('isAdmin');
if(isAdminString === "T") return true; if(isAdminString === "T") return true;

View file

@ -46,14 +46,14 @@ export class PageLoginComponent
if(retour.status !== "success") if(retour.status !== "success")
{ {
console.log(retour); console.log(retour);
this.errorMessage = retour.message; this.errorMessage = retour.error.message;
this.hasError = true; this.hasError = true;
} }
else { else {
this.profilService.setId(retour.data.id); this.profilService.setId(retour.data.id);
this.profilService.setIsAdmin(retour.data.is_admin) this.profilService.setIsAdmin(retour.data.is_admin)
if(retour.data.is_admin) this.router.navigateByUrl('admin/userList'); if(retour.data.is_admin) this.router.navigateByUrl('admin/userList');
else this.router.navigateByUrl('user/userList'); else this.router.navigateByUrl('user/registry');
} }
} }

View file

@ -11,13 +11,13 @@
<!-- nickname --> <!-- nickname -->
<mat-form-field appearance="fill"> <mat-form-field appearance="fill">
<mat-label>Pseudo</mat-label> <mat-label>Pseudo</mat-label>
<input matInput type="text" [(ngModel)]="person.nickname" required> <input matInput type="text" [(ngModel)]="nickname" required>
</mat-form-field><br> </mat-form-field><br>
<!-- email --> <!-- email -->
<mat-form-field appearance="fill"> <mat-form-field appearance="fill">
<mat-label>Email</mat-label> <mat-label>Email</mat-label>
<input matInput type="text" [(ngModel)]="person.email" required> <input matInput type="text" [(ngModel)]="email" required>
</mat-form-field><br> </mat-form-field><br>
<!-- Mot de passe --> <!-- Mot de passe -->

View file

@ -15,13 +15,8 @@ import {MessageService} from "../../common/services/message/message.service";
}) })
export class PageRegisterComponent export class PageRegisterComponent
{ {
person = { email: string = "";
id: "", nickname: string = "";
nickname: "",
email: "",
hash_pass: "",
role: "user"
};
password: string = ""; password: string = "";
confirmPassword: string = ""; confirmPassword: string = "";
hasError: boolean = false; hasError: boolean = false;
@ -41,8 +36,9 @@ export class PageRegisterComponent
if(!this.hasError) if(!this.hasError)
{ {
const data = { const data = {
email: this.person.email, email: this.email,
nickname: this.person.nickname, nickname: this.nickname,
password: this.password,
is_admin: false is_admin: false
}; };
this.messageService this.messageService
@ -58,7 +54,7 @@ export class PageRegisterComponent
if(retour.status !== "success") if(retour.status !== "success")
{ {
console.log(retour); console.log(retour);
this.errorMessage = retour.message; this.errorMessage = retour.error.message;
this.hasError = true; this.hasError = true;
} }
else { else {
@ -73,15 +69,15 @@ export class PageRegisterComponent
// Check les champs saisis par l'utilisateur // Check les champs saisis par l'utilisateur
checkField(): void checkField(): void
{ {
if(this.person.nickname.length === 0) { if(this.nickname.length === 0) {
this.errorMessage = "Veuillez remplir le champ 'pseudo'."; this.errorMessage = "Veuillez remplir le champ 'pseudo'.";
this.hasError = true; this.hasError = true;
} }
else if(this.person.email.length === 0) { else if(this.email.length === 0) {
this.errorMessage = "Veuillez remplir le champ 'email'."; this.errorMessage = "Veuillez remplir le champ 'email'.";
this.hasError = true; this.hasError = true;
} }
else if(!this.checkEmailService.isValidEmail(this.person.email)) { else if(!this.checkEmailService.isValidEmail(this.email)) {
this.errorMessage = "Email invalide."; this.errorMessage = "Email invalide.";
this.hasError = true; this.hasError = true;
} }