diff --git a/src/frontend/app/app.component.ts b/src/frontend/app/app.component.ts index e98a4906ff7863b994045f68331d30da0f143212..20bc3f49e426a29ce101532778d4726097a16cd9 100755 --- a/src/frontend/app/app.component.ts +++ b/src/frontend/app/app.component.ts @@ -24,24 +24,5 @@ export class AppComponent { this.cookieService.set( 'maarchParapheurLang', 'fr' ); translate.setDefaultLang('fr'); } - - if (this.cookieService.check('maarchParapheurAuth')) { - const cookieInfo = JSON.parse(atob(this.cookieService.get('maarchParapheurAuth'))); - - this.http.get('../rest/users/' + cookieInfo.id) - .subscribe((data: any) => { - this.signaturesService.userLogged = data.user; - if (this.signaturesService.signaturesList.length === 0) { - this.http.get('../rest/users/' + this.signaturesService.userLogged.id + '/signatures') - .subscribe((dataSign: any) => { - this.signaturesService.signaturesList = dataSign.signatures; - }); - } - this.translate.use(this.signaturesService.userLogged.preferences.lang); - }, - (err: any) => { - this.notificationService.handleErrors(err); - }); - } } } diff --git a/src/frontend/app/app.module.ts b/src/frontend/app/app.module.ts index eb2838be879832b4d5ff112992041412ca7d6aac..96ff4fbc1078fda5fa9a801811d04635f6445c99 100755 --- a/src/frontend/app/app.module.ts +++ b/src/frontend/app/app.module.ts @@ -1,11 +1,14 @@ import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; -import { HttpClientModule, HttpClient } from '@angular/common/http'; +import { HttpClientModule, HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http'; import { RouterModule } from '@angular/router'; import { NgModule } from '@angular/core'; import { HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser'; +import { AuthGuard } from './service/auth.guard'; +import { AuthInterceptor } from './service/auth-interceptor.service'; + // import ngx-translate and the http loader import { TranslateLoader, TranslateModule } from '@ngx-translate/core'; import { TranslateHttpLoader } from '@ngx-translate/http-loader'; @@ -119,12 +122,12 @@ import { ConfirmComponent } from './plugins/confirm.component'; AngularDraggableModule, AppMaterialModule, RouterModule.forRoot([ - { path: 'administration', component: AdministrationComponent }, - { path: 'administration/users', component: UsersListComponent }, - { path: 'administration/users/new', component: UserComponent }, - { path: 'administration/users/:id', component: UserComponent }, + { path: 'administration', canActivate: [AuthGuard], component: AdministrationComponent }, + { path: 'administration/users', canActivate: [AuthGuard], component: UsersListComponent }, + { path: 'administration/users/new', canActivate: [AuthGuard], component: UserComponent }, + { path: 'administration/users/:id', canActivate: [AuthGuard], component: UserComponent }, { path: 'documents/:id', component: DocumentComponent }, - { path: 'documents', component: DocumentComponent }, + { path: 'documents', canActivate: [AuthGuard], component: DocumentComponent }, { path: 'login', component: LoginComponent }, { path: 'forgot-password', component: ForgotPasswordComponent }, { path: 'update-password', component: UpdatePasswordComponent }, @@ -140,7 +143,9 @@ import { ConfirmComponent } from './plugins/confirm.component'; SignaturesComponent, ConfirmComponent ], - providers: [SignaturesContentService, + providers: [ + { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }, + SignaturesContentService, FiltersService, NotificationService, { diff --git a/src/frontend/app/document/document.component.ts b/src/frontend/app/document/document.component.ts index 9a7a7877044d2085eb5444b7a883f25fd7a4f231..9a980a3b1fbfe08999f406386975031ee52d9ee1 100755 --- a/src/frontend/app/document/document.component.ts +++ b/src/frontend/app/document/document.component.ts @@ -143,10 +143,6 @@ export class DocumentComponent implements OnInit { private cookieService: CookieService, private sanitizer: DomSanitizer, public dialog: MatDialog, private bottomSheet: MatBottomSheet) { this.draggable = false; - - if (!this.cookieService.check('maarchParapheurAuth')) { - this.router.navigate(['/login']); - } } ngOnInit(): void { diff --git a/src/frontend/app/profile/profile.component.ts b/src/frontend/app/profile/profile.component.ts index 0b0a9c272854c249db47e4c69f68bfdae70732a8..9a37b324084aa1440e7f340ae5d93ec569313b4a 100644 --- a/src/frontend/app/profile/profile.component.ts +++ b/src/frontend/app/profile/profile.component.ts @@ -64,21 +64,10 @@ export class ProfileComponent implements OnInit { msgButton = 'lang.validate'; loading: boolean = false; - constructor(private translate: TranslateService, public http: HttpClient, private router: Router, public sanitizer: DomSanitizer, public notificationService: NotificationService, public signaturesService: SignaturesContentService, private cookieService: CookieService, public filtersService: FiltersService) { } + constructor(private translate: TranslateService, public http: HttpClient, private router: Router, public sanitizer: DomSanitizer, public notificationService: NotificationService, public signaturesService: SignaturesContentService, private cookieService: CookieService, public filtersService: FiltersService) { } ngOnInit(): void { - if (this.cookieService.check('maarchParapheurAuth')) { - this.loading = true; - const cookieInfo = JSON.parse(atob(this.cookieService.get('maarchParapheurAuth'))); - this.http.get('../rest/users/' + cookieInfo.id) - .subscribe((data: any) => { - this.profileInfo = data.user; - this.loading = false; - }, - (err: any) => { - this.notificationService.handleErrors(err); - }); - } + this.profileInfo = this.signaturesService.userLogged; } closeProfile() { @@ -93,7 +82,6 @@ export class ProfileComponent implements OnInit { } } - changePasswd() { this.showPassword = true; this.getPassRules(); @@ -113,7 +101,7 @@ export class ProfileComponent implements OnInit { this.passwordRules.minLength.enabled = rule.enabled; this.passwordRules.minLength.value = rule.value; if (rule.enabled) { - this.translate.get('lang.minLengthChar', {charLength: rule.value}).subscribe((res: string) => { + this.translate.get('lang.minLengthChar', { charLength: rule.value }).subscribe((res: string) => { ruleTextArr.push(res); }); } @@ -142,7 +130,7 @@ export class ProfileComponent implements OnInit { this.passwordRules.renewal.enabled = rule.enabled; this.passwordRules.renewal.value = rule.value; if (rule.enabled) { - this.translate.get('lang.renewalInfo', {time: rule.value}).subscribe((res: string) => { + this.translate.get('lang.renewalInfo', { time: rule.value }).subscribe((res: string) => { otherRuleTextArr.push(res); }); } @@ -150,7 +138,7 @@ export class ProfileComponent implements OnInit { this.passwordRules.historyLastUse.enabled = rule.enabled; this.passwordRules.historyLastUse.value = rule.value; if (rule.enabled) { - this.translate.get('lang.historyUseInfo', {countPwd: rule.value}).subscribe((res: string) => { + this.translate.get('lang.historyUseInfo', { countPwd: rule.value }).subscribe((res: string) => { otherRuleTextArr.push(res); }); } @@ -174,7 +162,7 @@ export class ProfileComponent implements OnInit { } else if (!password.match(/[^A-Za-z0-9]/g) && this.passwordRules.complexitySpecial.enabled) { this.handlePassword.errorMsg = 'lang.specialCharRequired'; } else if (password.length < this.passwordRules.minLength.value && this.passwordRules.minLength.enabled) { - this.translate.get('lang.minLengthChar', {charLength: this.passwordRules.minLength.value}).subscribe((res: string) => { + this.translate.get('lang.minLengthChar', { charLength: this.passwordRules.minLength.value }).subscribe((res: string) => { this.handlePassword.errorMsg = res; }); } else { @@ -222,55 +210,62 @@ export class ProfileComponent implements OnInit { this.signaturesService.userLogged.firstname = this.profileInfo.firstname; this.signaturesService.userLogged.lastname = this.profileInfo.lastname; this.signaturesService.userLogged.picture = data.user.picture; - this.signaturesService.userLogged.preferences = data.user.preferences; this.signaturesService.userLogged.substitute = data.user.substitute; this.profileInfo.picture = data.user.picture; - this.setLang(this.signaturesService.userLogged.preferences.lang); - this.cookieService.set( 'maarchParapheurLang', this.signaturesService.userLogged.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']); - } - } + this.http.put('../rest/users/' + this.signaturesService.userLogged.id + '/preferences', profileToSend.preferences) + .subscribe(() => { + this.signaturesService.userLogged.preferences = this.profileInfo.preferences; + this.setLang(this.signaturesService.userLogged.preferences.lang); + this.cookieService.set('maarchParapheurLang', this.signaturesService.userLogged.preferences.lang); - $('.avatarProfile').css({ 'transform': 'rotate(0deg)' }); + 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']); + } + } - if (this.showPassword) { - this.http.put('../rest/users/' + this.signaturesService.userLogged.id + '/password', this.password) - .subscribe(() => { - this.password.newPassword = ''; - this.password.passwordConfirmation = ''; - this.password.currentPassword = ''; + $('.avatarProfile').css({ 'transform': 'rotate(0deg)' }); + + if (this.showPassword) { + this.http.put('../rest/users/' + this.signaturesService.userLogged.id + '/password', this.password) + .subscribe(() => { + this.password.newPassword = ''; + this.password.passwordConfirmation = ''; + this.password.currentPassword = ''; + this.notificationService.success('lang.profileUpdated'); + this.disableState = false; + this.msgButton = 'lang.validate'; + this.closeProfile(); + }, (err) => { + this.disableState = false; + this.msgButton = 'lang.validate'; + if (err.status === 401) { + this.notificationService.error('lang.wrongPassword'); + } else { + this.notificationService.handleErrors(err); + } + }); + } + + if (!this.showPassword) { this.notificationService.success('lang.profileUpdated'); this.disableState = false; this.msgButton = 'lang.validate'; this.closeProfile(); - }, (err) => { - this.disableState = false; - this.msgButton = 'lang.validate'; - if (err.status === 401) { - this.notificationService.error('lang.wrongPassword'); - } else { - this.notificationService.handleErrors(err); - } - }); - } - - if (!this.showPassword) { - this.notificationService.success('lang.profileUpdated'); - this.disableState = false; - this.msgButton = 'lang.validate'; - this.closeProfile(); - } - - if (this.profileInfo.substitute !== null && this.signaturesService.signaturesList.length > 0) { - this.http.patch('../rest/users/' + this.signaturesService.userLogged.id + '/signatures/substituted', {'signatures': this.signaturesService.signaturesList}) - .subscribe(() => { }, (err) => { - this.notificationService.handleErrors(err); - }); - } + } + + if (this.profileInfo.substitute !== null && this.signaturesService.signaturesList.length > 0) { + this.http.patch('../rest/users/' + this.signaturesService.userLogged.id + '/signatures/substituted', { 'signatures': this.signaturesService.signaturesList }) + .subscribe(() => { }, (err) => { + this.notificationService.handleErrors(err); + }); + } + + }, (err) => { + this.notificationService.handleErrors(err); + }); }, (err) => { this.disableState = false; @@ -405,7 +400,7 @@ export class ProfileComponent implements OnInit { this.http.put('../rest/users/' + this.currentUserRest.id + '/password', { 'newPassword': this.currentUserRestPassword }) .subscribe(() => { this.currentUserRestPassword = ''; - this.translate.get('lang.passwordOfUserUpdated', {user: this.currentUserRest.firstname + ' ' + this.currentUserRest.lastname}).subscribe((res: string) => { + this.translate.get('lang.passwordOfUserUpdated', { user: this.currentUserRest.firstname + ' ' + this.currentUserRest.lastname }).subscribe((res: string) => { this.notificationService.success(res); }); }, (err) => { diff --git a/src/frontend/app/service/auth-interceptor.service.ts b/src/frontend/app/service/auth-interceptor.service.ts new file mode 100644 index 0000000000000000000000000000000000000000..80c573f97d9c987ae06bd368dda3ea7d126f7d63 --- /dev/null +++ b/src/frontend/app/service/auth-interceptor.service.ts @@ -0,0 +1,13 @@ +import { Injectable } from '@angular/core'; +import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; +import { Observable } from 'rxjs'; + +@Injectable() +export class AuthInterceptor implements HttpInterceptor { + + constructor() {} + + intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { + return next.handle(req); + } +} diff --git a/src/frontend/app/service/auth.guard.ts b/src/frontend/app/service/auth.guard.ts new file mode 100644 index 0000000000000000000000000000000000000000..fceebd14825bb77644bad4f655a6978889c93abd --- /dev/null +++ b/src/frontend/app/service/auth.guard.ts @@ -0,0 +1,46 @@ + +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; +import { Observable, BehaviorSubject } from 'rxjs'; +import { tap } from 'rxjs/operators'; +import { CookieService } from 'ngx-cookie-service'; +import { HttpClient } from '@angular/common/http'; +import { SignaturesContentService } from './signatures.service'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthGuard implements CanActivate { + + constructor(public http: HttpClient, private router: Router, public signaturesService: SignaturesContentService, private cookieService: CookieService) { } + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + if (this.cookieService.check('maarchParapheurAuth')) { + console.log('Cookie ok !'); + if (this.signaturesService.userLogged.id === undefined) { + const cookieInfo = JSON.parse(atob(this.cookieService.get('maarchParapheurAuth'))); + this.http.get('../rest/users/' + cookieInfo.id) + .subscribe((data: any) => { + this.signaturesService.userLogged = data.user; + + if (this.signaturesService.signaturesList.length === 0) { + this.http.get('../rest/users/' + this.signaturesService.userLogged.id + '/signatures') + .subscribe((dataSign: any) => { + this.signaturesService.signaturesList = dataSign.signatures; + }); + } + }, + (err: any) => { + this.router.navigateByUrl('/login'); + }); + return true; + } else { + return true; + } + } else { + console.log('auth failed !'); + this.router.navigateByUrl('/login'); + return false; + } + } +} diff --git a/src/frontend/app/sidebar/administration/admin-sidebar.component.html b/src/frontend/app/sidebar/administration/admin-sidebar.component.html index 480344e0fe894b2a3a427893b0165b09da0ff00b..492b1435fb57172ceefb10cdf7f770367aa1efa2 100644 --- a/src/frontend/app/sidebar/administration/admin-sidebar.component.html +++ b/src/frontend/app/sidebar/administration/admin-sidebar.component.html @@ -14,8 +14,7 @@ {{signaturesService.userLogged.firstname}} {{signaturesService.userLogged.lastname}} </div> <div *ngIf="signaturesService.userLogged.picture" class="avatar" - [ngStyle]="{'background': 'url(' + signaturesService.userLogged.picture + ') no-repeat scroll center center / cover'}" - (click)="openProfile()"> + [ngStyle]="{'background': 'url(' + signaturesService.userLogged.picture + ') no-repeat scroll center center / cover'}"> </div> </header> <header class="sidebar-header"> diff --git a/src/frontend/app/sidebar/administration/admin-sidebar.component.scss b/src/frontend/app/sidebar/administration/admin-sidebar.component.scss index 0b6dcbd2ac9e4b3d6f9a95fa4545e7ab5193bc55..7249e5b399a31204dd916939ac965406b5ae1638 100644 --- a/src/frontend/app/sidebar/administration/admin-sidebar.component.scss +++ b/src/frontend/app/sidebar/administration/admin-sidebar.component.scss @@ -36,7 +36,6 @@ } .avatar { - cursor: pointer; position: absolute; width: 65px; height: 65px; @@ -46,11 +45,6 @@ background-size: cover; background-repeat: no-repeat; background-position: center; - transition: all 0.2s; -} - -.avatar:hover { - box-shadow: 0px 0px 5px 0px #656565; } .user { diff --git a/src/frontend/app/sidebar/administration/admin-sidebar.component.ts b/src/frontend/app/sidebar/administration/admin-sidebar.component.ts index 9314cc6f3ffbd9528df12c9df75083886e579713..b18cbd6a63c3c375548085bc1dfaa8bf084a45af 100644 --- a/src/frontend/app/sidebar/administration/admin-sidebar.component.ts +++ b/src/frontend/app/sidebar/administration/admin-sidebar.component.ts @@ -45,18 +45,6 @@ export class AdminSidebarComponent implements OnInit { }); } - openProfile() { - this.signaturesService.sideNavRigtDatas = { - mode : 'profile', - width : '650px', - locked : true, - }; - if (this.signaturesService.mobileMode) { - this.snavLeftComponent.close(); - this.snavRightComponent.open(); - } - } - openHome() { this.router.navigate(['/documents/']); if (this.signaturesService.mobileMode) { diff --git a/src/frontend/app/sidebar/sidebar.component.html b/src/frontend/app/sidebar/sidebar.component.html index 5c9407affc522f6be1261a63c6565f058d99507a..cae66d466c8d0f2f268722ec318aafa4770a6cbd 100755 --- a/src/frontend/app/sidebar/sidebar.component.html +++ b/src/frontend/app/sidebar/sidebar.component.html @@ -4,7 +4,7 @@ <button class="logout-button" mat-icon-button (click)="logout()"> <mat-icon fontSet="fas" fontIcon="fa-power-off"></mat-icon> </button> - <button class="admin-button" mat-icon-button (click)="openAdmin()"> + <button *ngIf="signaturesService.userLogged.hasAdmin" class="admin-button" mat-icon-button (click)="openAdmin()"> <mat-icon fontSet="fas" fontIcon="fa-tools"></mat-icon> </button> <button *ngIf="checkClose()" class="closePanel"