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

View file

@ -7,13 +7,13 @@
<!-- nickname -->
<mat-form-field appearance="fill">
<mat-label>Pseudo</mat-label>
<input matInput type="text" [(ngModel)]="person.nickname" required>
<input matInput type="text" [(ngModel)]="nickname" required>
</mat-form-field><br>
<!-- email -->
<mat-form-field appearance="fill">
<mat-label>Email</mat-label>
<input matInput type="text" [(ngModel)]="person.email" required>
<input matInput type="text" [(ngModel)]="email" required>
</mat-form-field><br>
<!-- mot de passe -->
@ -29,7 +29,7 @@
</mat-form-field><br>
<!-- 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]="true">Admin</mat-radio-button>
</mat-radio-group><br><br>

View file

@ -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<PopupCreatePersonComponent>,
@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;
}

View file

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

View file

@ -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<PopupUpdatePersonAdminComponent>,
@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);
}
}

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 {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({

View file

@ -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");
}

View file

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

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()
{
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;

View file

@ -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');
}
}

View file

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

View file

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