Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds filter on the front end for users #42

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion authorizer-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"bootstrap": "^4.1.3",
"core-js": "^2.5.4",
"font-awesome": "^4.7.0",
"rxjs": "~6.2.0",
"rxjs": "^6.5.4",
"zone.js": "~0.8.26"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource} from '@angular/material';
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatPaginator, MatSort, MatTableDataSource } from '@angular/material';

import { RestSourceUser } from '../../models/rest-source-user.model';
import { RestSourceUserService } from '../../services/rest-source-user.service';

Expand All @@ -9,7 +10,7 @@ import { RestSourceUserService } from '../../services/rest-source-user.service';
styleUrls: ['./rest-source-user-list.component.css']
})
export class RestSourceUserListComponent implements OnInit, AfterViewInit {

displayedColumns = ['id', 'projectId', 'userId', 'sourceId', 'startDate',
'endDate', 'externalUserId', 'authorized', 'edit', 'delete'];

Expand All @@ -22,7 +23,7 @@ export class RestSourceUserListComponent implements OnInit, AfterViewInit {

dataSource: MatTableDataSource<RestSourceUser>;

constructor(private restSourceUserService: RestSourceUserService) { }
constructor(private restSourceUserService: RestSourceUserService) {}

ngOnInit() {
this.loadAllRestSourceUsers();
Expand All @@ -45,19 +46,20 @@ export class RestSourceUserListComponent implements OnInit, AfterViewInit {
}

private loadAllRestSourceUsers() {
this.restSourceUserService.getAllUsers().subscribe((data: any) => {
this.restSourceUsers = data.users;
this.restSourceUserService.getAllUsersFromAssignedProjects().subscribe(
(users: any) => {
this.restSourceUsers = users;
this.dataSource.data = this.restSourceUsers;
},
() => {
this.errorMessage = 'Cannot load registered users!';
});
}
);
}

removeDevice(restSourceUser: RestSourceUser) {
this.restSourceUserService.deleteUser(restSourceUser.id).subscribe(() => {
this.loadAllRestSourceUsers();
});
}

}
1 change: 1 addition & 0 deletions authorizer-app/src/app/models/auth.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface User {
username: string;
name: string;
roles: string[];
projects?: string[];
}
33 changes: 26 additions & 7 deletions authorizer-app/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,31 @@ export class AuthService {

constructor(private http: HttpClient) {}

static basicCredentials(user: string, password: string): string {
return 'Basic ' + btoa(`${user}:${password}`);
}

static getToken(): string {
return localStorage.getItem(storageItems.token);
}

static getUser(): User {
getUser(): User {
const user = localStorage.getItem(storageItems.user);
return JSON.parse(user);
}

static basicCredentials(user: string, password: string): string {
return 'Basic ' + btoa(`${user}:${password}`);
}

getAuthData(): AuthData {
const token = AuthService.getToken();
const user = AuthService.getUser();
const user = this.getUser();
return { token, user };
}

setAuthData({ token, user }) {
localStorage.setItem(storageItems.token, token);
this.setUser(user);
}

setUser(user) {
localStorage.setItem(storageItems.user, JSON.stringify(user));
}

Expand All @@ -44,7 +48,10 @@ export class AuthService {
}

login(code) {
return this.authenticateUser(code).pipe(map(res => this.setAuthData(res)));
return this.authenticateUser(code).pipe(
map(res => this.setAuthData(res)),
map(() => this.getProjectsAssignedToUser(this.getUser()))
);
}

authenticateUser(code) {
Expand Down Expand Up @@ -84,4 +91,16 @@ export class AuthService {
.set('redirect_uri', window.location.href.split('?')[0])
.set('code', code);
}

getProjectsAssignedToUser(user: User) {
return this.http
.get<any[]>(`${environment.API_URI}/users/${user.username}/projects`)
.subscribe(projects =>
this.setUser(
Object.assign({}, user, {
projects: projects.map(p => p.projectName)
})
)
);
}
}
32 changes: 21 additions & 11 deletions authorizer-app/src/app/services/rest-source-user.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {Observable} from 'rxjs/internal/Observable';
import {RestSourceUser} from '../models/rest-source-user.model';
import {environment} from '../../environments/environment';
import { HttpClient, HttpParams } from '@angular/common/http';
import { map } from 'rxjs/operators';

import { AuthService } from './auth.service';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { RestSourceUser } from '../models/rest-source-user.model';
import { environment } from '../../environments/environment';

@Injectable({
providedIn: 'root'
})
export class RestSourceUserService {

private serviceUrl = environment.BACKEND_BASE_URL + '/users';

constructor(private http: HttpClient) {
}
constructor(private http: HttpClient, private authService: AuthService) {}

getAllUsers(): Observable<RestSourceUser[]> {
return this.http.get<RestSourceUser[]>(this.serviceUrl);
return this.http.get(this.serviceUrl).pipe(map((res: any) => res.users));
}

getAllUsersByProjectIds(projectIds: string[]): Observable<RestSourceUser[]> {
return this.getAllUsers().pipe(
map(res => res.filter(user => projectIds.includes(user.projectId)))
);
}

getAllUsersFromAssignedProjects() {
const projects = this.authService.getUser().projects;
return this.getAllUsersByProjectIds(projects);
}

updateUser(sourceUser: RestSourceUser): Observable<any> {
Expand All @@ -38,6 +50,4 @@ export class RestSourceUserService {
deleteUser(userId: string): Observable<any> {
return this.http.delete(this.serviceUrl + '/' + userId);
}


}
3 changes: 2 additions & 1 deletion authorizer-app/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export const environment = {
scope:
'SOURCETYPE.READ PROJECT.READ SOURCE.READ SUBJECT.READ MEASUREMENT.READ'
},
AUTH_URI: 'http://localhost:8080/oauth'
AUTH_URI: 'http://localhost:8080/oauth',
API_URI: 'http://localhost:8080/api'
};
3 changes: 2 additions & 1 deletion authorizer-app/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const environment = {
scope:
'SOURCETYPE.READ PROJECT.READ SOURCE.READ SUBJECT.READ MEASUREMENT.READ'
},
AUTH_URI: 'http://localhost:8080/oauth'
AUTH_URI: 'http://localhost:8080/oauth',
API_URI: 'http://localhost:8080/api'
};

/*
Expand Down
27 changes: 15 additions & 12 deletions authorizer-app/src/index.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AuthorizerApp</title>
<base href="/rest-sources/authorizer/">
<head>
<meta charset="utf-8" />
<title>AuthorizerApp</title>
<base href="/" />

<meta name="viewport" content="width=deviceUser-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<app-root></app-root>
</body>
<meta name="viewport" content="width=deviceUser-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<link
href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet"
/>
</head>
<body>
<app-root></app-root>
</body>
</html>