-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
59 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { CreateUserGuard } from './create-user-guard.service'; | ||
|
||
describe('CreateUserGuardService', () => { | ||
let service: CreateUserGuard; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(CreateUserGuard); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { AuthService } from '../auth.service'; | ||
import { Router } from '@angular/router'; | ||
import { RoleService } from '../role.service'; | ||
import { USER_PERMISSION } from './permissions'; | ||
import { ResourceAccess } from './resource-access-guard.service'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class CreateUserGuard extends ResourceAccess{ | ||
|
||
constructor(auth : AuthService, router : Router, roleService : RoleService) { | ||
super(auth, router, roleService, USER_PERMISSION.write, '/users'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { RouterStateSnapshot, Router, ActivatedRouteSnapshot } from '@angular/router'; | ||
import { UsersService } from '../users.service'; | ||
import { map, catchError } from 'rxjs/operators'; | ||
import { AuthService } from '../auth.service'; | ||
import { Router, RouterStateSnapshot } from '@angular/router'; | ||
import { RoleService } from '../role.service'; | ||
import { USER_PERMISSION } from './permissions'; | ||
import { ResourceAccess } from './resource-access-guard.service'; | ||
import { of } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class EditUserGuard extends ResourceAccess{ | ||
export class EditUserGuard { | ||
|
||
constructor(auth : AuthService, router : Router, roleService : RoleService) { | ||
super(auth, router, roleService, USER_PERMISSION.write, '/users'); | ||
constructor(private router : Router, private userService : UsersService, private authService : AuthService) { } | ||
|
||
canActivate(route : ActivatedRouteSnapshot, state : RouterStateSnapshot) { | ||
let role = this.authService.userLoggedIn().role.roleString; | ||
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)) | ||
return true; | ||
}), catchError(err => { | ||
this.router.navigateByUrl('/dashboard'); | ||
return of(false); | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters