import { Component , OnInit } from '@angular/core' ; import { VulnsService } from 'src/app/services/vulns.service' ; import { HostVulnsService } from 'src/app/services/hostVulns.service' ; import { MissionsService } from 'src/app/services/missions.service' ; import { ActivatedRoute , Router } from '@angular/router' ; import { NgForm } from '@angular/forms' ; import { Locale } from 'src/app/storage/Locale' ; import { ImpactsService } from 'src/app/services/impacts.service' ; import { MatSnackBar } from '@angular/material/snack-bar' ; import { MissionRouter } from 'src/app/router/MissionRouter' ; import { ImpactModelApplication } from 'src/app/model/Impact' ; import { VulnModelApplication } from 'src/app/model/Vuln' ; import { HostsService } from 'src/app/services/hosts.service' ; import { HostModelApplication } from 'src/app/model/Host' ; import { VulnRouter } from 'src/app/router/VulnRouter' ; import { MediaObjectsService } from 'src/app/services/mediaObjects.service' ; @ Component ( { selector : 'app-add-vulns-to-host-external' , templateUrl : './add-vulns-to-host-external.component.html' , styleUrls : [ './add-vulns-to-host-external.component.scss' ] , } ) export class AddVulnsToHostExternalComponent implements OnInit { public vulns = [ ] ; public impacts = [ ] ; public selectedVuln : string | null = null ; public selectedImpact : string | null = null ; public currentStateUser = '' ; public host : HostModelApplication ; public durationInSeconds = 4 ; public missionId : string ; public image : FormData = null ; constructor ( private vulnsService , private mediaObjectsService , private hostsService , private hostVulnsService , private activatedRoute , private impactService , private _snackBar , private missionServices , private router ) { } openSnackBar ( message ) { this . _snackBar . open ( message , '' , { duration : this . durationInSeconds * 1000 , } ) ; } ngOnInit ( ) { this . missionId = this . activatedRoute . snapshot . params . id ; this . hostsService . getDataById ( this . activatedRoute . snapshot . params . targetHost ) . then ( ( host ) => ( this . host = host ) ) ; this . loadVulns ( ) ; this . loadImpact ( ) ; } loadVulns ( ) { this . vulnsService . getData ( ) . then ( ( { data } ) => { const locale = new Locale ( ) . get ( ) ; this . vulns = data . map ( ( e ) => { const { translations } = e ; return { name : ( translations [ locale . toString ( ) ] ?? translations . en ?? translations [ Object . keys ( translations ) [ 0 ] ] ) . name , value : e [ '@id' ] , } ; } ) ; } ) ; } loadImpact ( ) { this . impactService . getData ( ) . then ( ( { data } ) => { this . impacts = data . map ( ( e ) => { return { name : e . name , value : e [ '@id' ] , } ; } ) ; } ) ; } onSubmit ( form ) { const data = { ... form . value , vuln : this . selectedVuln , host : this . host [ '@id' ] , impact : this . selectedImpact , currentState : this . currentStateUser , } ; const callback = ( body ) => this . hostVulnsService . insert ( body ) . then ( ( ) => { this . openSnackBar ( 'vulnerability added' ) ; this . router . navigateByUrl ( MissionRouter . redirectToShow ( this . missionId ) ) ; } , ( err ) => { this . openSnackBar ( 'Error : ' + err . error [ 'hydra:description' ] ) ; } ) ; if ( this . image ) { this . mediaObjectsService . insert ( this . image ) . then ( ( { [ '@id' ] : id } ) => { data . image = id ; callback ( data ) ; } ) ; } else { callback ( data ) ; } } Vulns ( value ) { this . selectedVuln = value ; } Impacts ( value ) { this . selectedImpact = value ; } createVuln ( ) { this . router . navigateByUrl ( VulnRouter . redirectToCreate ( ) ) ; } storeImage ( inputElement ) { const formdata = new FormData ( ) ; formdata . append ( 'file' , inputElement . files [ 0 ] ) ; this . image = formdata ; } }