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 {
this.initializeDataService();
}
initializeDataService() {
private initializeDataService() {
if (!this.idps$) {
this.idps$ = <BehaviorSubject<IdP[]>> new BehaviorSubject(new Array<IdP>());
}
......@@ -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<AuthInfo> {
return Observable.of(null);
private getIdPPreference(): number {
return Number(this.cookieService.get(environment.idpCookieName));
}
subscribeIdps(): Observable<IdP[]> {
public subscribeIdps(): Observable<IdP[]> {
return this.idps$.asObservable();
}
subscribeSelectedIdp(): Observable<IdP> {
public subscribeSelectedIdp(): Observable<IdP> {
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<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';
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$ = <BehaviorSubject<t.User>> new BehaviorSubject(new Object);
public userState: t.UserState;
userState$ = <BehaviorSubject<t.UserState>> new BehaviorSubject(new Object);
public messages: string[] = [];
// local copy of services
......@@ -25,26 +30,44 @@ export class UserService {
public groupMap: Map<string, t.Service[]> = 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<t.UserState> {
return this.userState$.asObservable();
}
public subscribeUser(): Observable<t.User> {
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();
}
);
......
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