Skip to content

Commit

Permalink
Added guard to edit user
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaneej committed Jul 23, 2020
1 parent 25513f4 commit d9cd8d5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 12 deletions.
9 changes: 5 additions & 4 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ import { InterviewCardComponent } from './interview-card/interview-card.componen
import { RescheduleComponent } from './interview-form/reschedule/reschedule.component';
import { FeedbackComponent } from './feedback/feedback.component';
import { FilterComponent } from './dashboard/filter/filter.component';
import { SkillsChipListComponent } from './skills-chip-list/skills-chip-list.component';

import { AuthGuard } from './services/guards/auth-guard.service';
import { ViewUserGuard } from './services/guards/view-user-guard.service';
import { EditUserGuard } from './services/guards/edit-user-guard.service';
import { CreateUserGuard } from './services/guards/create-user-guard.service';
import { ViewCandidateGuard } from './services/guards/view-candidate-guard.service';
import { EditCandidateGuard } from './services/guards/edit-candidate-guard.service';
import { ViewCandidateProfileGuard } from './services/guards/view-candidate-profile-guard.service';
import { CreateCandidateGuard } from './services/guards/create-candidate-guard.service';
import { CreateInterviewGuard } from './services/guards/create-interview-guard.service';
import { SkillsChipListComponent } from './skills-chip-list/skills-chip-list.component';
import { EditUserGuard } from './services/guards/edit-user-guard.service';



Expand Down Expand Up @@ -72,8 +73,8 @@ import { SkillsChipListComponent } from './skills-chip-list/skills-chip-list.com
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: 'users', component: UsersComponent, canActivate: [AuthGuard, ViewUserGuard] },
{ path: 'users/new', component: UserFormComponent, canActivate: [AuthGuard, EditUserGuard] },
{ path: 'users/:id', component: EditUserComponent, canActivate: [AuthGuard] },
{ path: 'users/new', component: UserFormComponent, canActivate: [AuthGuard, CreateUserGuard] },
{ path: 'users/:id', component: EditUserComponent, canActivate: [AuthGuard, EditUserGuard] },
{ path: 'candidates', component: CandidatesComponent, canActivate: [AuthGuard, ViewCandidateGuard] },
{ path: 'candidates/new', component: CandidateFormComponent, canActivate: [AuthGuard, CreateCandidateGuard] },
{ path: 'candidates/:id', component: CandidateProfileComponent, canActivate: [AuthGuard, ViewCandidateProfileGuard] },
Expand Down
2 changes: 1 addition & 1 deletion src/app/dashboard/dashboard.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<div>
<button mat-raised-button (click)="openFilterDialog()">Duration</button>
<mat-button-toggle-group #group="matButtonToggleGroup" [(value)]="statsMode">
<mat-button-toggle value="overall"> Overall </mat-button-toggle>
<mat-button-toggle value="user"> My Stats </mat-button-toggle>
<mat-button-toggle value="overall"> Overall </mat-button-toggle>
</mat-button-toggle-group>
</div>
</mat-card-header>
Expand Down
2 changes: 1 addition & 1 deletion src/app/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class DashboardComponent implements OnInit {
objectKeys = Object.keys;
user;
showStatistics;
statsMode = 'overall';
statsMode = 'user';
showInterviews;
cardStyles = CARD_STYLES;
options = DURATION_OPTIONS;
Expand Down
16 changes: 16 additions & 0 deletions src/app/services/guards/create-user-guard.service.spec.ts
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();
});
});
16 changes: 16 additions & 0 deletions src/app/services/guards/create-user-guard.service.ts
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');
}
}
25 changes: 19 additions & 6 deletions src/app/services/guards/edit-user-guard.service.ts
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);
}));
}
}
1 change: 1 addition & 0 deletions src/app/user-form/user-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class UserFormComponent implements OnInit {
let processedFormData = this.processManagerData(this.form.getRawValue(), this.options);
if(processedFormData === null) {
this.snackBar.open("Could not create user", "Dismiss", { duration: 2000 });
this.responsePending = false;
return;
}
let response : any = await this.usersService.save(processedFormData);
Expand Down

0 comments on commit d9cd8d5

Please sign in to comment.