user.service.ts 5.19 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 14
import { environment } from '../environments/environment';

Lukas Burgey's avatar
Lukas Burgey committed
15 16 17 18 19 20 21 22 23 24 25 26 27
interface Group {
  name: string;
}

interface SSHKey {
  name: string;
  key: string;
}

interface Service {
  name: string;
}

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

Lukas Burgey's avatar
Lukas Burgey committed
33 34
interface User {
  email: string;
Lukas Burgey's avatar
Lukas Burgey committed
35
  userinfo: any;
Lukas Burgey's avatar
Lukas Burgey committed
36
  ssh_keys: SSHKey[];
37
  deployments: Deployment[];
Lukas Burgey's avatar
Lukas Burgey committed
38 39 40
}

interface StateAPI {
Lukas Burgey's avatar
Lukas Burgey committed
41
  services: Service[];
Lukas Burgey's avatar
Lukas Burgey committed
42 43 44 45
  logged_in: boolean;
}

interface StateAPIUser {
Lukas Burgey's avatar
Lukas Burgey committed
46
  services: Service[];
Lukas Burgey's avatar
Lukas Burgey committed
47 48 49 50
  logged_in: boolean;
  user: User;
}

Lukas Burgey's avatar
Lukas Burgey committed
51 52 53 54 55 56 57 58 59 60
interface IdP {
  id: string;
  name: string;
}

interface AuthInfo {
  idps: IdP[];
  default: number;
}

Lukas Burgey's avatar
Lukas Burgey committed
61 62 63 64 65 66 67

@Injectable()
export class UserService {
  public loggedIn = false;
  public user: User;
  public sshKeyData: MatTableDataSource<any>;
  public userInfoData: MatTableDataSource<any>;
68
  public services: Service[];
Lukas Burgey's avatar
Lukas Burgey committed
69
  public authInfo: AuthInfo;
Lukas Burgey's avatar
Lukas Burgey committed
70 71 72 73 74 75 76


  constructor(
    public cookieService: CookieService,
    public http: HttpClient,
    public snackBar: SnackBarService,
  ) {
Lukas Burgey's avatar
Lukas Burgey committed
77
    this.update();
Lukas Burgey's avatar
Lukas Burgey committed
78 79 80
  }

  public logState() {
Lukas Burgey's avatar
Lukas Burgey committed
81 82
    console.log(
      this.user,
Lukas Burgey's avatar
Lukas Burgey committed
83 84 85
    );
  }

86
  public setIdPPreference(idpId: number) {
Lukas Burgey's avatar
Lukas Burgey committed
87
    this.cookieService.set(environment.idpCookieName, String(idpId));
Lukas Burgey's avatar
Lukas Burgey committed
88 89
  }

90
  public getIdPPreference(): number {
Lukas Burgey's avatar
Lukas Burgey committed
91
    return Number(this.cookieService.get(environment.idpCookieName));
Lukas Burgey's avatar
Lukas Burgey committed
92 93
  }

94
  public updateData(data: any) {
95 96 97 98 99

    if (data['error']) {
      this.snackBar.open(data['error']);
    }

100 101 102 103 104
    if (!this.loggedIn && data.logged_in) {
      this.snackBar.open('Logged in');
    }

    this.loggedIn = data.logged_in;
Lukas Burgey's avatar
Lukas Burgey committed
105 106
    if (!this.services) {
      this.services = data.services;
107 108 109 110 111 112 113 114 115 116 117 118 119
    }
    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
120 121 122
      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
123
        }
124 125 126 127 128 129 130
      }
      this.userInfoData = new MatTableDataSource(userInfoList);

    } else {
      this.sshKeyData = null;
      this.userInfoData = null;
    }
Lukas Burgey's avatar
Lukas Burgey committed
131 132

    // this.logState();
133 134
  }

Lukas Burgey's avatar
Lukas Burgey committed
135 136 137 138 139 140 141 142 143 144 145 146
  public getAuthInfo() {
    return this.http.get('/backend/auth/v1/info/').map(
      (data) => {
        return data;
      },
      (err) => {
        return {};
      }
    );
  }

  public updateState() {
Lukas Burgey's avatar
Lukas Burgey committed
147 148 149 150
    this.http
      .get('/backend/api/state/')
      .subscribe(
        (data: StateAPIUser) => {
151
          this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
152 153 154 155 156 157 158 159
        },
        (err: HttpErrorResponse) => {
          console.log('Error', err);
          this.snackBar.open('Error receiving data from the server');
        }
      );
  }

Lukas Burgey's avatar
Lukas Burgey committed
160 161 162 163
  public update() {
    this.updateState();
  }

164 165 166 167 168
  public login(idpId: number) {
    this.setIdPPreference(idpId);
    window.location.href = 'https://hdf-portal.data.kit.edu/backend/auth/v1/request';
  }

Lukas Burgey's avatar
Lukas Burgey committed
169
  public logout() {
Lukas Burgey's avatar
Lukas Burgey committed
170
    this.http.post('/backend/auth/v1/logout/', {}).subscribe(
Lukas Burgey's avatar
Lukas Burgey committed
171
      data => {
Lukas Burgey's avatar
Lukas Burgey committed
172
        this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
173 174 175 176 177 178 179 180 181 182 183
      }
    );
  }

  public addSshKey(key: Object) {
    const body = {
      'type': 'add',
      'key': key,
    };
    return this.http.post('/backend/api/sshkey/', body).subscribe(
      data => {
184
        this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
185 186 187 188
      },
      (err) => {
        this.snackBar.open('Error uploading key');
        console.log(err);
189
        this.update();
Lukas Burgey's avatar
Lukas Burgey committed
190 191 192 193 194 195 196 197 198 199 200
      }
    );
  }

  public removeSshKey(name: string) {
    const body = {
      'type': 'remove',
      'name': name,
    };
    return this.http.post('/backend/api/sshkey/', body).subscribe(
      (data) => {
201
        this.updateData(data);
Lukas Burgey's avatar
Lukas Burgey committed
202 203 204 205
      },
      (err) => {
        this.snackBar.open('Error deleting key');
        console.log(err);
206 207 208 209 210 211 212 213 214 215 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
        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
245 246 247 248
      }
    );
  }
}