diff --git a/src/app/idp.service.ts b/src/app/idp.service.ts index 750b25992aefa60a5ba8debee49cdacaeee83a2e..908e19e4b0274d5de844eac517a08c722b7054fe 100644 --- a/src/app/idp.service.ts +++ b/src/app/idp.service.ts @@ -27,7 +27,7 @@ export class IdpService { this.initializeDataService(); } - initializeDataService() { + private initializeDataService() { if (!this.idps$) { this.idps$ = > new BehaviorSubject(new Array()); } @@ -37,7 +37,7 @@ export class IdpService { this.apiCall(); } - apiCall(): void { + private apiCall(): void { // initialize the subject with data this.http.get('/backend/auth/v1/info').subscribe( (info: AuthInfo) => { @@ -63,66 +63,19 @@ export class IdpService { } - handleError(error: any): Observable { - return Observable.of(null); + private getIdPPreference(): number { + return Number(this.cookieService.get(environment.idpCookieName)); } - subscribeIdps(): Observable { + public subscribeIdps(): Observable { return this.idps$.asObservable(); } - subscribeSelectedIdp(): Observable { + public subscribeSelectedIdp(): Observable { return this.selectedIdp$.asObservable(); } - private setIdPPreference(idp: IdP) { + public setIdPPreference(idp: IdP) { this.cookieService.set(environment.idpCookieName, String(idp.id)); } - - public getIdPPreference(): number { - return Number(this.cookieService.get(environment.idpCookieName)); - } - - /* - public getIdPs(): Observable { - return this.http.get('/backend/auth/v1/info').map( - (authInfo: t.AuthInfo) => { - return authInfo; - } - ).catch( - (error: any) => { - this.errorHandler(error); - return Observable.of(null); - } - ); - } - - public getIdPPreference(): Observable { - let idpID = Number(this.cookieService.get(environment.idpCookieName)); - - return this.idps.map( - (authInfo: t.AuthInfo) => { - let selected = authInfo.idps[0]; - - if (!idpID) { - idpID = authInfo.default; - } - for (const idp of authInfo.idps) { - if (idp.id === idpID) { - selected = idp; - } - } - return { - idps: authInfo.idps, - selected: selected, - }; - } - ).catch( - (error: any) => { - this.errorHandler(error); - return Observable.of(null); - } - ); - } - */ } diff --git a/src/app/user.service.ts b/src/app/user.service.ts index 693596888fb5dbe6966ff481b7ecab2fd8d0e08d..3c0f3af34406a2d757445e8242de312eeeb7305b 100644 --- a/src/app/user.service.ts +++ b/src/app/user.service.ts @@ -3,12 +3,14 @@ import {HttpClient, HttpErrorResponse} from '@angular/common/http'; import {MatTableDataSource} from '@angular/material'; import {Observable} from 'rxjs/Observable'; +import {BehaviorSubject} from 'rxjs/BehaviorSubject'; import {CookieService} from 'ngx-cookie-service'; import {StompConfig, StompRService} from '@stomp/ng2-stompjs'; import {Message} from '@stomp/stompjs'; import {SnackBarService} from './snackbar.service'; +import {IdpService} from './idp.service'; import * as t from './types/types.module'; @@ -17,7 +19,10 @@ export class UserService { private _loggedIn: boolean = false; public user: t.User; + user$ = > new BehaviorSubject(new Object); + public userState: t.UserState; + userState$ = > new BehaviorSubject(new Object); public messages: string[] = []; // local copy of services @@ -25,26 +30,44 @@ export class UserService { public groupMap: Map = new Map(); public groupsParsed: boolean = false; + // TODO put in preferences cookie + autoLogin = true; + constructor( public cookieService: CookieService, public http: HttpClient, public snackBar: SnackBarService, + public idpService: IdpService, private _stompService: StompRService, ) { this.update(); - /* - this.idps = this.getIdPs(); - - // if there is only one IdP immediately use it to login - this.idps.subscribe( - (authInfo: t.AuthInfo) => { - if (authInfo.idps && authInfo.idps.length == 1) { - this.login(authInfo.idps[0]); + + // AUTO LOGIN + this.subscribeUserState().subscribe( + (state: t.UserState) => { + console.log(JSON.stringify(state)); + + if (!state) { + this.idpService.subscribeIdps().subscribe( + (idps: t.IdP[]) => { + if (idps.length === 1 && this.autoLogin) { + this.login(idps[0]); + } + } + ); } } ); - */ + } + + // DATA SERVICE API + public subscribeUserState(): Observable { + return this.userState$.asObservable(); + } + + public subscribeUser(): Observable { + return this.user$.asObservable(); } // PRIVATE API @@ -105,6 +128,7 @@ export class UserService { private updateUser(newUser: t.User) { this.user = newUser; + this.user$.next(this.user); } private updateServices(newServices: t.Service[]){ @@ -122,19 +146,6 @@ export class UserService { } } - private _getServices(groupName: string) : t.Service[] { - let services: t.Service[] = []; - return this.services.filter( - (service: t.Service) => { - return service.groups.some( - (group : t.Group) => { - return group.name === groupName; - } - ); - } - ); - } - private updateUserState(newState: t.UserState) { // did a login occur? let login = (!this._loggedIn && newState); @@ -155,6 +166,7 @@ export class UserService { } this.userState = newState; + this.userState$.next(this.userState); } private _logout() { @@ -164,6 +176,7 @@ export class UserService { this.services = []; this.user = null; this.userState = null; + this.userState$.complete(); } private stateAPIUpdate(update: t.StateAPIResult) { @@ -181,6 +194,29 @@ export class UserService { } } + private _getServices(groupName: string) : t.Service[] { + let services: t.Service[] = []; + return this.services.filter( + (service: t.Service) => { + return service.groups.some( + (group : t.Group) => { + return group.name === groupName; + } + ); + } + ); + } + + private getDeploymentByGroup(group: t.Group): t.Deployment | undefined { + let dep = this.userState.deployments.find( + (d: t.Deployment) => { + return d.group === group.id; + } + ); + return dep + } + + // PUBLIC API public serviceDescription(service: t.Service): string { if (service.description != "") { @@ -207,15 +243,6 @@ export class UserService { return s } - private getDeploymentByGroup(group: t.Group): t.Deployment | undefined { - let dep = this.userState.deployments.find( - (d: t.Deployment) => { - return d.group === group.id; - } - ); - return dep - } - public getGroups(): t.Group[] { if (this.user.groups) { return this.user.groups.sort( @@ -265,13 +292,12 @@ export class UserService { } public errorHandler(error: any): void { - if (error.status === 500) { - this.snackBar.open('Server Error'); - } + console.log(error); + this.snackBar.open('Error'); } public login(idp: t.IdP) { - //this.setIdPPreference(idp); + this.idpService.setIdPPreference(idp); if (!this.loggedIn()) { window.location.href = '/backend/auth/v1/request'; @@ -280,11 +306,9 @@ export class UserService { public logout() { this.http.post('/backend/auth/v1/logout', {}).subscribe( - (data: t.StateAPIResult) => { - this.stateAPIUpdate(data); - }, + (data: t.StateAPIResult) => this.stateAPIUpdate(data), (err) => { - console.log(err); + this.errorHandler(err); this._logout(); } );