import { Component , OnInit , OnDestroy } from '@angular/core' ; import { Subscription } from "rxjs" ; import { Organization } from "../../services/api-services/models/organization.class" ; import { OrganizationService } from "../../services/api-services/organization.service" ; import { Router , ActivatedRoute } from "@angular/router" ; import { WsTitleService } from "../../services/ui-services/ws-title.service" ; import { WsBreadcrumbsService } from "../../services/ui-services/ws-breadcrumbs.service" ; import { fadeInOut } from "../../animations" ; import { OrgDetailsViewstate } from "../../services/ui-services/models/viewstates/org-details-viewstate.class" ; import { WsViewstateService } from "../../services/ui-services/ws-viewstate.service" ; import { QueryFilter } from "../../services/api-services/models/requests/query-filter.class" ; import { QueryOrdering } from "../../services/api-services/models/requests/query-ordering.class" ; import { ManyApiResponse } from "../../services/api-services/models/responses/many-api-response.interface" ; import { Network } from "../../services/api-services/models/network.class" ; import { NetworkService } from "../../services/api-services/models/network-service.interface" ; import { WsNetworkService } from "../../services/api-services/ws-network.service" ; import { WsDialogService } from "../../components/ws-dialogs/ws-dialog.service" ; import { FormGroup } from "@angular/forms" ; import { DomainName } from "../../services/api-services/models/domain-name.class" ; import { WsDomainNameService } from "../../services/api-services/ws-domain-name.service" ; import { OrganizationPermission } from "../../services/api-services/models/organization-permission.class" ; import { AuthService } from "../../services/api-services/auth.service" ; import { NotificationsService } from "angular2-notifications" ; import { PresentationResponse } from "../../services/api-services/models/responses/presentation-response.interface" ; @ Component ( { selector : 'ws-org-details' , templateUrl : './org-details.component.html' , styleUrls : [ './org-details.component.sass' ] , host : { '[@fadeInOut]' : 'true' , 'class' : 'page-swap' , } , animations : [ fadeInOut , ] } ) export class OrgDetailsComponent implements OnInit , OnDestroy { private subscriptions : Subscription [ ] = [ ] ; private _orgUuid : string ; private _organization : Organization ; private _tabIndex : number = 0 ; private _networksCurrentPage : number = 0 ; private _networksQueryFilters : QueryFilter [ ] = [ ] ; private _networksQueryOrdering : QueryOrdering = null ; private _networksSearchTerm : string = null ; private _networks : ManyApiResponse < Network [ ] > ; private networkForm : FormGroup ; private networkFormErrors : any ; private _domainsCurrentPage : number = 0 ; private _domainsQueryFilters : QueryFilter [ ] = [ ] ; private _domainsQueryOrdering : QueryOrdering = null ; private _domainsSearchTerm : string = null ; private _domains : ManyApiResponse < DomainName [ ] > ; private domainNameForm : FormGroup ; private domainFormErrors : any ; private _permissions : OrganizationPermission [ ] ; private userIsAdmin : boolean ; private userUuid : string ; private networkPresentation : PresentationResponse ; private domainPresentation : PresentationResponse ; constructor ( private orgService , private router , private titleService , private breadcrumbService , private route , private viewstateService , private networkService , private dialogService , private domainsService , private authService , private notifyService ) { } ngOnDestroy ( ) { for ( let subscription of this . subscriptions ) { subscription . unsubscribe ( ) ; } } ngOnInit ( ) { this . loadViewstate ( ) ; this . subscriptions . push ( this . route . params . subscribe ( params => this . orgUuid = params [ 'orgId' ] ) ) ; this . subscriptions . push ( this . authService . userUuid . subscribe ( uuid => this . userUuid = uuid ) ) ; this . subscriptions . push ( this . authService . isAdmin . subscribe ( isAdmin => this . userIsAdmin = isAdmin ) ) ; } private addDomainsQueryFilter ( queryFilter ) { let currentFilters = this . domainsQueryFilters . slice ( 0 ) ; currentFilters . push ( queryFilter ) ; this . domainsQueryFilters = currentFilters ; } private addNetworksQueryFilter ( queryFilter ) { let currentFilters = this . networksQueryFilters . slice ( 0 ) ; currentFilters . push ( queryFilter ) ; this . networksQueryFilters = currentFilters ; } private createDomainsQueryFilter ( filterKey , filterValue , filterLabel ) { this . addDomainsQueryFilter ( new QueryFilter ( filterKey , filterValue , filterLabel ) ) ; } private createNetworksQueryFilter ( filterKey , filterValue , filterLabel ) { this . addNetworksQueryFilter ( new QueryFilter ( filterKey , filterValue , filterLabel ) ) ; } private deleteOrganization ( ) { this . orgService . deleteOrganization ( this . orgUuid ) . subscribe ( _ => this . router . navigate ( [ '/organizations/home' ] ) ) ; } private fetchAll ( ) { this . fetchOrganization ( ) ; this . fetchNetworks ( ) ; this . fetchDomains ( ) ; this . fetchPermissions ( ) ; this . fetchNetworkPresentation ( ) ; this . fetchDomainPresentation ( ) ; } private fetchDomainPresentation ( ) { this . domainsService . getPresentation ( ) . subscribe ( presentation => this . domainPresentation = presentation ) ; } private fetchDomains ( ) { this . orgService . getDomainsNew ( this . orgUuid , this . domainsCurrentPage , this . domainsQueryFilters , this . domainsQueryOrdering , this . domainsSearchTerm ) . subscribe ( domains => this . domains = domains ) ; } private fetchOrganization ( ) { this . orgService . getOrganization ( this . orgUuid ) . subscribe ( organization => this . organization = organization , error => { if ( error . status_code == 404 ) { this . router . navigate ( [ '/organizations/home' ] ) ; } } ) ; } private fetchNetworkPresentation ( ) { this . networkService . getPresentation ( ) . subscribe ( presentation => this . networkPresentation = presentation ) ; } private fetchNetworks ( ) { this . orgService . getNetworksNew ( this . orgUuid , this . networksCurrentPage , this . networksQueryFilters , this . networksQueryOrdering , this . networksSearchTerm ) . subscribe ( networks => this . networks = networks ) ; } private fetchPermissions ( ) { this . orgService . getUserPermissions ( this . orgUuid ) . subscribe ( permissions => this . permissions = permissions , error => this . permissions = null ) ; } private loadViewstate ( ) { let viewstate = this . viewstateService . getViewstate ( ) ; if ( viewstate ) { this . _tabIndex = viewstate . tabIndex ; this . _networksCurrentPage = viewstate . networksCurrentPage ; this . _networksQueryOrdering = viewstate . networksQueryOrdering ; this . _networksQueryFilters = viewstate . networksQueryFilters ; this . _networksSearchTerm = viewstate . networksSearchTerm ; this . _domainsCurrentPage = viewstate . domainsCurrentPage ; this . _domainsQueryOrdering = viewstate . domainsQueryOrdering ; this . _domainsQueryFilters = viewstate . domainsQueryFilters ; this . _domainsSearchTerm = viewstate . domainsSearchTerm ; } } private onAddDomainEnterPressed ( ) { if ( this . domainNameForm . valid ) { this . onNewDomainClicked ( ) ; } } private onAddNetworkEnterPressed ( ) { if ( this . networkForm . valid ) { this . onNewNetworkClicked ( ) ; } } private onCheckOutClicked ( ) { this . orgService . createOrderForOrganization ( this . orgUuid ) . subscribe ( order => { console . log ( 'Got order' ) ; console . log ( order ) ; this . router . navigate ( [ '/organizations/' + this . orgUuid + '/configure-scan/' + order . uuid ] ) ; } ) ; } private onDeleteDomainClicked ( domainUuid ) { this . domainsService . deleteDomainName ( domainUuid ) . subscribe ( _ => { this . fetchDomains ( ) ; this . fetchOrganization ( ) ; } ) ; } private onDeleteNetworkClicked ( networkUuid ) { this . networkService . deleteNetwork ( networkUuid ) . subscribe ( _ => { this . fetchNetworks ( ) ; this . fetchOrganization ( ) ; } ) ; } private onDeleteOrganizationClicked ( ) { this . dialogService . showConfirmDeleteOrganizationDialog ( this . organization ) . subscribe ( result => { if ( result === true ) { this . deleteOrganization ( ) ; } } ) ; } private onDomainsFilterCreated ( queryFilter ) { this . addDomainsQueryFilter ( queryFilter ) ; } private onDomainIncludeToggled ( toggle ) { let domainUuid = toggle [ 0 ] ; let include = toggle [ 1 ] ; this . domainsService . setDomainNameInclusion ( domainUuid , include ) . subscribe ( _ => { this . fetchOrganization ( ) ; this . fetchDomains ( ) ; } ) ; } private onDomainsOrderingChanged ( ordering ) { this . domainsQueryOrdering = ordering ; } private onDomainsPageChanged ( page ) { this . domainsCurrentPage = page ; this . fetchDomains ( ) ; } private onDomainsSearchChanged ( searchTerm ) { this . domainsSearchTerm = searchTerm ; } private onExportDomains ( ) { this . dialogService . showExportDataDialog ( this . orgService . getDomainsUrl ( this . orgUuid ) , this . domainsQueryFilters , this . domainsQueryOrdering , this . domainsSearchTerm , this . domainPresentation . fields ) ; } private onExportNetworks ( ) { this . dialogService . showExportDataDialog ( this . orgService . getNetworksUrl ( this . orgUuid ) , this . networksQueryFilters , this . networksQueryOrdering , this . networksSearchTerm , this . networkPresentation . fields ) ; } private onNetworkIncludeToggled ( toggle ) { let networkUuid = toggle [ 0 ] ; let include = toggle [ 1 ] ; this . networkService . setNetworkInclusion ( networkUuid , include ) . subscribe ( _ => { this . fetchOrganization ( ) ; this . fetchNetworks ( ) ; } ) ; } private onNetworksFilterCreated ( queryFilter ) { this . addNetworksQueryFilter ( queryFilter ) ; } private onNetworksOrderingChanged ( queryOrdering ) { this . networksQueryOrdering = queryOrdering ; } private onNetworksPageChanged ( page ) { this . networksCurrentPage = page ; this . fetchNetworks ( ) ; } private onNetworksSearchChanged ( searchTerm ) { this . networksSearchTerm = searchTerm ; } private onNewDomainClicked ( ) { this . domainFormErrors = null ; this . orgService . createDomainForOrganization ( this . orgUuid , this . domainNameForm . value . name ) . subscribe ( _ => { this . fetchOrganization ( ) ; this . fetchDomains ( ) ; this . domainNameForm . reset ( ) ; } , error => this . domainFormErrors = error ) ; } private onNewNetworkClicked ( ) { this . networkFormErrors = null ; this . orgService . createNetworkForOrganization ( this . orgUuid , this . networkForm . value . name , this . networkForm . value . address , this . networkForm . value . mask_length ) . subscribe ( _ => { this . fetchOrganization ( ) ; this . fetchNetworks ( ) ; this . networkForm . reset ( ) ; } , error => this . networkFormErrors = error ) ; } private onPermissionsChanged ( event ) { this . orgService . updateUserPermissions ( this . organization . uuid , event [ 0 ] . user_uuid , event [ 1 ] ) . subscribe ( permissions => this . permissions = permissions , error => this . notifyService . error ( 'Error' , error . detail ) ) ; } private onUploadDomainClicked ( ) { this . dialogService . showUploadDomainsDialog ( this . organization , true ) . subscribe ( result => { if ( result . new_domains > 0 ) { this . fetchOrganization ( ) ; this . fetchDomains ( ) ; } } ) ; } private onUploadNetworkClicked ( ) { this . dialogService . showUploadNetworksDialog ( this . organization , true ) . subscribe ( result => { if ( result . new_networks > 0 ) { this . fetchOrganization ( ) ; this . fetchNetworks ( ) ; } } ) ; } private onUserAdded ( userEmail ) { this . orgService . addUser ( this . organization . uuid , userEmail ) . subscribe ( permissions => this . permissions = permissions , error => this . notifyService . error ( 'Error' , error . detail ) ) ; } private onUserRemoved ( userUuid ) { this . orgService . removeUser ( this . organization . uuid , userUuid ) . subscribe ( permissions => this . permissions = permissions , error => this . notifyService . error ( 'Error' , error . detail ) ) ; } private onViewOrganizationClicked ( ) { this . router . navigate ( [ '/topography/' + this . orgUuid ] ) ; } private setViewstate ( ) { this . viewstateService . setViewstate ( this . viewstate ) ; } get canUserAdmin ( ) : boolean { if ( this . userIsAdmin ) { return true ; } else if ( this . userPermission ) { return this . userPermission . can_admin ; } else { return false ; } } get canUserScan ( ) : boolean { if ( this . userIsAdmin ) { return true ; } else if ( this . userPermission ) { return this . userPermission . can_scan ; } else { return false ; } } get canUserWrite ( ) : boolean { if ( this . userIsAdmin ) { return true ; } else if ( this . userPermission ) { return this . userPermission . can_write ; } else { return false ; } } get domains ( ) : ManyApiResponse < DomainName [ ] > { return this . _domains ; } set domains ( domains ) { this . _domains = domains ; console . log ( 'Got domains' ) ; console . log ( domains ) ; } get domainsCurrentPage ( ) : number { return this . _domainsCurrentPage ; } set domainsCurrentPage ( newValue ) { this . _domainsCurrentPage = newValue ; this . setViewstate ( ) ; } get domainsQueryFilters ( ) : QueryFilter [ ] { return this . _domainsQueryFilters ; } set domainsQueryFilters ( filters ) { this . _domainsQueryFilters = filters ; this . setViewstate ( ) ; this . fetchDomains ( ) ; } get domainsQueryOrdering ( ) : QueryOrdering { return this . _domainsQueryOrdering ; } set domainsQueryOrdering ( ordering ) { this . _domainsQueryOrdering = ordering ; this . setViewstate ( ) ; this . fetchDomains ( ) ; } get domainsSearchTerm ( ) : string { return this . _domainsSearchTerm ; } set domainsSearchTerm ( term ) { this . _domainsSearchTerm = term ; this . setViewstate ( ) ; this . fetchDomains ( ) ; } get networks ( ) : ManyApiResponse < Network [ ] > { return this . _networks ; } set networks ( networks ) { this . _networks = networks ; console . log ( 'Got networks' ) ; console . log ( networks ) ; } get networksCurrentPage ( ) : number { return this . _networksCurrentPage ; } set networksCurrentPage ( newValue ) { this . _networksCurrentPage = newValue ; this . setViewstate ( ) ; } get networksQueryFilters ( ) : QueryFilter [ ] { return this . _networksQueryFilters ; } set networksQueryFilters ( queryFilters ) { this . _networksQueryFilters = queryFilters ; this . setViewstate ( ) ; this . fetchNetworks ( ) ; } get networksQueryOrdering ( ) : QueryOrdering { return this . _networksQueryOrdering ; } set networksQueryOrdering ( ordering ) { this . _networksQueryOrdering = ordering ; this . setViewstate ( ) ; this . fetchNetworks ( ) ; } get networksSearchTerm ( ) : string { return this . _networksSearchTerm ; } set networksSearchTerm ( newValue ) { this . _networksSearchTerm = newValue ; this . setViewstate ( ) ; this . fetchNetworks ( ) ; } get organization ( ) : Organization { return this . _organization ; } set organization ( newValue ) { this . _organization = newValue ; this . titleService . currentTitle = this . _organization . name ; this . breadcrumbService . setBreadcrumbsForOrganizationDetails ( this . _organization ) ; console . log ( 'got organization' ) ; console . log ( newValue ) ; } get orgUuid ( ) : string { return this . _orgUuid ; } set orgUuid ( newValue ) { this . _orgUuid = newValue ; console . log ( 'Org UUID is ' + newValue ) ; this . fetchAll ( ) ; } get permissions ( ) : OrganizationPermission [ ] { return this . _permissions ; } set permissions ( permissions ) { this . _permissions = permissions ; console . log ( 'Got permissions' ) ; console . log ( permissions ) ; } get scanButtonEnabled ( ) : boolean { if ( ! this . organization ) { return false ; } else if ( ! this . organization . ready_for_scan ) { return false ; } else { return true ; } } get tabIndex ( ) : number { return this . _tabIndex ; } set tabIndex ( newValue ) { this . _tabIndex = newValue ; this . setViewstate ( ) ; } get userPermission ( ) : OrganizationPermission { if ( this . permissions ) { for ( let permission of this . permissions ) { if ( permission . user_uuid == this . userUuid ) { return permission ; } } return null ; } else { return null ; } } get viewstate ( ) : OrgDetailsViewstate { return new OrgDetailsViewstate ( this . networksCurrentPage , this . networksQueryFilters , this . networksQueryOrdering , this . networksSearchTerm , this . domainsCurrentPage , this . domainsQueryFilters , this . domainsQueryOrdering , this . domainsSearchTerm , this . tabIndex ) ; } }