Skip to content

Commit

Permalink
Modified edit user guard based on managers
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaneej committed Jul 24, 2020
1 parent d9cd8d5 commit b2726f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/app/services/guards/edit-user-guard.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable } from '@angular/core';
import { RouterStateSnapshot, Router, ActivatedRouteSnapshot } from '@angular/router';
import { UsersService } from '../users.service';
import { map, catchError } from 'rxjs/operators';
import { map, catchError, take } from 'rxjs/operators';
import { AuthService } from '../auth.service';
import { USER_PERMISSION } from './permissions';
import { of } from 'rxjs';
import { of, Observable } from 'rxjs';

@Injectable({
providedIn: 'root'
Expand All @@ -13,16 +13,25 @@ export class EditUserGuard {

constructor(private router : Router, private userService : UsersService, private authService : AuthService) { }

canActivate(route : ActivatedRouteSnapshot, state : RouterStateSnapshot) {
let role = this.authService.userLoggedIn().role.roleString;
canActivate(route : ActivatedRouteSnapshot, state : RouterStateSnapshot) : Observable<boolean>{
let loggedInUser = this.authService.userLoggedIn();
let role = loggedInUser.role.roleString;
let userId = loggedInUser.id;
let routeId = parseInt(route.params.id, 10);
if(!USER_PERMISSION.write.includes(role)) return false;
return this.userService.getAll()
.pipe(map((users : any) => users.map(user => user.id )), map(users => {
if(users.includes(routeId))
if(!USER_PERMISSION.write.includes(role) || userId === routeId) return of(false).pipe(take(1));
return this.userService.getManagers(routeId)
.pipe(
take(1),
map((users : any) => {
let managers = users.map(user => user.id);
if(managers.includes(userId))
return true;
else {
this.router.navigate(['/dashboard']);
return false;
}
}), catchError(err => {
this.router.navigateByUrl('/dashboard');
this.router.navigate(['/dashboard']);
return of(false);
}));
}
Expand Down
5 changes: 5 additions & 0 deletions src/app/services/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export class UsersService extends DataService{
return this.http.get(url);
}

getManagers(id) {
let url = 'http://localhost:8080/users/managers/' + id;
return this.http.get(url);
}

changePassword(oldPassword, newPassword) {
return new Promise((resolve, reject) => {
// POST request to server with email and password
Expand Down

0 comments on commit b2726f7

Please sign in to comment.