Commit 8ce12a45 authored by Lukas Burgey's avatar Lukas Burgey

The whole API now uses id's for identification

parent 231b0bd8
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
(ngSubmit)="userService.login(selectedIdP)" #loginForm="ngForm"> (ngSubmit)="userService.login(selectedIdP)" #loginForm="ngForm">
<mat-form-field> <mat-form-field>
<mat-select name="idp" required [(ngModel)]="selectedIdP"> <mat-select name="idp" required [(ngModel)]="selectedIdP">
<mat-option *ngFor="let idp of idps" [value]="idp.id"> <mat-option *ngFor="let idp of idps" [value]="idp">
{{ idp.name }} {{ idp.name }}
</mat-option> </mat-option>
</mat-select> </mat-select>
......
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DialogService } from '../dialog.service'; import { DialogService } from '../dialog.service';
import { UserService } from '../user.service'; import { UserService } from '../user.service';
import { AllAuthInfo, IdP } from '../types/types.module';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
...@@ -11,23 +11,18 @@ import { UserService } from '../user.service'; ...@@ -11,23 +11,18 @@ import { UserService } from '../user.service';
}) })
export class LoginComponent { export class LoginComponent {
private profileDialog; private profileDialog;
public idps: any[]; public idps: IdP[];
public selectedIdP: number; public selectedIdP: IdP;
constructor( constructor(
public userService: UserService, public userService: UserService,
public dialog: DialogService, public dialog: DialogService,
public http: HttpClient,
) { ) {
this.selectedIdP = this.userService.getIdPPreference(); this.userService.getIdPPreference().subscribe(
(allAuthInfo: AllAuthInfo) => {
this.userService.getAuthInfo().subscribe( this.idps = allAuthInfo.idps;
data => { this.selectedIdP = allAuthInfo.selected;
this.idps = data['idps'];
if (!this.selectedIdP) {
this.selectedIdP = data['default'];
} }
} );
);
} }
} }
...@@ -36,7 +36,7 @@ export class ServiceComponent implements OnInit { ...@@ -36,7 +36,7 @@ export class ServiceComponent implements OnInit {
if (deployment) { if (deployment) {
return deployment.ssh_keys.some(k => { return deployment.ssh_keys.some(k => {
return k.name === key.name; return k.id === key.id;
}); });
} }
return false; return false;
...@@ -44,9 +44,9 @@ export class ServiceComponent implements OnInit { ...@@ -44,9 +44,9 @@ export class ServiceComponent implements OnInit {
public deploymentChange(key) { public deploymentChange(key) {
if (!this.isDeployed(key)) { if (!this.isDeployed(key)) {
this.userService.addDeployment(this.serviceData.name, key.name); this.userService.addDeployment(this.serviceData, key);
} else { } else {
this.userService.removeDeployment(this.serviceData.name, key.name); this.userService.removeDeployment(this.serviceData, key);
} }
} }
} }
<div style="margin-bottom: 25px;"> <div style="margin-bottom: 25px;">
<mat-table *ngIf="userService.user.ssh_keys" [dataSource]="userService.sshKeyData"> <mat-table *ngIf="userService.user.ssh_keys" [dataSource]="userService.sshKeyData">
<ng-container matColumnDef="name"> <ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef>Name</mat-header-cell> <mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.name}}</mat-cell> <mat-cell *matCellDef="let element">{{ element.name}}</mat-cell>
</ng-container> </ng-container>
<ng-container matColumnDef="key"> <ng-container matColumnDef="key">
<mat-header-cell *matHeaderCellDef>Key</mat-header-cell> <mat-header-cell *matHeaderCellDef>Key</mat-header-cell>
<mat-cell *matCellDef="let element">{{ element.key }}</mat-cell> <mat-cell *matCellDef="let element">{{ element.key }}</mat-cell>
</ng-container> </ng-container>
<ng-container matColumnDef="action"> <ng-container matColumnDef="action">
<mat-header-cell *matHeaderCellDef>Action</mat-header-cell> <mat-header-cell *matHeaderCellDef>Action</mat-header-cell>
<mat-cell *matCellDef="let element"> <mat-cell *matCellDef="let element">
<button mat-icon-button (click)="deleteKey(element.name)"><mat-icon>delete_forever</mat-icon></button> <button mat-icon-button (click)="deleteKey(element)"><mat-icon>delete_forever</mat-icon></button>
</mat-cell> </mat-cell>
</ng-container> </ng-container>
<mat-header-row *matHeaderRowDef="columns"></mat-header-row> <mat-header-row *matHeaderRowDef="columns"></mat-header-row>
<mat-row *matRowDef="let row; columns: columns;"></mat-row> <mat-row *matRowDef="let row; columns: columns;"></mat-row>
</mat-table> </mat-table>
<p *ngIf="!userService.user.ssh_keys"> <p *ngIf="!userService.user.ssh_keys">
You have no keys uploaded yet. You have no uploaded keys.
</p> </p>
</div> </div>
<div *ngIf="!upload"> <div *ngIf="!upload">
......
...@@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core'; ...@@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { MatTableDataSource } from '@angular/material'; import { MatTableDataSource } from '@angular/material';
import { UserService } from '../user.service'; import { UserService } from '../user.service';
import { SSHKey, NewSSHKey } from '../types/types.module';
@Component({ @Component({
selector: 'app-ssh-keys', selector: 'app-ssh-keys',
...@@ -26,12 +27,16 @@ export class SshKeysComponent implements OnInit { ...@@ -26,12 +27,16 @@ export class SshKeysComponent implements OnInit {
} }
public uploadKey() { public uploadKey() {
this.userService.addSshKey({name: this.newKeyName, key: this.newKeyKey}); const newKey: NewSSHKey = {
name: this.newKeyName,
key: this.newKeyKey,
};
this.userService.addSshKey(newKey);
this.ngOnInit(); this.ngOnInit();
} }
public deleteKey(name: string) { public deleteKey(key: SSHKey) {
this.userService.removeSshKey(name); this.userService.removeSshKey(key);
this.ngOnInit(); this.ngOnInit();
} }
} }
......
...@@ -2,8 +2,8 @@ import { NgModule } from '@angular/core'; ...@@ -2,8 +2,8 @@ import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
interface IdP { export interface IdP {
id: string; id: number;
name: string; name: string;
} }
...@@ -12,17 +12,28 @@ export interface AuthInfo { ...@@ -12,17 +12,28 @@ export interface AuthInfo {
default: number; default: number;
} }
export interface AllAuthInfo {
idps: IdP[];
selected: IdP;
}
interface Group { interface Group {
name: string; name: string;
} }
interface SSHKey { export interface NewSSHKey {
id: string; name: string;
key: string;
}
export interface SSHKey {
id: number;
name: string; name: string;
key: string; key: string;
} }
export interface Service { export interface Service {
id: number;
name: string; name: string;
} }
...@@ -38,6 +49,11 @@ export interface User { ...@@ -38,6 +49,11 @@ export interface User {
deployments: Deployment[]; deployments: Deployment[];
} }
interface StateAPI {
services: Service[];
logged_in: boolean;
}
export interface UserState { export interface UserState {
services: Service[]; services: Service[];
logged_in: boolean; logged_in: boolean;
......
...@@ -11,7 +11,7 @@ import { MatTableDataSource } from '@angular/material'; ...@@ -11,7 +11,7 @@ import { MatTableDataSource } from '@angular/material';
import { SnackBarService } from './snackbar.service'; import { SnackBarService } from './snackbar.service';
import { environment } from '../environments/environment'; import { environment } from '../environments/environment';
import { User, Service, AuthInfo, UserState } from './types/types.module'; import { IdP, User, Service, AuthInfo, AllAuthInfo, UserState, SSHKey, NewSSHKey } from './types/types.module';
@Injectable() @Injectable()
...@@ -21,7 +21,6 @@ export class UserService { ...@@ -21,7 +21,6 @@ export class UserService {
public sshKeyData: MatTableDataSource<any>; public sshKeyData: MatTableDataSource<any>;
public userInfoData: MatTableDataSource<any>; public userInfoData: MatTableDataSource<any>;
public services: Service[]; public services: Service[];
public authInfo: AuthInfo;
constructor( constructor(
...@@ -32,22 +31,34 @@ export class UserService { ...@@ -32,22 +31,34 @@ export class UserService {
this.update(); this.update();
} }
public logState() { public setIdPPreference(idp: IdP) {
console.log( this.cookieService.set(environment.idpCookieName, String(idp.id));
this.user,
);
} }
public setIdPPreference(idpId: number) { public getIdPPreference(): Observable<AllAuthInfo> {
this.cookieService.set(environment.idpCookieName, String(idpId)); let idpID = Number(this.cookieService.get(environment.idpCookieName));
}
public getIdPPreference(): number { return this.http.get('/backend/auth/v1/info/').map(
return Number(this.cookieService.get(environment.idpCookieName)); (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,
};
}
);
} }
public updateData(data: any) { public updateData(data: any) {
if (data['error']) { if (data['error']) {
this.snackBar.open(data['error']); this.snackBar.open(data['error']);
} }
...@@ -83,20 +94,6 @@ export class UserService { ...@@ -83,20 +94,6 @@ export class UserService {
this.sshKeyData = null; this.sshKeyData = null;
this.userInfoData = 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() { public updateState() {
...@@ -117,8 +114,8 @@ export class UserService { ...@@ -117,8 +114,8 @@ export class UserService {
this.updateState(); this.updateState();
} }
public login(idpId: number) { public login(idp: IdP) {
this.setIdPPreference(idpId); this.setIdPPreference(idp);
window.location.href = 'https://hdf-portal.data.kit.edu/backend/auth/v1/request'; window.location.href = 'https://hdf-portal.data.kit.edu/backend/auth/v1/request';
} }
...@@ -130,7 +127,7 @@ export class UserService { ...@@ -130,7 +127,7 @@ export class UserService {
); );
} }
public addSshKey(key: Object) { public addSshKey(key: NewSSHKey) {
const body = { const body = {
'type': 'add', 'type': 'add',
'key': key, 'key': key,
...@@ -147,10 +144,10 @@ export class UserService { ...@@ -147,10 +144,10 @@ export class UserService {
); );
} }
public removeSshKey(id: string) { public removeSshKey(key: SSHKey) {
const body = { const body = {
'type': 'remove', 'type': 'remove',
'id': id, 'id': key.id,
}; };
return this.http.post('/backend/api/sshkey/', body).subscribe( return this.http.post('/backend/api/sshkey/', body).subscribe(
(data) => { (data) => {
...@@ -164,38 +161,39 @@ export class UserService { ...@@ -164,38 +161,39 @@ export class UserService {
); );
} }
public addDeployment(serviceName: string, keyName: string) { public addDeployment(service: Service, key: SSHKey) {
const body = { const body = {
'type': 'add', 'type': 'add',
'key': keyName, 'key': key.id,
'service': serviceName, 'service': service.id,
}; };
return this.http.post('/backend/api/deployments/', body).subscribe( return this.http.post('/backend/api/deployments/', body).subscribe(
(data) => { (data) => {
this.snackBar.open('Deployed key ' + keyName); this.snackBar.open('Deployed key ' + key.name);
this.updateData(data); this.updateData(data);
}, },
(err) => { (err) => {
this.snackBar.open('Error deploying key ' + keyName); this.snackBar.open('Error deploying key ' + key.name);
console.log(err); console.log(err);
this.update(); this.update();
} }
); );
} }
public removeDeployment(serviceName: string, keyName: string) { public removeDeployment(service: Service, key: SSHKey) {
const body = { const body = {
'type': 'remove', 'type': 'remove',
'key': keyName, 'key': key.id,
'service': serviceName, 'service': service.id,
}; };
return this.http.post('/backend/api/deployments/', body).subscribe( return this.http.post('/backend/api/deployments/', body).subscribe(
(data) => { (data) => {
this.snackBar.open('Withdrew key ' + keyName); this.snackBar.open('Withdrew key ' + key.name);
this.updateData(data); this.updateData(data);
}, },
(err) => { (err) => {
this.snackBar.open('Error withdrawing key ' + keyName); this.snackBar.open('Error withdrawing key ' + key.name);
console.log(err); console.log(err);
this.update(); this.update();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment