import { Component , OnDestroy , OnInit } from '@angular/core' ; import { ReplaySubject } from 'rxjs' ; import { FormArray , FormControl , FormGroup } from '@angular/forms' ; import { ToastService } from '../../layout/toast/toast.service' ; import { ProgressSpinnerService } from '../../layout/progress/progress-spinner.service' ; import { takeUntil } from 'rxjs/operators' ; import { AuthService } from '../../auth/services/auth.service' ; import { ActivatedRoute , Data } from '@angular/router' ; import { AuthProvider } from '../../auth/models/providers' ; import { AuthSettings } from '../../auth/models/settings' ; import { GroupService } from '../../management/services/group.service' ; import { Group } from '../../management/models/group' ; import { APIGetMultiResponse } from '../../services/models/api-response' ; import { CollectionParameters } from '../../services/models/api-parameter' ; @ Component ( { selector : 'cmdb-auth-settings' , templateUrl : './auth-settings.component.html' , styleUrls : [ './auth-settings.component.scss' ] } ) export class AuthSettingsComponent implements OnInit , OnDestroy { private private subscriber : ReplaySubject < void > = new ReplaySubject < void > ( ) ; public authConfigForm : FormGroup ; public authSettings : AuthSettings ; public installedProviderList : Array < AuthProvider > ; public groups : Array < Group > = [ ] ; public constructor ( private authSettingsService , private activateRoute , private groupService , private spinner , private toast ) { this . authConfigForm = new FormGroup ( { _id : new FormControl ( 'auth' ) , enable_external : new FormControl ( false ) , token_lifetime : new FormControl ( null ) , providers : new FormArray ( [ ] ) } ) ; this . activateRoute . data . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( data ) => { this . installedProviderList = data . providers ; } ) ; } public ngOnInit ( ) { this . authSettingsService . getSettings ( ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( settings ) => { this . authSettings = settings ; this . authConfigForm . patchValue ( this . authSettings ) ; } ) ; const groupAPIParameters = { filter : undefined , limit : 0 , sort : 'public_id' , order : 1 , page : 1 } ; this . groupService . getGroups ( groupAPIParameters ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( apiResponse ) => { this . groups = apiResponse . results as Array < Group > ; } , ( error ) => this . toast . error ( error ) ) ; } public get providersArray ( ) : FormArray { return this . authConfigForm . get ( 'providers' ) as FormArray ; } public onSave ( ) { if ( this . authConfigForm . valid ) { this . authSettingsService . postSettings ( this . authConfigForm . getRawValue ( ) ) . pipe ( takeUntil ( this . subscriber ) ) . subscribe ( ( ) => { this . toast . success ( 'Authentication config was updated!' ) ; } ) ; } } public ngOnDestroy ( ) { this . subscriber . next ( ) ; this . subscriber . complete ( ) ; } }