From 1d3be1e80b5147ba23cb69e06a1b76274cef6fd3 Mon Sep 17 00:00:00 2001 From: ByronHsu Date: Fri, 4 Dec 2020 00:32:49 +0800 Subject: [PATCH] use canActivate as authguard --- .../notebook/notebook-routing.module.ts | 35 ----------- .../workbench/notebook/notebook.module.ts | 5 +- .../workbench/workbench-routing.module.ts | 60 ++++++++++++------- .../app/pages/workbench/workbench.module.ts | 7 ++- 4 files changed, 44 insertions(+), 63 deletions(-) delete mode 100644 submarine-workbench/workbench-web/src/app/pages/workbench/notebook/notebook-routing.module.ts diff --git a/submarine-workbench/workbench-web/src/app/pages/workbench/notebook/notebook-routing.module.ts b/submarine-workbench/workbench-web/src/app/pages/workbench/notebook/notebook-routing.module.ts deleted file mode 100644 index a4a15a0e9a..0000000000 --- a/submarine-workbench/workbench-web/src/app/pages/workbench/notebook/notebook-routing.module.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { NgModule } from '@angular/core'; -import { RouterModule, Routes } from '@angular/router'; -import { NotebookComponent } from './notebook.component'; - -const routes: Routes = [ - { - path: '', - component: NotebookComponent - } -]; - -@NgModule({ - imports: [RouterModule.forChild(routes)], - exports: [RouterModule] -}) -export class NotebookRoutingModule {} diff --git a/submarine-workbench/workbench-web/src/app/pages/workbench/notebook/notebook.module.ts b/submarine-workbench/workbench-web/src/app/pages/workbench/notebook/notebook.module.ts index 9a99d23063..06996b9a94 100644 --- a/submarine-workbench/workbench-web/src/app/pages/workbench/notebook/notebook.module.ts +++ b/submarine-workbench/workbench-web/src/app/pages/workbench/notebook/notebook.module.ts @@ -20,14 +20,13 @@ import { CommonModule } from '@angular/common'; import { NgModule } from '@angular/core'; import { NotebookComponent } from './notebook.component'; -import { NotebookRoutingModule } from './notebook-routing.module'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { NgZorroAntdModule } from 'ng-zorro-antd'; import { PipeSharedModule } from '@submarine/pipe/pipe-shared.module'; @NgModule({ - exports: [ReactiveFormsModule], declarations: [NotebookComponent], - imports: [CommonModule, NotebookRoutingModule, FormsModule, ReactiveFormsModule, NgZorroAntdModule, PipeSharedModule] + exports: [NotebookComponent], + imports: [CommonModule, FormsModule, ReactiveFormsModule, NgZorroAntdModule, PipeSharedModule] }) export class NotebookModule {} diff --git a/submarine-workbench/workbench-web/src/app/pages/workbench/workbench-routing.module.ts b/submarine-workbench/workbench-web/src/app/pages/workbench/workbench-routing.module.ts index 1d5372e8e7..8d92fa3717 100644 --- a/submarine-workbench/workbench-web/src/app/pages/workbench/workbench-routing.module.ts +++ b/submarine-workbench/workbench-web/src/app/pages/workbench/workbench-routing.module.ts @@ -18,7 +18,7 @@ */ import { NgModule } from '@angular/core'; -import { RouterModule, Routes } from '@angular/router'; +import { ActivatedRouteSnapshot, RouterModule, RouterStateSnapshot, Routes } from '@angular/router'; import { EnvironmentComponent } from '@submarine/pages/workbench/environment/environment.component'; import { ExperimentComponent } from '@submarine/pages/workbench/experiment/experiment.component'; import { WorkbenchComponent } from '@submarine/pages/workbench/workbench.component'; @@ -27,15 +27,9 @@ import { ExperimentInfoComponent } from './experiment/experiment-info/experiment import { HomeComponent } from './home/home.component'; import { InterpreterComponent } from './interpreter/interpreter.component'; import { ModelComponent } from './model/model.component'; +import { NotebookComponent } from './notebook/notebook.component'; import { WorkspaceComponent } from './workspace/workspace.component'; -function disablePage(allRoutes: Routes): Routes { - const disabledList: string[] = ['home', 'data', 'model', 'workspace', 'interpreter']; - allRoutes[0].children[0].redirectTo = 'experiment'; // redirect root page to experiment - allRoutes[0].children = allRoutes[0].children.filter((item) => !disabledList.includes(item.path)); // filter pages which are incomplete - return allRoutes; -} - const routes: Routes = [ { path: '', @@ -44,19 +38,22 @@ const routes: Routes = [ { path: '', pathMatch: 'full', - redirectTo: 'home' + redirectTo: 'experiment' }, { path: 'home', - component: HomeComponent + component: HomeComponent, + canActivate: ['canActivatePage'] }, { path: 'workspace', - component: WorkspaceComponent + component: WorkspaceComponent, + canActivate: ['canActivatePage'] }, { path: 'interpreter', - component: InterpreterComponent + component: InterpreterComponent, + canActivate: ['canActivatePage'] }, { path: 'experiment', @@ -66,33 +63,52 @@ const routes: Routes = [ path: 'info/:id', component: ExperimentInfoComponent } - ] + ], + canActivate: ['canActivatePage'], + canActivateChild: ['canActivatePage'] }, { path: 'environment', - component: EnvironmentComponent + component: EnvironmentComponent, + canActivate: ['canActivatePage'] }, { path: 'data', - component: DataComponent + component: DataComponent, + canActivate: ['canActivatePage'] }, { path: 'model', - component: ModelComponent + component: ModelComponent, + canActivate: ['canActivatePage'] }, { - path: 'manager', - loadChildren: () => import('./manager/manager.module').then((m) => m.ManagerModule) + path: 'notebook', + component: NotebookComponent, + canActivate: ['canActivatePage'], }, { - path: 'notebook', - loadChildren: () => import('./notebook/notebook.module').then((m) => m.NotebookModule) + path: 'manager', + loadChildren: () => import('./manager/manager.module').then((m) => m.ManagerModule), + canActivate: ['canActivatePage'] } ] } ]; @NgModule({ - imports: [RouterModule.forChild(disablePage(routes))] + imports: [RouterModule.forChild(routes)], + providers: [ + { + provide: 'canActivatePage', + useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => { + const disablePaths = ['home', 'data', 'model', 'workspace', 'interpreter']; + let currentPage = state.url.split('/')[2]; + console.log('currentPage', currentPage); + if (disablePaths.includes(currentPage)) return false; + else return true; + } + } + ] }) -export class WorkbenchRoutingModule {} +export class WorkbenchRoutingModule { } diff --git a/submarine-workbench/workbench-web/src/app/pages/workbench/workbench.module.ts b/submarine-workbench/workbench-web/src/app/pages/workbench/workbench.module.ts index af8b64d704..bf1e7480d5 100644 --- a/submarine-workbench/workbench-web/src/app/pages/workbench/workbench.module.ts +++ b/submarine-workbench/workbench-web/src/app/pages/workbench/workbench.module.ts @@ -25,7 +25,6 @@ import { WorkbenchRoutingModule } from '@submarine/pages/workbench/workbench-rou import { PipeSharedModule } from '@submarine/pipe/pipe-shared.module'; import { NgZorroAntdModule } from 'ng-zorro-antd'; import { DataComponent } from './data/data.component'; -import { ExperimentComponent } from './experiment/experiment.component'; import { ExperimentModule } from './experiment/experiment.module'; import { HomeComponent } from './home/home.component'; @@ -35,6 +34,7 @@ import { WorkbenchComponent } from './workbench.component'; import { WorkspaceComponent } from './workspace/workspace.component'; import { WorkspaceModule } from './workspace/workspace.module'; import { EnvironmentComponent } from './environment/environment.component'; +import { NotebookComponent } from './notebook/notebook.component'; @NgModule({ declarations: [ @@ -43,7 +43,8 @@ import { EnvironmentComponent } from './environment/environment.component'; WorkspaceComponent, DataComponent, ModelComponent, - EnvironmentComponent + EnvironmentComponent, + NotebookComponent ], imports: [ CommonModule, @@ -57,4 +58,4 @@ import { EnvironmentComponent } from './environment/environment.component'; PipeSharedModule ] }) -export class WorkbenchModule {} +export class WorkbenchModule { }