Commit 03c7b036 authored by Lukas Burgey's avatar Lukas Burgey

Refactor parts of the user.service towards BehaviorSubject

parent 3b963418
...@@ -27,7 +27,7 @@ export class IdpService { ...@@ -27,7 +27,7 @@ export class IdpService {
this.initializeDataService(); this.initializeDataService();
} }
initializeDataService() { private initializeDataService() {
if (!this.idps$) { if (!this.idps$) {
this.idps$ = <BehaviorSubject<IdP[]>> new BehaviorSubject(new Array<IdP>()); this.idps$ = <BehaviorSubject<IdP[]>> new BehaviorSubject(new Array<IdP>());
} }
...@@ -37,7 +37,7 @@ export class IdpService { ...@@ -37,7 +37,7 @@ export class IdpService {
this.apiCall(); this.apiCall();
} }
apiCall(): void { private apiCall(): void {
// initialize the subject with data // initialize the subject with data
this.http.get('/backend/auth/v1/info').subscribe( this.http.get('/backend/auth/v1/info').subscribe(
(info: AuthInfo) => { (info: AuthInfo) => {
...@@ -63,66 +63,19 @@ export class IdpService { ...@@ -63,66 +63,19 @@ export class IdpService {
} }
handleError(error: any): Observable<AuthInfo> { private getIdPPreference(): number {
return Observable.of(null); return Number(this.cookieService.get(environment.idpCookieName));
} }
subscribeIdps(): Observable<IdP[]> { public subscribeIdps(): Observable<IdP[]> {
return this.idps$.asObservable(); return this.idps$.asObservable();
} }
subscribeSelectedIdp(): Observable<IdP> { public subscribeSelectedIdp(): Observable<IdP> {
return this.selectedIdp$.asObservable(); return this.selectedIdp$.asObservable();
} }
private setIdPPreference(idp: IdP) { public setIdPPreference(idp: IdP) {
this.cookieService.set(environment.idpCookieName, String(idp.id)); this.cookieService.set(environment.idpCookieName, String(idp.id));
} }
public getIdPPreference(): number {
return Number(this.cookieService.get(environment.idpCookieName));
}
/*
public getIdPs(): Observable<t.AuthInfo> {
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<t.AllAuthInfo> {
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);
}
);
}
*/
} }
...@@ -3,12 +3,14 @@ import {HttpClient, HttpErrorResponse} from '@angular/common/http'; ...@@ -3,12 +3,14 @@ import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {MatTableDataSource} from '@angular/material'; import {MatTableDataSource} from '@angular/material';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {CookieService} from 'ngx-cookie-service'; import {CookieService} from 'ngx-cookie-service';
import {StompConfig, StompRService} from '@stomp/ng2-stompjs'; import {StompConfig, StompRService} from '@stomp/ng2-stompjs';
import {Message} from '@stomp/stompjs'; import {Message} from '@stomp/stompjs';
import {SnackBarService} from './snackbar.service'; import {SnackBarService} from './snackbar.service';
import {IdpService} from './idp.service';
import * as t from './types/types.module'; import * as t from './types/types.module';
...@@ -17,7 +19,10 @@ export class UserService { ...@@ -17,7 +19,10 @@ export class UserService {
private _loggedIn: boolean = false; private _loggedIn: boolean = false;
public user: t.User; public user: t.User;
user$ = <BehaviorSubject<t.User>> new BehaviorSubject(new Object);
public userState: t.UserState; public userState: t.UserState;
userState$ = <BehaviorSubject<t.UserState>> new BehaviorSubject(new Object);
public messages: string[] = []; public messages: string[] = [];
// local copy of services // local copy of services
...@@ -25,26 +30,44 @@ export class UserService { ...@@ -25,26 +30,44 @@ export class UserService {
public groupMap: Map<string, t.Service[]> = new Map(); public groupMap: Map<string, t.Service[]> = new Map();
public groupsParsed: boolean = false; public groupsParsed: boolean = false;
// TODO put in preferences cookie
autoLogin = true;
constructor( constructor(
public cookieService: CookieService, public cookieService: CookieService,
public http: HttpClient, public http: HttpClient,
public snackBar: SnackBarService, public snackBar: SnackBarService,
public idpService: IdpService,
private _stompService: StompRService, private _stompService: StompRService,
) { ) {
this.update(); this.update();
/*
this.idps = this.getIdPs(); // AUTO LOGIN
this.subscribeUserState().subscribe(
// if there is only one IdP immediately use it to login (state: t.UserState) => {
this.idps.subscribe( console.log(JSON.stringify(state));
(authInfo: t.AuthInfo) => {
if (authInfo.idps && authInfo.idps.length == 1) { if (!state) {
this.login(authInfo.idps[0]); this.idpService.subscribeIdps().subscribe(
(idps: t.IdP[]) => {
if (idps.length === 1 && this.autoLogin) {
this.login(idps[0]);
}
}
);
} }
} }
); );
*/ }
// DATA SERVICE API
public subscribeUserState(): Observable<t.UserState> {
return this.userState$.asObservable();
}
public subscribeUser(): Observable<t.User> {
return this.user$.asObservable();
} }
// PRIVATE API // PRIVATE API
...@@ -105,6 +128,7 @@ export class UserService { ...@@ -105,6 +128,7 @@ export class UserService {
private updateUser(newUser: t.User) { private updateUser(newUser: t.User) {
this.user = newUser; this.user = newUser;
this.user$.next(this.user);
} }
private updateServices(newServices: t.Service[]){ private updateServices(newServices: t.Service[]){
...@@ -122,19 +146,6 @@ export class UserService { ...@@ -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) { private updateUserState(newState: t.UserState) {
// did a login occur? // did a login occur?
let login = (!this._loggedIn && newState); let login = (!this._loggedIn && newState);
...@@ -155,6 +166,7 @@ export class UserService { ...@@ -155,6 +166,7 @@ export class UserService {
} }
this.userState = newState; this.userState = newState;
this.userState$.next(this.userState);
} }
private _logout() { private _logout() {
...@@ -164,6 +176,7 @@ export class UserService { ...@@ -164,6 +176,7 @@ export class UserService {
this.services = []; this.services = [];
this.user = null; this.user = null;
this.userState = null; this.userState = null;
this.userState$.complete();
} }
private stateAPIUpdate(update: t.StateAPIResult) { private stateAPIUpdate(update: t.StateAPIResult) {
...@@ -181,6 +194,29 @@ export class UserService { ...@@ -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 API
public serviceDescription(service: t.Service): string { public serviceDescription(service: t.Service): string {
if (service.description != "") { if (service.description != "") {
...@@ -207,15 +243,6 @@ export class UserService { ...@@ -207,15 +243,6 @@ export class UserService {
return s 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[] { public getGroups(): t.Group[] {
if (this.user.groups) { if (this.user.groups) {
return this.user.groups.sort( return this.user.groups.sort(
...@@ -265,13 +292,12 @@ export class UserService { ...@@ -265,13 +292,12 @@ export class UserService {
} }
public errorHandler(error: any): void { public errorHandler(error: any): void {
if (error.status === 500) { console.log(error);
this.snackBar.open('Server Error'); this.snackBar.open('Error');
}
} }
public login(idp: t.IdP) { public login(idp: t.IdP) {
//this.setIdPPreference(idp); this.idpService.setIdPPreference(idp);
if (!this.loggedIn()) { if (!this.loggedIn()) {
window.location.href = '/backend/auth/v1/request'; window.location.href = '/backend/auth/v1/request';
...@@ -280,11 +306,9 @@ export class UserService { ...@@ -280,11 +306,9 @@ export class UserService {
public logout() { public logout() {
this.http.post('/backend/auth/v1/logout', {}).subscribe( this.http.post('/backend/auth/v1/logout', {}).subscribe(
(data: t.StateAPIResult) => { (data: t.StateAPIResult) => this.stateAPIUpdate(data),
this.stateAPIUpdate(data);
},
(err) => { (err) => {
console.log(err); this.errorHandler(err);
this._logout(); this._logout();
} }
); );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment