import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; import 'rxjs/add/observable/of'; import { CookieService } from 'ngx-cookie-service'; import { MatTableDataSource } from '@angular/material'; import { SnackBarService } from './snackbar.service'; import { environment } from '../environments/environment'; import { User, Service, AuthInfo, UserState } from './types/types.module'; @Injectable() export class UserService { public loggedIn = false; public user: User; public sshKeyData: MatTableDataSource; public userInfoData: MatTableDataSource; public services: Service[]; public authInfo: AuthInfo; constructor( public cookieService: CookieService, public http: HttpClient, public snackBar: SnackBarService, ) { this.update(); } public logState() { console.log( this.user, ); } public setIdPPreference(idpId: number) { this.cookieService.set(environment.idpCookieName, String(idpId)); } public getIdPPreference(): number { return Number(this.cookieService.get(environment.idpCookieName)); } public updateData(data: any) { if (data['error']) { this.snackBar.open(data['error']); } if (!this.loggedIn && data.logged_in) { this.snackBar.open('Logged in'); } this.loggedIn = data.logged_in; if (!this.services) { this.services = data.services; } this.user = data.user; if (this.user) { // build sshKeyData if ('ssh_keys' in this.user) { this.sshKeyData = new MatTableDataSource(this.user.ssh_keys); } else { this.sshKeyData = null; } // build userInfoData const userInfoList = []; for (const key in this.user.userinfo) { if (this.user.userinfo.hasOwnProperty(key)) { userInfoList.push({name: key, info: this.user.userinfo[key]}); } } this.userInfoData = new MatTableDataSource(userInfoList); } else { this.sshKeyData = null; this.userInfoData = null; } // FIXME remove this.logState(); } public getAuthInfo() { return this.http.get('/backend/auth/v1/info/').map( (data) => { return data; }, (err) => { return {}; } ); } public updateState() { this.http .get('/backend/api/state/') .subscribe( (data: UserState) => { this.updateData(data); }, (err: HttpErrorResponse) => { console.log('Error', err); this.snackBar.open('Error receiving data from the server'); } ); } public update() { this.updateState(); } public login(idpId: number) { this.setIdPPreference(idpId); window.location.href = 'https://hdf-portal.data.kit.edu/backend/auth/v1/request'; } public logout() { this.http.post('/backend/auth/v1/logout/', {}).subscribe( data => { this.updateData(data); } ); } public addSshKey(key: Object) { const body = { 'type': 'add', 'key': key, }; return this.http.post('/backend/api/sshkey/', body).subscribe( data => { this.updateData(data); }, (err) => { this.snackBar.open('Error uploading key'); console.log(err); this.update(); } ); } public removeSshKey(id: string) { const body = { 'type': 'remove', 'id': id, }; return this.http.post('/backend/api/sshkey/', body).subscribe( (data) => { this.updateData(data); }, (err) => { this.snackBar.open('Error deleting key'); console.log(err); this.update(); } ); } public addDeployment(serviceName: string, keyName: string) { const body = { 'type': 'add', 'key': keyName, 'service': serviceName, }; return this.http.post('/backend/api/deployments/', body).subscribe( (data) => { this.snackBar.open('Deployed key ' + keyName); this.updateData(data); }, (err) => { this.snackBar.open('Error deploying key ' + keyName); console.log(err); this.update(); } ); } public removeDeployment(serviceName: string, keyName: string) { const body = { 'type': 'remove', 'key': keyName, 'service': serviceName, }; return this.http.post('/backend/api/deployments/', body).subscribe( (data) => { this.snackBar.open('Withdrew key ' + keyName); this.updateData(data); }, (err) => { this.snackBar.open('Error withdrawing key ' + keyName); console.log(err); this.update(); } ); } }