From a3a8da0726f2d87eed27faa11be2de65f23991cb Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 17 Apr 2020 18:24:46 +0200
Subject: [PATCH] FEAT #13670 TIME 1 add update-password redirect

---
 src/frontend/app/login/login.component.ts     |  3 +-
 .../updatePassword.component.ts               | 33 ++++++++++++++-----
 src/frontend/lang/lang-en.ts                  |  1 +
 src/frontend/lang/lang-fr.ts                  |  1 +
 src/frontend/lang/lang-nl.ts                  |  1 +
 src/frontend/service/app.guard.ts             | 18 +++++++++-
 .../service/auth-interceptor.service.ts       |  4 +++
 7 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/src/frontend/app/login/login.component.ts b/src/frontend/app/login/login.component.ts
index 9b77b51aaae..025448b5e22 100644
--- a/src/frontend/app/login/login.component.ts
+++ b/src/frontend/app/login/login.component.ts
@@ -42,6 +42,7 @@ export class LoginComponent implements OnInit {
     ) { }
 
     ngOnInit(): void {
+
         this.headerService.hideSideBar = true;
         this.loginForm = this.formBuilder.group({
             login: [null, Validators.required],
@@ -92,8 +93,6 @@ export class LoginComponent implements OnInit {
                     this.notify.error(this.lang.accountSuspended);
                 } else if (err.error.errors === 'Account Locked') {
                     this.notify.error(this.lang.accountLocked + ' ' + this.timeLimit.transform(err.error.date));
-                } else if (err.error.errors === 'User must change his password') {
-                    this.router.navigate(['/update-password']);
                 } else {
                     this.notify.handleSoftErrors(err);
                 }
diff --git a/src/frontend/app/login/updatePassword/updatePassword.component.ts b/src/frontend/app/login/updatePassword/updatePassword.component.ts
index df647e1be94..42fba18e7a9 100644
--- a/src/frontend/app/login/updatePassword/updatePassword.component.ts
+++ b/src/frontend/app/login/updatePassword/updatePassword.component.ts
@@ -4,6 +4,7 @@ import { Router, ActivatedRoute } from '@angular/router';
 import { NotificationService } from '../../notification.service';
 import { LANG } from '../../translate.component';
 import { finalize } from 'rxjs/operators';
+import { AuthService } from '../../../service/auth.service';
 
 @Component({
     templateUrl: 'updatePassword.component.html',
@@ -45,20 +46,36 @@ export class UpdatePasswordComponent implements OnInit {
     otherRuleText = '';
 
 
-    constructor(private router: Router, private route: ActivatedRoute, public http: HttpClient, public notificationService: NotificationService) {
+    constructor(
+        private router: Router,
+        private route: ActivatedRoute,
+        public http: HttpClient,
+        public notificationService: NotificationService,
+        private authService: AuthService,
+    ) { }
+
+    ngOnInit(): void {
         this.route.queryParams
             .subscribe(params => {
-                this.token = params.token;
+                if (typeof params['token'] !== 'undefined') {
+                    this.token = params.token;
+                    this.notificationService.success(this.lang.mustChangePassword);
+                    this.getPassRules();
+                } else if (this.authService.getToken() !== null) {
+                    this.token = this.authService.getToken();
+                    this.notificationService.success(this.lang.mustChangePassword);
+                    this.getPassRules();
+                } else {
+                    this.router.navigate(['/login']);
+                }
             });
     }
 
-    ngOnInit(): void {
-        this.getPassRules();
-    }
-
     updatePassword() {
         this.labelButton = this.lang.emailSendInProgress;
         this.loading = true;
+        this.token = this.authService.getToken();
+        console.log(this.token);
 
         this.http.put('../rest/password', { 'token': this.token, 'password': this.password.newPassword })
             .pipe(
@@ -72,7 +89,7 @@ export class UpdatePasswordComponent implements OnInit {
                 this.notificationService.success(this.lang.passwordChanged);
                 this.router.navigate(['/login']);
             }, (err: any) => {
-                this.notificationService.error(this.lang[err.error.lang]);
+                this.notificationService.handleSoftErrors(err);
             });
     }
 
@@ -162,6 +179,6 @@ export class UpdatePasswordComponent implements OnInit {
     }
 
     cancel() {
-        this.router.navigate(['/login']);
+        this.authService.logout();
     }
 }
diff --git a/src/frontend/lang/lang-en.ts b/src/frontend/lang/lang-en.ts
index a03033edb65..27eb96e580f 100755
--- a/src/frontend/lang/lang-en.ts
+++ b/src/frontend/lang/lang-en.ts
@@ -1672,4 +1672,5 @@ export const LANG_EN = {
     "accountSuspended": "Your user account has been suspended",
     "accountLocked": "Too many connections attemps. Retry in",
     "modelUsedByResources": "This model is used by resources, you can't delete it.",
+    "mustChangePassword": "Please, you must change your password.",
 };
diff --git a/src/frontend/lang/lang-fr.ts b/src/frontend/lang/lang-fr.ts
index 64d47ddc9e9..d3a07e6ec3b 100755
--- a/src/frontend/lang/lang-fr.ts
+++ b/src/frontend/lang/lang-fr.ts
@@ -1672,4 +1672,5 @@ export const LANG_FR = {
     "accountSuspended": "Votre compte utilisateur a été suspendu",
     "accountLocked": "Nombre de tentatives de connexion dépassée. Réessayez dans",
     "modelUsedByResources": "Le modèle est utilisé par des courriers, vous ne pouvez pas le supprimer.",
+    "mustChangePassword": "Vous êtes invité à changer votre mot de passe.",
 };
diff --git a/src/frontend/lang/lang-nl.ts b/src/frontend/lang/lang-nl.ts
index c2619987494..4c4b2945bdc 100755
--- a/src/frontend/lang/lang-nl.ts
+++ b/src/frontend/lang/lang-nl.ts
@@ -1656,4 +1656,5 @@ export const LANG_NL = {
     "accountSuspended": "Your user account has been suspended", //_TO_TRANSLATE
     "accountLocked": "Too many connections attemps. Retry in", //_TO_TRANSLATE
     "modelUsedByResources": "This model is used by resources, you can't delete it.", //_TO_TRANSLATE
+    "mustChangePassword": "Please, you must change your password.", //_TO_TRANSLATE
 };
diff --git a/src/frontend/service/app.guard.ts b/src/frontend/service/app.guard.ts
index f0dd4d69984..b664432dc78 100644
--- a/src/frontend/service/app.guard.ts
+++ b/src/frontend/service/app.guard.ts
@@ -25,7 +25,7 @@ export class AppGuard implements CanActivate {
     ) { }
 
     canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
-        console.log('guard');
+        console.log('guard : ' + route.url.join('/'));
 
         this.headerService.resetSideNavSelection();
 
@@ -72,6 +72,14 @@ export class AppGuard implements CanActivate {
                                 return true;
                             }
 
+                        }),
+                        catchError((err: any) => {
+                            console.log(err);
+                            if (err.error.errors === 'User must change his password') {
+                                return this.router.navigate(['/update-password']);
+                            } else {
+                                return of(false);
+                            }
                         })
                     );
             } else {
@@ -152,6 +160,14 @@ export class AppGuard implements CanActivate {
                             return true;
                         }
                     }),
+                    catchError((err: any) => {
+                        console.log(err);
+                        if (err.error.errors === 'User must change his password') {
+                            return this.router.navigate(['/update-password']);
+                        } else {
+                            return of(false);
+                        }
+                    })
                 );
 
         }
diff --git a/src/frontend/service/auth-interceptor.service.ts b/src/frontend/service/auth-interceptor.service.ts
index 41d54ce2184..486d6659c93 100644
--- a/src/frontend/service/auth-interceptor.service.ts
+++ b/src/frontend/service/auth-interceptor.service.ts
@@ -6,6 +6,7 @@ import { NotificationService } from '../app/notification.service';
 import { AuthService } from './auth.service';
 import { Observable } from 'rxjs/internal/Observable';
 import { of } from 'rxjs/internal/observable/of';
+import { Router } from '@angular/router';
 
 @Injectable()
 export class AuthInterceptor implements HttpInterceptor {
@@ -34,6 +35,7 @@ export class AuthInterceptor implements HttpInterceptor {
     ];
     constructor(
         public http: HttpClient,
+        private router: Router,
         public notificationService: NotificationService,
         public authService: AuthService
     ) { }
@@ -111,6 +113,8 @@ export class AuthInterceptor implements HttpInterceptor {
                                 return of(false);
                             })
                         );
+                    } else if (error.status === 405) {
+                        return this.router.navigate(['/update-password']);
                     } else {
                         return next.handle(request);
                     }
-- 
GitLab