user.service.ts 4.87 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1 2 3 4 5 6 7 8 9 10 11 12
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';

Lukas Burgey's avatar
Lukas Burgey committed
13
import { environment } from '../environments/environment';
14
import { IdP, User, Service, AuthInfo, AllAuthInfo, UserState, SSHKey, NewSSHKey } from './types/types.module';
Lukas Burgey's avatar
Lukas Burgey committed
15

Lukas Burgey's avatar
Lukas Burgey committed
16 17 18 19 20 21 22

@Injectable()
export class UserService {
  public loggedIn = false;
  public user: User;
  public sshKeyData: MatTableDataSource<any>;
  public userInfoData: MatTableDataSource<any>;
23
  public services: Service[];
Lukas Burgey's avatar
Lukas Burgey committed
24 25 26 27 28 29 30


  constructor(
    public cookieService: CookieService,
    public http: HttpClient,
    public snackBar: SnackBarService,
  ) {
Lukas Burgey's avatar
Lukas Burgey committed
31
    this.update();
Lukas Burgey's avatar
Lukas Burgey committed
32 33
  }

34 35
  public setIdPPreference(idp: IdP) {
    this.cookieService.set(environment.idpCookieName, String(idp.id));
Lukas Burgey's avatar
Lukas Burgey committed
36 37
  }

38 39
  public getIdPPreference(): Observable<AllAuthInfo> {
    let idpID = Number(this.cookieService.get(environment.idpCookieName));
Lukas Burgey's avatar
Lukas Burgey committed
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    return this.http.get('/backend/auth/v1/info/').map(
      (authInfo: AuthInfo) => {
        let selected = authInfo.idps[1];

        if (!idpID) {
          idpID = authInfo.default;
        }
        for (const idp of authInfo.idps) {
          if (idp.id === idpID) {
            selected = idp;
          }
        }
        return {
          idps: authInfo.idps,
          selected: selected,
        };
      }
    );
Lukas Burgey's avatar
Lukas Burgey committed
59 60
  }

61
  public updateData(data: any) {
62 63 64 65
    if (data['error']) {
      this.snackBar.open(data['error']);
    }

66 67 68 69 70
    if (!this.loggedIn && data.logged_in) {
      this.snackBar.open('Logged in');
    }

    this.loggedIn = data.logged_in;
Lukas Burgey's avatar
Lukas Burgey committed
71 72
    if (!this.services) {
      this.services = data.services;
73 74 75 76 77 78 79 80 81 82 83 84 85
    }
    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 = [];
Lukas Burgey's avatar
Lukas Burgey committed
86 87 88
      for (const key in this.user.userinfo) {
        if (this.user.userinfo.hasOwnProperty(key)) {
          userInfoList.push({name: key, info: this.user.userinfo[key]});
Lukas Burgey's avatar
Lukas Burgey committed
89
        }
90 91 92 93 94 95 96
      }
      this.userInfoData = new MatTableDataSource(userInfoList);

    } else {
      this.sshKeyData = null;
      this.userInfoData = null;
    }
Lukas Burgey's avatar
Lukas Burgey committed
97 98 99
  }

  public updateState() {
Lukas Burgey's avatar
Lukas Burgey committed
100 101 102
    this.http
      .get('/backend/api/state/')
      .subscribe(
103
        (data: UserState) => {
104
          this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
105 106 107 108 109 110 111 112
        },
        (err: HttpErrorResponse) => {
          console.log('Error', err);
          this.snackBar.open('Error receiving data from the server');
        }
      );
  }

Lukas Burgey's avatar
Lukas Burgey committed
113 114 115 116
  public update() {
    this.updateState();
  }

117 118
  public login(idp: IdP) {
    this.setIdPPreference(idp);
119 120 121
    window.location.href = 'https://hdf-portal.data.kit.edu/backend/auth/v1/request';
  }

Lukas Burgey's avatar
Lukas Burgey committed
122
  public logout() {
Lukas Burgey's avatar
Lukas Burgey committed
123
    this.http.post('/backend/auth/v1/logout/', {}).subscribe(
Lukas Burgey's avatar
Lukas Burgey committed
124
      data => {
Lukas Burgey's avatar
Lukas Burgey committed
125
        this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
126 127 128 129
      }
    );
  }

130
  public addSshKey(key: NewSSHKey) {
Lukas Burgey's avatar
Lukas Burgey committed
131 132 133 134 135 136
    const body = {
      'type': 'add',
      'key': key,
    };
    return this.http.post('/backend/api/sshkey/', body).subscribe(
      data => {
137
        this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
138 139 140 141
      },
      (err) => {
        this.snackBar.open('Error uploading key');
        console.log(err);
142
        this.update();
Lukas Burgey's avatar
Lukas Burgey committed
143 144 145 146
      }
    );
  }

147
  public removeSshKey(key: SSHKey) {
Lukas Burgey's avatar
Lukas Burgey committed
148 149
    const body = {
      'type': 'remove',
150
      'id': key.id,
Lukas Burgey's avatar
Lukas Burgey committed
151 152 153
    };
    return this.http.post('/backend/api/sshkey/', body).subscribe(
      (data) => {
154
        this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
155 156 157 158
      },
      (err) => {
        this.snackBar.open('Error deleting key');
        console.log(err);
159 160 161 162 163
        this.update();
      }
    );
  }

164
  public addDeployment(service: Service, key: SSHKey) {
165 166
    const body = {
      'type': 'add',
167 168
      'key': key.id,
      'service': service.id,
169 170 171
    };
    return this.http.post('/backend/api/deployments/', body).subscribe(
      (data) => {
172
        this.snackBar.open('Deployed key ' + key.name);
173 174 175
        this.updateData(data);
      },
      (err) => {
176
        this.snackBar.open('Error deploying key ' + key.name);
177 178 179 180 181 182
        console.log(err);
        this.update();
      }
    );
  }

183
  public removeDeployment(service: Service, key: SSHKey) {
184 185
    const body = {
      'type': 'remove',
186 187
      'key': key.id,
      'service':  service.id,
188
    };
189

190 191
    return this.http.post('/backend/api/deployments/', body).subscribe(
      (data) => {
192
        this.snackBar.open('Withdrew key ' + key.name);
193 194 195
        this.updateData(data);
      },
      (err) => {
196
        this.snackBar.open('Error withdrawing key ' + key.name);
197 198
        console.log(err);
        this.update();
Lukas Burgey's avatar
Lukas Burgey committed
199 200 201 202
      }
    );
  }
}