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'; interface Group { name: string; } interface SSHKey { name: string; key: string; } interface Service { name: string; } interface Deployment { service: Service; ssh_keys: SSHKey[]; } interface User { sub: string; email: string; username: string; groups: Group[]; ssh_keys: SSHKey[]; deployments: Deployment[]; } interface State { services: Service[]; } interface StateAPI { state: State; logged_in: boolean; } interface StateAPIUser { state: State; logged_in: boolean; user: User; } @Injectable() export class UserService { public loggedIn = false; public state: State; public user: User; public sshKeyData: MatTableDataSource; public userInfoData: MatTableDataSource; public services: Service[]; constructor( public cookieService: CookieService, public http: HttpClient, public snackBar: SnackBarService, ) { this.update(); } public logState() { console.log( this.state, ); console.log( this.user, ); } public backendOperational(): Observable { return this.http.get('/backend/api/operational').map( (data) => { return data['operational']; }, (err) => { return false; } ); } public login() { this.backendOperational().subscribe( (operational: boolean) => { console.log(operational); if (operational) { window.location.href = 'https://hdf-portal.data.kit.edu/backend/auth'; } else { this.snackBar.open('The backend is currently not operational'); } } ); } public updateData(data: any) { if (!this.loggedIn && data.logged_in) { this.snackBar.open('Logged in'); } this.loggedIn = data.logged_in; if (!this.state) { this.state = data.state; } 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 of ['sub', 'email']) { userInfoList.push({name: key, info: this.user[key]}); } this.userInfoData = new MatTableDataSource(userInfoList); } else { this.sshKeyData = null; this.userInfoData = null; } this.logState(); } public update() { this.http .get('/backend/api/state/') .subscribe( (data: StateAPIUser) => { this.updateData(data); }, (err: HttpErrorResponse) => { console.log('Error', err); this.snackBar.open('Error receiving data from the server'); } ); } public logout() { this.http.post('/backend/auth_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(name: string) { const body = { 'type': 'remove', 'name': name, }; 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(); } ); } }