diff --git a/src/frontend/app/administration/user/user.component.ts b/src/frontend/app/administration/user/user.component.ts index 85d64c3a67d6c4f7295f606c0ece778aaa36819a..59c3b736f9921d2fb7cc1db07e6fb07d450fba31 100644 --- a/src/frontend/app/administration/user/user.component.ts +++ b/src/frontend/app/administration/user/user.component.ts @@ -126,9 +126,7 @@ export class UserComponent implements OnInit { .subscribe({ next: () => { if (this.authService.user.id === this.user.id) { - this.authService.user.firstname = this.user.firstname; - this.authService.user.lastname = this.user.lastname; - this.authService.user.email = this.user.email; + this.authService.updateUserInfoWithTokenRefresh(); } if (this.passwordRest.newPassword !== '') { this.updateRestUser(); diff --git a/src/frontend/app/document/document.component.ts b/src/frontend/app/document/document.component.ts index 35eb414c44a368f72a96786c84f3fa382b1d026a..2bf3e72042435ebc85ff8a9cb4a1e078286177bd 100755 --- a/src/frontend/app/document/document.component.ts +++ b/src/frontend/app/document/document.component.ts @@ -512,7 +512,7 @@ export class DocumentComponent implements OnInit { this.http.put('../rest/users/' + this.authService.user.id, userUpdated) .subscribe(() => { - this.authService.user = userUpdated; + this.authService.updateUserInfoWithTokenRefresh(); this.notificationService.success('lang.substitutionDeleted'); }); } diff --git a/src/frontend/app/profile/profile.component.html b/src/frontend/app/profile/profile.component.html index 2da7f64d8294c0ae55ff30d7848c72e8650ed389..6e8e42bb3d11aefdbf56b6aee268cb47b9a1feae 100644 --- a/src/frontend/app/profile/profile.component.html +++ b/src/frontend/app/profile/profile.component.html @@ -16,7 +16,7 @@ <div class="panel-content"> <form (ngSubmit)="submitProfile()" #profileForm="ngForm"> <mat-tab-group #tabProfile (selectedTabChange)="initProfileTab($event);" - (swipeleft)="siwtchToleft(tabProfile)" (swiperight)="siwtchToRight(tabProfile)"> + (swipeleft)="switchToleft(tabProfile)" (swiperight)="switchToRight(tabProfile)"> <mat-tab label="{{'lang.informations' | translate}}"> <div class="profile-content"> <mat-form-field class="input-row"> @@ -208,7 +208,7 @@ <div class="form-2-col"> <mat-form-field style="width:100%"> <mat-select placeholder="{{'lang.chooseSubstitute' | translate}}" name="usersList" - [(ngModel)]="profileInfo.substitute"> + [(ngModel)]="profileInfo.substitute" (selectionChange)="toggleSubstitute($event)"> <ng-container *ngFor="let userRest of usersList"> <mat-option *ngIf="userRest.id !== profileInfo.id && !userRest.substitute" [value]="userRest.id"> diff --git a/src/frontend/app/profile/profile.component.ts b/src/frontend/app/profile/profile.component.ts index 6276696918067d98ccb93373093b736e88b63e75..24e9e79e66cd7968b6128989a7e633f5ddc421d1 100644 --- a/src/frontend/app/profile/profile.component.ts +++ b/src/frontend/app/profile/profile.component.ts @@ -73,7 +73,9 @@ export class ProfileComponent implements OnInit { closeProfile() { $('.avatarProfile').css({ 'transform': 'rotate(0deg)' }); $('.avatarProfile').css({ 'content': '' }); - this.profileInfo = JSON.parse(JSON.stringify(this.authService.user)); + setTimeout(() => { + this.profileInfo = JSON.parse(JSON.stringify(this.authService.user)); + }, 200); this.passwordContent.close(); if (this.signaturesService.mobileMode) { @@ -193,10 +195,6 @@ export class ProfileComponent implements OnInit { 'substitute': this.profileInfo.substitute, }; - if (this.profileInfo.substitute !== null) { - alert(this.translate.instant('lang.substitutionWarn')); - } - if (this.profileInfo.picture === this.authService.user.picture) { profileToSend.picture = ''; } else { @@ -206,11 +204,9 @@ export class ProfileComponent implements OnInit { this.http.put('../rest/users/' + this.authService.user.id, profileToSend) .subscribe((data: any) => { - this.authService.user.email = this.profileInfo.email; - this.authService.user.firstname = this.profileInfo.firstname; - this.authService.user.lastname = this.profileInfo.lastname; + this.authService.user.picture = data.user.picture; - this.authService.user.substitute = data.user.substitute; + this.profileInfo.picture = data.user.picture; this.http.put('../rest/users/' + this.authService.user.id + '/preferences', profileToSend.preferences) @@ -222,22 +218,14 @@ export class ProfileComponent implements OnInit { }) ) .subscribe(() => { - this.authService.user.preferences = this.profileInfo.preferences; this.setLang(this.authService.user.preferences.lang); this.cookieService.set('maarchParapheurLang', this.authService.user.preferences.lang); - if (this.profileInfo.substitute !== null) { - this.filtersService.resfreshDocuments(); - if (this.signaturesService.documentsList.length > 0 && this.signaturesService.documentsList[this.signaturesService.indexDocumentsList].owner === false) { - this.router.navigate(['/documents']); - } - } - $('.avatarProfile').css({ 'transform': 'rotate(0deg)' }); if (this.showPassword) { const headers = new HttpHeaders({ - 'Authorization': 'Bearer ' + this.authService.getToken() - }); + 'Authorization': 'Bearer ' + this.authService.getToken() + }); this.http.put('../rest/users/' + this.authService.user.id + '/password', this.password, { observe: 'response', headers: headers }) .subscribe((dataPass: any) => { @@ -260,16 +248,31 @@ export class ProfileComponent implements OnInit { this.notificationService.success('lang.profileUpdated'); } - if (this.profileInfo.substitute !== null && this.signaturesService.signaturesList.length > 0) { - this.http.patch('../rest/users/' + this.authService.user.id + '/signatures/substituted', { 'signatures': this.signaturesService.signaturesList }) - .subscribe(); - } + this.authService.updateUserInfoWithTokenRefresh(); }); }); } + toggleSubstitute(ev: any) { + if (this.profileInfo.substitute !== null) { + alert(this.translate.instant('lang.substitutionWarn')); + } + const newUserSubtituted = ev.value; + + // TO DO : ROUTE IS FAKE + this.http.put('../rest/users/' + this.authService.user.id + '/substitute', newUserSubtituted) + .subscribe(() => { + this.authService.updateUserInfoWithTokenRefresh(); + this.filtersService.resfreshDocuments(); + if (this.signaturesService.documentsList.length > 0 && this.signaturesService.documentsList[this.signaturesService.indexDocumentsList].owner === false) { + this.router.navigate(['/documents']); + } + this.notificationService.success('lang.userSubstituted'); + }); + } + handleFileInput(files: FileList) { this.passwordContent.close(); const fileToUpload = files.item(0); @@ -340,7 +343,7 @@ export class ProfileComponent implements OnInit { return new Array(i); } - siwtchToleft(tab: MatTabGroup) { + switchToleft(tab: MatTabGroup) { tab.selectedIndex++; if (tab.selectedIndex === 2) { @@ -348,7 +351,7 @@ export class ProfileComponent implements OnInit { } } - siwtchToRight(tab: MatTabGroup) { + switchToRight(tab: MatTabGroup) { if (tab.selectedIndex > 0) { tab.selectedIndex--; @@ -380,5 +383,8 @@ export class ProfileComponent implements OnInit { toggleSignature(i: number) { this.signaturesService.signaturesList[i].substituted = !this.signaturesService.signaturesList[i].substituted; + + this.http.patch('../rest/users/' + this.authService.user.id + '/signatures/substituted', { 'signatures': this.signaturesService.signaturesList }) + .subscribe(); } } diff --git a/src/frontend/app/service/auth.service.ts b/src/frontend/app/service/auth.service.ts index 2e0a973fc2e2cfa37be5f747d57742759a5e3498..ef0089d2cf33e52728eb8c84ab3c0b6e5e23f7b1 100755 --- a/src/frontend/app/service/auth.service.ts +++ b/src/frontend/app/service/auth.service.ts @@ -87,6 +87,27 @@ export class AuthService { } updateUserInfo(token: string) { + const currentPicture = this.user.picture; + this.user = JSON.parse(atob(token.split('.')[1])).user; + + this.user.picture = currentPicture; + } + + updateUserInfoWithTokenRefresh() { + this.http.get('../rest/authenticate/token', { + params: { + refreshToken: this.getRefreshToken() + } + }).subscribe({ + next: (data: any) => { + this.setToken(data.token); + + this.updateUserInfo(this.getToken()); + }, + error: err => { + this.notificationService.handleErrors(err); + } + }); } }