From 4620f0dff9f82228dc89655f3bb250923ffcc625 Mon Sep 17 00:00:00 2001 From: MiharyR Date: Fri, 14 Jan 2022 20:15:50 +0100 Subject: [PATCH] connexion avec le back --- .../page-user-list.component.ts | 16 +++--- .../popup-create-person.component.html | 6 +-- .../popup-create-person.component.ts | 52 ++++++++----------- .../popup-update-person-admin.component.html | 8 ++- .../popup-update-person-admin.component.ts | 51 ++++++------------ frontend/src/app/app-routing.module.ts | 14 ++--- .../page-profil/page-profil.component.ts | 6 +-- .../popup-delete-profil.component.ts | 5 +- .../common/guards/admin/admin.guard.spec.ts | 16 ++++++ .../app/common/guards/admin/admin.guard.ts | 31 +++++++++++ .../app/common/guards/user/user.guard.spec.ts | 16 ++++++ .../src/app/common/guards/user/user.guard.ts | 31 +++++++++++ .../common/services/profil/profil.service.ts | 6 +-- .../login/page-login/page-login.component.ts | 4 +- .../page-register.component.html | 4 +- .../page-register/page-register.component.ts | 22 ++++---- 16 files changed, 178 insertions(+), 110 deletions(-) create mode 100644 frontend/src/app/common/guards/admin/admin.guard.spec.ts create mode 100644 frontend/src/app/common/guards/admin/admin.guard.ts create mode 100644 frontend/src/app/common/guards/user/user.guard.spec.ts create mode 100644 frontend/src/app/common/guards/user/user.guard.ts diff --git a/frontend/src/app/admin/userList/page-user-list/page-user-list.component.ts b/frontend/src/app/admin/userList/page-user-list/page-user-list.component.ts index b398b65..6496d7e 100644 --- a/frontend/src/app/admin/userList/page-user-list/page-user-list.component.ts +++ b/frontend/src/app/admin/userList/page-user-list/page-user-list.component.ts @@ -77,6 +77,8 @@ export class PageUserListComponent implements AfterViewInit this.snackBar.open( "Opération annulée", "", this.configSnackBar); } else { + if(retour.data.is_admin) retour.data.role = "admin" ; + else retour.data.role = "utilisateur" ; this.dataSource.data.push(retour.data); this.dataSource.data = this.dataSource.data; this.dataSource = this.dataSource; @@ -96,17 +98,17 @@ export class PageUserListComponent implements AfterViewInit this.dialog .open(PopupUpdatePersonAdminComponent, config) .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); } else { - const index = this.dataSource.data.findIndex( elt => (elt.id === personToUpdate.id)); - this.dataSource.data.splice(index, 1, retour.data); - this.dataSource.data = this.dataSource.data; - this.dataSource = this.dataSource; + const index = this.dataSource.data.findIndex(elt => (elt.id === personToUpdate.id)); + this.dataSource.data[index].is_admin = is_admin; + if(is_admin) this.dataSource.data[index].role = "admin"; + else this.dataSource.data[index].role = "utilisateur"; 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") { - this.snackBar.open(retour.message, "", this.configSnackBar); + this.snackBar.open(retour.error.message, "", this.configSnackBar); } else { const index = this.dataSource.data.findIndex( elt => (elt.id === personToDelete.id)); diff --git a/frontend/src/app/admin/userList/popup-create-person/popup-create-person.component.html b/frontend/src/app/admin/userList/popup-create-person/popup-create-person.component.html index 9f648d7..15a27f5 100644 --- a/frontend/src/app/admin/userList/popup-create-person/popup-create-person.component.html +++ b/frontend/src/app/admin/userList/popup-create-person/popup-create-person.component.html @@ -7,13 +7,13 @@ Pseudo - +
Email - +
@@ -29,7 +29,7 @@
- + Utilisateur   Admin

diff --git a/frontend/src/app/admin/userList/popup-create-person/popup-create-person.component.ts b/frontend/src/app/admin/userList/popup-create-person/popup-create-person.component.ts index 23f6a13..11f0b1c 100644 --- a/frontend/src/app/admin/userList/popup-create-person/popup-create-person.component.ts +++ b/frontend/src/app/admin/userList/popup-create-person/popup-create-person.component.ts @@ -1,7 +1,7 @@ import {Component, Inject} from '@angular/core'; import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog"; 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 { - person = { - id: "", - nickname: "", - email: "", - hash_pass: "", - is_admin: false, - }; + nickname: string = ""; + email: string = ""; + is_admin: boolean = false; password: string = ""; + confirmPassword: string = "" ; - changePassword: boolean = false ; hasError: boolean = false; errorMessage: string = "" ; @@ -29,7 +25,7 @@ export class PopupCreatePersonComponent constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, private checkEmailService: CheckEmailService, - private hashageService: HashageService ) { } + private messageService: MessageService ) { } // Appuie sur le bouton "valider" @@ -38,13 +34,15 @@ export class PopupCreatePersonComponent this.checkField(); if(!this.hasError) { - if(this.changePassword) this.person.hash_pass = this.hashageService.run(this.password); - const data = { user: this.person }; - - // ... - - // Faux code - this.onValiderCallback({ status: "success", data: {}}); + const data = { + email: this.email, + nickname: this.nickname, + password: this.password, + is_admin: this.is_admin + }; + 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' onValiderCallback(retour: any) { - if(retour.status === 'success') - { - this.dialogRef.close(retour); - } - else if(retour.status === 'error') + if(retour.status !== 'success') { console.log(retour); - this.errorMessage = retour.message; + this.errorMessage = retour.error.message; this.hasError = true; } - else { - console.log(retour); - this.dialogRef.close(null); + else + { + this.dialogRef.close(retour); } } @@ -72,15 +66,15 @@ export class PopupCreatePersonComponent // Check les champs saisis par l'utilisateur checkField(): void { - if(this.person.nickname.length === 0) { + if(this.nickname.length === 0) { this.errorMessage = "Veuillez remplir le champ 'pseudo'."; this.hasError = true; } - else if(this.person.email.length === 0) { + else if(this.email.length === 0) { this.errorMessage = "Veuillez remplir le champ 'email'."; this.hasError = true; } - else if(!this.checkEmailService.isValidEmail(this.person.email)) { + else if(!this.checkEmailService.isValidEmail(this.email)) { this.errorMessage = "Email invalide."; this.hasError = true; } diff --git a/frontend/src/app/admin/userList/popup-update-person-admin/popup-update-person-admin.component.html b/frontend/src/app/admin/userList/popup-update-person-admin/popup-update-person-admin.component.html index c1a0ce9..2c2e58c 100644 --- a/frontend/src/app/admin/userList/popup-update-person-admin/popup-update-person-admin.component.html +++ b/frontend/src/app/admin/userList/popup-update-person-admin/popup-update-person-admin.component.html @@ -7,13 +7,11 @@
- + Utilisateur   + [checked]="!is_admin">Utilisateur   Admin + [checked]="is_admin">Admin

diff --git a/frontend/src/app/admin/userList/popup-update-person-admin/popup-update-person-admin.component.ts b/frontend/src/app/admin/userList/popup-update-person-admin/popup-update-person-admin.component.ts index 6f1c8bf..1bf55e4 100644 --- a/frontend/src/app/admin/userList/popup-update-person-admin/popup-update-person-admin.component.ts +++ b/frontend/src/app/admin/userList/popup-update-person-admin/popup-update-person-admin.component.ts @@ -1,7 +1,7 @@ import {Component, Inject, OnInit} from '@angular/core'; import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog"; 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 { - personCopy: any = { - id: "", - nickname: "", - email: "", - hash_pass: "", - is_admin: true, - role: "utilisateur" - }; + id: number = 0; + is_admin: boolean = false; newPassword: string = ""; + confirmNewPassword: string = "" ; changePassword: boolean = false ; hasError: boolean = false; @@ -30,41 +25,28 @@ export class PopupUpdatePersonAdminComponent implements OnInit constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: any, private checkEmailService: CheckEmailService, - private hashageService: HashageService ) { } + private messageService: MessageService ) { } ngOnInit(): void { - const person = this.data.person; - this.personCopy = { - 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); + this.id = this.data.person.id; + this.is_admin = this.data.person.is_admin; } // Appuie sur le bouton "valider" onValider(): void { - console.log("onValider") - console.log(this.personCopy); - this.checkField(); if(!this.hasError) { - if(this.changePassword) this.personCopy.hash_pass = this.hashageService.run(this.newPassword); - const data = { user: this.personCopy }; - - // ... - - // Faux code - this.onValiderCallback({ status: "success"}); + let data = {}; + 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) + .subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err)); } } @@ -72,14 +54,15 @@ export class PopupUpdatePersonAdminComponent implements OnInit // Callback de 'onValider' onValiderCallback(retour: any) { - if(retour.status === 'error') + if(retour.status !== 'success') { console.log(retour); - this.dialogRef.close(null); + this.errorMessage = retour.error.message; + this.hasError = true; } else { - this.dialogRef.close(this.personCopy); + this.dialogRef.close(this.is_admin); } } diff --git a/frontend/src/app/app-routing.module.ts b/frontend/src/app/app-routing.module.ts index 5373cfb..6fd371d 100644 --- a/frontend/src/app/app-routing.module.ts +++ b/frontend/src/app/app-routing.module.ts @@ -5,6 +5,8 @@ import {PageRegisterComponent} from "./register/page-register/page-register.comp import {PageProfilComponent} from "./common/components/page-profil/page-profil.component"; import {PageUserListComponent} from "./admin/userList/page-user-list/page-user-list.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 = [ @@ -13,13 +15,13 @@ const routes: Routes = [ { path: "register", component: PageRegisterComponent }, - { path: "user", component: PageRegistryComponent }, - { path: "user/registry", component: PageRegistryComponent }, - { path: "user/myProfil", component: PageProfilComponent }, + { path: "user", component: PageRegistryComponent, canActivate: [UserGuard] }, + { path: "user/registry", component: PageRegistryComponent, canActivate: [UserGuard] }, + { path: "user/myProfil", component: PageProfilComponent, canActivate: [UserGuard] }, - { path: "admin", component: PageUserListComponent }, - { path: "admin/userList", component: PageUserListComponent }, - { path: "admin/myProfil", component: PageProfilComponent }, + { path: "admin", component: PageUserListComponent, canActivate: [AdminGuard] }, + { path: "admin/userList", component: PageUserListComponent, canActivate: [AdminGuard] }, + { path: "admin/myProfil", component: PageProfilComponent, canActivate: [AdminGuard] }, ]; @NgModule({ diff --git a/frontend/src/app/common/components/page-profil/page-profil.component.ts b/frontend/src/app/common/components/page-profil/page-profil.component.ts index f1f8349..23e3d43 100644 --- a/frontend/src/app/common/components/page-profil/page-profil.component.ts +++ b/frontend/src/app/common/components/page-profil/page-profil.component.ts @@ -40,10 +40,10 @@ export class PageProfilComponent implements OnInit else if(this.router.url.startsWith("/admin")) this.from = "admin" ; let params = new HttpParams() - params = params.set("order", ""); + params = params.set("order_by", "nickname"); params = params.set("id", this.profilService.getId()); this.messageService - .get("user", params) + .get("users", params) .subscribe(ret => this.ngOnInitCallback(ret), err => this.ngOnInitCallback(err)); } @@ -103,7 +103,7 @@ export class PageProfilComponent implements OnInit onSupprimerCallback(retour: any): void { 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"); } diff --git a/frontend/src/app/common/components/popup-delete-profil/popup-delete-profil.component.ts b/frontend/src/app/common/components/popup-delete-profil/popup-delete-profil.component.ts index 2b3065a..49d6b5b 100644 --- a/frontend/src/app/common/components/popup-delete-profil/popup-delete-profil.component.ts +++ b/frontend/src/app/common/components/popup-delete-profil/popup-delete-profil.component.ts @@ -39,10 +39,9 @@ export class PopupDeleteProfilComponent implements OnInit .subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err)); } else { - let params = new HttpParams(); - params = params.set("id", this.id); + //let params = (new HttpParams()).set("id", this.id); this.messageService - .delete("admin/delete", params) + .delete("admin/delete/user/"+this.id) .subscribe(ret => this.onValiderCallback(ret), err => this.onValiderCallback(err)); } } diff --git a/frontend/src/app/common/guards/admin/admin.guard.spec.ts b/frontend/src/app/common/guards/admin/admin.guard.spec.ts new file mode 100644 index 0000000..e982f62 --- /dev/null +++ b/frontend/src/app/common/guards/admin/admin.guard.spec.ts @@ -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(); + }); +}); diff --git a/frontend/src/app/common/guards/admin/admin.guard.ts b/frontend/src/app/common/guards/admin/admin.guard.ts new file mode 100644 index 0000000..701b288 --- /dev/null +++ b/frontend/src/app/common/guards/admin/admin.guard.ts @@ -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 | Promise | 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; + } + } + } + +} diff --git a/frontend/src/app/common/guards/user/user.guard.spec.ts b/frontend/src/app/common/guards/user/user.guard.spec.ts new file mode 100644 index 0000000..a657320 --- /dev/null +++ b/frontend/src/app/common/guards/user/user.guard.spec.ts @@ -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(); + }); +}); diff --git a/frontend/src/app/common/guards/user/user.guard.ts b/frontend/src/app/common/guards/user/user.guard.ts new file mode 100644 index 0000000..d357d34 --- /dev/null +++ b/frontend/src/app/common/guards/user/user.guard.ts @@ -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 | Promise | 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; + } + } + } + +} diff --git a/frontend/src/app/common/services/profil/profil.service.ts b/frontend/src/app/common/services/profil/profil.service.ts index 4f1c55a..71fac4a 100644 --- a/frontend/src/app/common/services/profil/profil.service.ts +++ b/frontend/src/app/common/services/profil/profil.service.ts @@ -8,8 +8,8 @@ export class ProfilService constructor() { - this.setId(-1); - this.setIsAdmin(false); + if(localStorage.getItem('id') === null) this.setId(-1); + if(localStorage.getItem('isAdmin') === null) this.setIsAdmin(false); } getId(): number @@ -24,7 +24,7 @@ export class ProfilService localStorage.setItem('id', id.toString()); } - getIsAdmin(): boolean + isAdmin(): boolean { let isAdminString = localStorage.getItem('isAdmin'); if(isAdminString === "T") return true; diff --git a/frontend/src/app/login/page-login/page-login.component.ts b/frontend/src/app/login/page-login/page-login.component.ts index d855ba1..54ce267 100644 --- a/frontend/src/app/login/page-login/page-login.component.ts +++ b/frontend/src/app/login/page-login/page-login.component.ts @@ -46,14 +46,14 @@ export class PageLoginComponent if(retour.status !== "success") { console.log(retour); - this.errorMessage = retour.message; + this.errorMessage = retour.error.message; this.hasError = true; } else { this.profilService.setId(retour.data.id); this.profilService.setIsAdmin(retour.data.is_admin) if(retour.data.is_admin) this.router.navigateByUrl('admin/userList'); - else this.router.navigateByUrl('user/userList'); + else this.router.navigateByUrl('user/registry'); } } diff --git a/frontend/src/app/register/page-register/page-register.component.html b/frontend/src/app/register/page-register/page-register.component.html index b2ca879..afa10a8 100644 --- a/frontend/src/app/register/page-register/page-register.component.html +++ b/frontend/src/app/register/page-register/page-register.component.html @@ -11,13 +11,13 @@ Pseudo - +
Email - +
diff --git a/frontend/src/app/register/page-register/page-register.component.ts b/frontend/src/app/register/page-register/page-register.component.ts index 6638448..ebd1e62 100644 --- a/frontend/src/app/register/page-register/page-register.component.ts +++ b/frontend/src/app/register/page-register/page-register.component.ts @@ -15,13 +15,8 @@ import {MessageService} from "../../common/services/message/message.service"; }) export class PageRegisterComponent { - person = { - id: "", - nickname: "", - email: "", - hash_pass: "", - role: "user" - }; + email: string = ""; + nickname: string = ""; password: string = ""; confirmPassword: string = ""; hasError: boolean = false; @@ -41,8 +36,9 @@ export class PageRegisterComponent if(!this.hasError) { const data = { - email: this.person.email, - nickname: this.person.nickname, + email: this.email, + nickname: this.nickname, + password: this.password, is_admin: false }; this.messageService @@ -58,7 +54,7 @@ export class PageRegisterComponent if(retour.status !== "success") { console.log(retour); - this.errorMessage = retour.message; + this.errorMessage = retour.error.message; this.hasError = true; } else { @@ -73,15 +69,15 @@ export class PageRegisterComponent // Check les champs saisis par l'utilisateur checkField(): void { - if(this.person.nickname.length === 0) { + if(this.nickname.length === 0) { this.errorMessage = "Veuillez remplir le champ 'pseudo'."; this.hasError = true; } - else if(this.person.email.length === 0) { + else if(this.email.length === 0) { this.errorMessage = "Veuillez remplir le champ 'email'."; this.hasError = true; } - else if(!this.checkEmailService.isValidEmail(this.person.email)) { + else if(!this.checkEmailService.isValidEmail(this.email)) { this.errorMessage = "Email invalide."; this.hasError = true; }