user.service.ts 5.59 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import { Injectable } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
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;
}

27 28 29 30 31
interface Deployment {
  service: Service;
  ssh_keys: SSHKey[];
}

Lukas Burgey's avatar
Lukas Burgey committed
32 33 34 35 36 37
interface User {
  sub: string;
  email: string;
  username: string;
  groups: Group[];
  ssh_keys: SSHKey[];
38
  deployments: Deployment[];
Lukas Burgey's avatar
Lukas Burgey committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

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<any>;
  public userInfoData: MatTableDataSource<any>;
64
  public services: Service[];
Lukas Burgey's avatar
Lukas Burgey committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129


  constructor(
    public router: Router,
    public activatedRoute: ActivatedRoute,
    public cookieService: CookieService,
    public http: HttpClient,
    public snackBar: SnackBarService,
  ) {
    this.errorStatus().subscribe(
      (operational) => {
        if (operational) {
          this.update();
        }
      }
    );
  }

  public logState() {
    console.log(
      'loggedIn', this.loggedIn,
      'state', this.state,
      'user', this.user,
      'sshKeyData', this.sshKeyData,
      'userInfoData', this.userInfoData,
    );
  }

  public errorStatus(): Observable<boolean> {
    return this.activatedRoute.params.map(
      (params) => {
        if ('error' in params) {
          this.snackBar.open('Backend error: ' + params['error']);
          return false;
        } else {
          return true;
        }
      }
    );
  }

  public backendOperational(): Observable<boolean> {
    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');
        }
      }
    );
  }

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  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();
  }

Lukas Burgey's avatar
Lukas Burgey committed
164 165 166 167 168
  public update() {
    this.http
      .get('/backend/api/state/')
      .subscribe(
        (data: StateAPIUser) => {
169
          this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
        },
        (err: HttpErrorResponse) => {
          console.log('Error', err);
          this.snackBar.open('Error receiving data from the server');
        }
      );
  }

  public logout() {
    this.http.get('/backend/auth_logout/').subscribe(
      data => {
        this.update();
        this.router.navigate(['']);
      }
    );
  }

  public addSshKey(key: Object) {
    const body = {
      'type': 'add',
      'key': key,
    };
    return this.http.post('/backend/api/sshkey/', body).subscribe(
      data => {
194
        this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
195 196 197 198
      },
      (err) => {
        this.snackBar.open('Error uploading key');
        console.log(err);
199
        this.update();
Lukas Burgey's avatar
Lukas Burgey committed
200 201 202 203 204 205 206 207 208 209 210
      }
    );
  }

  public removeSshKey(name: string) {
    const body = {
      'type': 'remove',
      'name': name,
    };
    return this.http.post('/backend/api/sshkey/', body).subscribe(
      (data) => {
211
        this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
212 213 214 215
      },
      (err) => {
        this.snackBar.open('Error deleting key');
        console.log(err);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        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();
Lukas Burgey's avatar
Lukas Burgey committed
255 256 257 258
      }
    );
  }
}