]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: redirect to original URL after successful login 36834/head
authorAvan Thakkar <athakkar@redhat.com>
Wed, 12 Aug 2020 09:22:22 +0000 (14:52 +0530)
committerAvan Thakkar <athakkar@redhat.com>
Mon, 28 Sep 2020 09:34:05 +0000 (15:04 +0530)
Fixes: https://tracker.ceph.com/issues/45067
Signed-off-by: Avan Thakkar <athakkar@redhat.com>
(cherry picked from commit b5359a155c6ff7e92ab6c6ed16f7ff9fc76c76ea)
(cherry picked from commit 330c75cb511d61306a8841973f2b18e17893c555)

Conflicts:
src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-guard.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.ts
  - Adapted changes in these files to nautilus code.

src/pybind/mgr/dashboard/frontend/src/app/core/auth/login/login.component.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-guard.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-guard.service.ts

index 108663be985fd358398525c8670b8398bb779de3..6eee431b957d8a993959663a6c6e58612821d3c5 100644 (file)
@@ -1,5 +1,7 @@
 import { Component, OnInit } from '@angular/core';
-import { Router } from '@angular/router';
+import { ActivatedRoute, Router } from '@angular/router';
+
+import * as _ from 'lodash';
 
 import { BsModalService } from 'ngx-bootstrap/modal';
 
@@ -15,11 +17,13 @@ import { AuthStorageService } from '../../../shared/services/auth-storage.servic
 export class LoginComponent implements OnInit {
   model = new Credentials();
   isLoginActive = false;
+  returnUrl: string;
 
   constructor(
     private authService: AuthService,
     private authStorageService: AuthStorageService,
     private bsModalService: BsModalService,
+    private route: ActivatedRoute,
     private router: Router
   ) {}
 
@@ -57,7 +61,8 @@ export class LoginComponent implements OnInit {
 
   login() {
     this.authService.login(this.model).then(() => {
-      this.router.navigate(['']);
+      const url = _.get(this.route.snapshot.queryParams, 'returnUrl', '/');
+      this.router.navigate([url]);
     });
   }
 }
index f23d7dc267009ac99dc4a2b18960c9b415e331a9..796b42b4373ae5a4e6ffb00c77e4ebf6f20aec22 100644 (file)
@@ -1,6 +1,6 @@
 import { Component, NgZone } from '@angular/core';
 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
-import { Router, Routes } from '@angular/router';
+import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, Routes } from '@angular/router';
 import { RouterTestingModule } from '@angular/router/testing';
 
 import { configureTestBed } from '../../../testing/unit-test-helper';
@@ -11,6 +11,8 @@ describe('AuthGuardService', () => {
   let service: AuthGuardService;
   let authStorageService: AuthStorageService;
   let ngZone: NgZone;
+  let route: ActivatedRouteSnapshot;
+  let state: RouterStateSnapshot;
 
   @Component({ selector: 'cd-login', template: '' })
   class LoginComponent {}
@@ -34,16 +36,19 @@ describe('AuthGuardService', () => {
   });
 
   it('should allow the user if loggedIn', () => {
+    route = null;
+    state = { url: '/', root: null };
     spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
-    expect(service.canActivate()).toBe(true);
+    expect(service.canActivate(route, state)).toBe(true);
   });
 
   it('should prevent user if not loggedIn and redirect to login page', fakeAsync(() => {
     const router = TestBed.get(Router);
+    state = { url: '/pool', root: null };
     ngZone.run(() => {
-      expect(service.canActivate()).toBe(false);
+      expect(service.canActivate(route, state)).toBe(false);
     });
     tick();
-    expect(router.url).toBe('/login');
+    expect(router.url).toBe('/login?returnUrl=%2Fpool');
   }));
 });
index 7e11d9a2d033804488e687e5504b884ede4bfb0d..61c06c81d2bcd9fc8b383a2adfecf835fd6fa656 100644 (file)
@@ -1,5 +1,11 @@
 import { Injectable } from '@angular/core';
-import { CanActivate, CanActivateChild, Router } from '@angular/router';
+import {
+  ActivatedRouteSnapshot,
+  CanActivate,
+  CanActivateChild,
+  Router,
+  RouterStateSnapshot
+} from '@angular/router';
 
 import { AuthStorageService } from './auth-storage.service';
 
@@ -9,15 +15,15 @@ import { AuthStorageService } from './auth-storage.service';
 export class AuthGuardService implements CanActivate, CanActivateChild {
   constructor(private router: Router, private authStorageService: AuthStorageService) {}
 
-  canActivate() {
+  canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
     if (this.authStorageService.isLoggedIn()) {
       return true;
     }
-    this.router.navigate(['/login']);
+    this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
     return false;
   }
 
-  canActivateChild(): boolean {
-    return this.canActivate();
+  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
+    return this.canActivate(childRoute, state);
   }
 }