Login guard added

This commit is contained in:
piotrpekala7 2021-04-29 18:15:22 +02:00
parent 71077413b7
commit 9835c106d8
5 changed files with 76 additions and 28 deletions

View File

@ -53,6 +53,7 @@ import { ListOfSnapshotsComponent } from './components/snapshots/list-of-snapsho
import { SystemStatusComponent } from './components/system-status/system-status.component'; import { SystemStatusComponent } from './components/system-status/system-status.component';
import { WebConsoleFullWindowComponent } from './components/web-console-full-window/web-console-full-window.component'; import { WebConsoleFullWindowComponent } from './components/web-console-full-window/web-console-full-window.component';
import { ConsoleGuard } from './guards/console-guard'; import { ConsoleGuard } from './guards/console-guard';
import { LoginGuard } from './guards/login-guard';
import { DefaultLayoutComponent } from './layouts/default-layout/default-layout.component'; import { DefaultLayoutComponent } from './layouts/default-layout/default-layout.component';
import { ServerResolve } from './resolvers/server-resolve'; import { ServerResolve } from './resolvers/server-resolve';
@ -68,6 +69,7 @@ const routes: Routes = [
{ {
path: 'server/:server_id/projects', path: 'server/:server_id/projects',
component: ProjectsComponent, component: ProjectsComponent,
canActivate: [LoginGuard],
resolve: { server: ServerResolve }, resolve: { server: ServerResolve },
}, },
{ path: 'help', component: HelpComponent }, { path: 'help', component: HelpComponent },
@ -75,120 +77,137 @@ const routes: Routes = [
{ path: 'settings', component: SettingsComponent }, { path: 'settings', component: SettingsComponent },
{ path: 'settings/console', component: ConsoleComponent }, { path: 'settings/console', component: ConsoleComponent },
{ path: 'installed-software', component: InstalledSoftwareComponent }, { path: 'installed-software', component: InstalledSoftwareComponent },
{ path: 'server/:server_id/systemstatus', component: SystemStatusComponent }, { path: 'server/:server_id/systemstatus', component: SystemStatusComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_ip/:server_port/project/:project_id', component: DirectLinkComponent }, { path: 'server/:server_ip/:server_port/project/:project_id', component: DirectLinkComponent, canActivate: [LoginGuard] },
{ {
path: 'server/:server_id/project/:project_id/snapshots', path: 'server/:server_id/project/:project_id/snapshots',
component: ListOfSnapshotsComponent, component: ListOfSnapshotsComponent,
canActivate: [LoginGuard],
resolve: { server: ServerResolve }, resolve: { server: ServerResolve },
}, },
{ path: 'server/:server_id/preferences', component: PreferencesComponent }, { path: 'server/:server_id/preferences', component: PreferencesComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/gns3vm', component: Gns3vmComponent }, { path: 'server/:server_id/preferences/gns3vm', component: Gns3vmComponent, canActivate: [LoginGuard] },
// { path: 'server/:server_id/preferences/general', component: GeneralPreferencesComponent }, // { path: 'server/:server_id/preferences/general', component: GeneralPreferencesComponent },
{ path: 'server/:server_id/preferences/builtin', component: BuiltInPreferencesComponent }, { path: 'server/:server_id/preferences/builtin', component: BuiltInPreferencesComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/builtin/ethernet-hubs', component: EthernetHubsTemplatesComponent }, { path: 'server/:server_id/preferences/builtin/ethernet-hubs', component: EthernetHubsTemplatesComponent, canActivate: [LoginGuard] },
{ {
path: 'server/:server_id/preferences/builtin/ethernet-hubs/addtemplate', path: 'server/:server_id/preferences/builtin/ethernet-hubs/addtemplate',
component: EthernetHubsAddTemplateComponent, component: EthernetHubsAddTemplateComponent,
canActivate: [LoginGuard]
}, },
{ {
path: 'server/:server_id/preferences/builtin/ethernet-hubs/:template_id', path: 'server/:server_id/preferences/builtin/ethernet-hubs/:template_id',
component: EthernetHubsTemplateDetailsComponent, component: EthernetHubsTemplateDetailsComponent,
canActivate: [LoginGuard]
}, },
{ {
path: 'server/:server_id/preferences/builtin/ethernet-switches', path: 'server/:server_id/preferences/builtin/ethernet-switches',
component: EthernetSwitchesTemplatesComponent, component: EthernetSwitchesTemplatesComponent,
canActivate: [LoginGuard]
}, },
{ {
path: 'server/:server_id/preferences/builtin/ethernet-switches/addtemplate', path: 'server/:server_id/preferences/builtin/ethernet-switches/addtemplate',
component: EthernetSwitchesAddTemplateComponent, component: EthernetSwitchesAddTemplateComponent,
canActivate: [LoginGuard]
}, },
{ {
path: 'server/:server_id/preferences/builtin/ethernet-switches/:template_id', path: 'server/:server_id/preferences/builtin/ethernet-switches/:template_id',
component: EthernetSwitchesTemplateDetailsComponent, component: EthernetSwitchesTemplateDetailsComponent,
canActivate: [LoginGuard]
}, },
{ path: 'server/:server_id/preferences/builtin/cloud-nodes', component: CloudNodesTemplatesComponent }, { path: 'server/:server_id/preferences/builtin/cloud-nodes', component: CloudNodesTemplatesComponent, canActivate: [LoginGuard] },
{ {
path: 'server/:server_id/preferences/builtin/cloud-nodes/addtemplate', path: 'server/:server_id/preferences/builtin/cloud-nodes/addtemplate',
component: CloudNodesAddTemplateComponent, component: CloudNodesAddTemplateComponent,
canActivate: [LoginGuard]
}, },
{ {
path: 'server/:server_id/preferences/builtin/cloud-nodes/:template_id', path: 'server/:server_id/preferences/builtin/cloud-nodes/:template_id',
component: CloudNodesTemplateDetailsComponent, component: CloudNodesTemplateDetailsComponent,
canActivate: [LoginGuard]
}, },
//{ path: 'server/:server_id/preferences/dynamips', component: DynamipsPreferencesComponent }, //{ path: 'server/:server_id/preferences/dynamips', component: DynamipsPreferencesComponent },
{ path: 'server/:server_id/preferences/dynamips/templates', component: IosTemplatesComponent }, { path: 'server/:server_id/preferences/dynamips/templates', component: IosTemplatesComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/dynamips/templates/addtemplate', component: AddIosTemplateComponent }, { path: 'server/:server_id/preferences/dynamips/templates/addtemplate', component: AddIosTemplateComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/dynamips/templates/:template_id', component: IosTemplateDetailsComponent }, { path: 'server/:server_id/preferences/dynamips/templates/:template_id', component: IosTemplateDetailsComponent, canActivate: [LoginGuard] },
{ {
path: 'server/:server_id/preferences/dynamips/templates/:template_id/copy', path: 'server/:server_id/preferences/dynamips/templates/:template_id/copy',
component: CopyIosTemplateComponent, component: CopyIosTemplateComponent,
canActivate: [LoginGuard]
}, },
// { path: 'server/:server_id/preferences/qemu', component: QemuPreferencesComponent }, // { path: 'server/:server_id/preferences/qemu', component: QemuPreferencesComponent },
{ path: 'server/:server_id/preferences/qemu/templates', component: QemuVmTemplatesComponent }, { path: 'server/:server_id/preferences/qemu/templates', component: QemuVmTemplatesComponent, canActivate: [LoginGuard] },
{ {
path: 'server/:server_id/preferences/qemu/templates/:template_id/copy', path: 'server/:server_id/preferences/qemu/templates/:template_id/copy',
component: CopyQemuVmTemplateComponent, component: CopyQemuVmTemplateComponent,
canActivate: [LoginGuard]
}, },
{ path: 'server/:server_id/preferences/qemu/templates/:template_id', component: QemuVmTemplateDetailsComponent }, { path: 'server/:server_id/preferences/qemu/templates/:template_id', component: QemuVmTemplateDetailsComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/qemu/addtemplate', component: AddQemuVmTemplateComponent }, { path: 'server/:server_id/preferences/qemu/addtemplate', component: AddQemuVmTemplateComponent, canActivate: [LoginGuard] },
// { path: 'server/:server_id/preferences/vpcs', component: VpcsPreferencesComponent }, // { path: 'server/:server_id/preferences/vpcs', component: VpcsPreferencesComponent },
{ path: 'server/:server_id/preferences/vpcs/templates', component: VpcsTemplatesComponent }, { path: 'server/:server_id/preferences/vpcs/templates', component: VpcsTemplatesComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/vpcs/templates/:template_id', component: VpcsTemplateDetailsComponent }, { path: 'server/:server_id/preferences/vpcs/templates/:template_id', component: VpcsTemplateDetailsComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/vpcs/addtemplate', component: AddVpcsTemplateComponent }, { path: 'server/:server_id/preferences/vpcs/addtemplate', component: AddVpcsTemplateComponent, canActivate: [LoginGuard] },
// { path: 'server/:server_id/preferences/virtualbox', component: VirtualBoxPreferencesComponent }, // { path: 'server/:server_id/preferences/virtualbox', component: VirtualBoxPreferencesComponent },
{ path: 'server/:server_id/preferences/virtualbox/templates', component: VirtualBoxTemplatesComponent }, { path: 'server/:server_id/preferences/virtualbox/templates', component: VirtualBoxTemplatesComponent, canActivate: [LoginGuard] },
{ {
path: 'server/:server_id/preferences/virtualbox/templates/:template_id', path: 'server/:server_id/preferences/virtualbox/templates/:template_id',
component: VirtualBoxTemplateDetailsComponent, component: VirtualBoxTemplateDetailsComponent,
canActivate: [LoginGuard]
}, },
{ path: 'server/:server_id/preferences/virtualbox/addtemplate', component: AddVirtualBoxTemplateComponent }, { path: 'server/:server_id/preferences/virtualbox/addtemplate', component: AddVirtualBoxTemplateComponent, canActivate: [LoginGuard] },
// { path: 'server/:server_id/preferences/vmware', component: VmwarePreferencesComponent }, // { path: 'server/:server_id/preferences/vmware', component: VmwarePreferencesComponent },
{ path: 'server/:server_id/preferences/vmware/templates', component: VmwareTemplatesComponent }, { path: 'server/:server_id/preferences/vmware/templates', component: VmwareTemplatesComponent, canActivate: [LoginGuard] },
{ {
path: 'server/:server_id/preferences/vmware/templates/:template_id', path: 'server/:server_id/preferences/vmware/templates/:template_id',
component: VmwareTemplateDetailsComponent, component: VmwareTemplateDetailsComponent,
canActivate: [LoginGuard]
}, },
{ path: 'server/:server_id/preferences/vmware/addtemplate', component: AddVmwareTemplateComponent }, { path: 'server/:server_id/preferences/vmware/addtemplate', component: AddVmwareTemplateComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/docker/templates', component: DockerTemplatesComponent }, { path: 'server/:server_id/preferences/docker/templates', component: DockerTemplatesComponent, canActivate: [LoginGuard] },
{ {
path: 'server/:server_id/preferences/docker/templates/:template_id', path: 'server/:server_id/preferences/docker/templates/:template_id',
component: DockerTemplateDetailsComponent, component: DockerTemplateDetailsComponent,
canActivate: [LoginGuard]
}, },
{ {
path: 'server/:server_id/preferences/docker/templates/:template_id/copy', path: 'server/:server_id/preferences/docker/templates/:template_id/copy',
component: CopyDockerTemplateComponent, component: CopyDockerTemplateComponent,
canActivate: [LoginGuard]
}, },
{ path: 'server/:server_id/preferences/docker/addtemplate', component: AddDockerTemplateComponent }, { path: 'server/:server_id/preferences/docker/addtemplate', component: AddDockerTemplateComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/iou/templates', component: IouTemplatesComponent }, { path: 'server/:server_id/preferences/iou/templates', component: IouTemplatesComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/iou/templates/:template_id', component: IouTemplateDetailsComponent }, { path: 'server/:server_id/preferences/iou/templates/:template_id', component: IouTemplateDetailsComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/iou/templates/:template_id/copy', component: CopyIouTemplateComponent }, { path: 'server/:server_id/preferences/iou/templates/:template_id/copy', component: CopyIouTemplateComponent, canActivate: [LoginGuard] },
{ path: 'server/:server_id/preferences/iou/addtemplate', component: AddIouTemplateComponent }, { path: 'server/:server_id/preferences/iou/addtemplate', component: AddIouTemplateComponent, canActivate: [LoginGuard] },
], ],
}, },
{ {
path: 'server/:server_id/project/:project_id', path: 'server/:server_id/project/:project_id',
component: ProjectMapComponent, component: ProjectMapComponent,
canActivate: [LoginGuard],
canDeactivate: [ConsoleGuard], canDeactivate: [ConsoleGuard],
}, },
{ {
path: 'server/:server_id/project/:project_id/nodes/:node_id', path: 'server/:server_id/project/:project_id/nodes/:node_id',
component: WebConsoleFullWindowComponent, component: WebConsoleFullWindowComponent,
canActivate: [LoginGuard]
}, },
{ {
path: 'static/web-ui/server/:server_id/project/:project_id/nodes/:node_id', path: 'static/web-ui/server/:server_id/project/:project_id/nodes/:node_id',
component: WebConsoleFullWindowComponent, component: WebConsoleFullWindowComponent,
canActivate: [LoginGuard]
}, },
{ {
path: '**', path: '**',

View File

@ -210,6 +210,7 @@ import { ProjectsFilter } from './filters/projectsFilter.pipe';
import { SearchFilter } from './filters/searchFilter.pipe'; import { SearchFilter } from './filters/searchFilter.pipe';
import { TemplateFilter } from './filters/templateFilter.pipe'; import { TemplateFilter } from './filters/templateFilter.pipe';
import { ConsoleGuard } from './guards/console-guard'; import { ConsoleGuard } from './guards/console-guard';
import { LoginGuard } from './guards/login-guard';
import { ProjectWebServiceHandler } from './handlers/project-web-service-handler'; import { ProjectWebServiceHandler } from './handlers/project-web-service-handler';
import { DefaultLayoutComponent } from './layouts/default-layout/default-layout.component'; import { DefaultLayoutComponent } from './layouts/default-layout/default-layout.component';
import { MATERIAL_IMPORTS } from './material.imports'; import { MATERIAL_IMPORTS } from './material.imports';
@ -548,6 +549,7 @@ import { LoginService } from './services/login.service';
GoogleAnalyticsService, GoogleAnalyticsService,
NodeConsoleService, NodeConsoleService,
ServerResolve, ServerResolve,
LoginGuard,
ConsoleGuard, ConsoleGuard,
Title, Title,
ApplianceService, ApplianceService,

View File

@ -3,7 +3,8 @@
<mat-card class="matCard"> <mat-card class="matCard">
<div class="loginTitle"> <div class="loginTitle">
<div class="loginIcon"> <div class="loginIcon">
<mat-icon class="mat-icon-login" svgIcon="gns3"></mat-icon> <mat-icon class="mat-icon-login" *ngIf="!isLightThemeEnabled" svgIcon="gns3"></mat-icon>
<mat-icon class="mat-icon-login" *ngIf="isLightThemeEnabled" svgIcon="gns3black"></mat-icon>
<div> <div>
<h1>GNS3</h1> <h1>GNS3</h1>
<h6>v{{version}}</h6> <h6>v{{version}}</h6>

View File

@ -9,6 +9,7 @@ import { ToasterService } from '../../services/toaster.service';
import { AuthResponse } from '../../models/authResponse'; import { AuthResponse } from '../../models/authResponse';
import { VersionService } from '../../services/version.service'; import { VersionService } from '../../services/version.service';
import { Version } from '../../models/version'; import { Version } from '../../models/version';
import { ThemeService } from '../../services/theme.service';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
@ -19,6 +20,7 @@ import { Version } from '../../models/version';
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
private server: Server; private server: Server;
public version: string; public version: string;
public isLightThemeEnabled: boolean = false;
loginForm = new FormGroup({ loginForm = new FormGroup({
username: new FormControl('', [Validators.required]), username: new FormControl('', [Validators.required]),
@ -32,7 +34,8 @@ export class LoginComponent implements OnInit {
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private toasterService: ToasterService, private toasterService: ToasterService,
private versionService: VersionService private versionService: VersionService,
private themeService: ThemeService
) {} ) {}
async ngOnInit() { async ngOnInit() {
@ -44,6 +47,10 @@ export class LoginComponent implements OnInit {
this.version = version.version; this.version = version.version;
}); });
}); });
this.themeService.getActualTheme() === 'light'
? (this.isLightThemeEnabled = true)
: (this.isLightThemeEnabled = false);
} }
public login() { public login() {

View File

@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
import { Server } from 'app/models/server';
import { ServerService } from '../services/server.service';
@Injectable()
export class LoginGuard implements CanActivate {
constructor(
private serverService: ServerService
) {}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const server_id = next.paramMap.get('server_id');
return this.serverService.get(parseInt(server_id, 10)).then((server: Server) => {
if (server.authToken) return true;
return false
});
}
}