Skip to content

Commit 220c90f

Browse files
committed
add admin-guard
1 parent 47bcdb6 commit 220c90f

File tree

8 files changed

+63
-11
lines changed

8 files changed

+63
-11
lines changed

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { JwtInterceptor } from './jwt.interceptor';
2323
import { UserManagementComponent } from './user-management/user-management.component';
2424
import { AddUserComponent } from './add-user/add-user.component';
2525
import { PatronsListComponent } from './patrons-list/patrons-list.component';
26+
import { AuthErrorComponent } from './auth-error/auth-error.component';
2627

2728

2829
@NgModule({
@@ -38,7 +39,8 @@ import { PatronsListComponent } from './patrons-list/patrons-list.component';
3839
LinkThemesComponent,
3940
UserManagementComponent,
4041
AddUserComponent,
41-
PatronsListComponent
42+
PatronsListComponent,
43+
AuthErrorComponent
4244
],
4345
imports: [
4446
BrowserModule,

src/app/app.routing.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ import { LoginComponent } from './login/login.component';
1010
import { LinkThemesComponent } from './link-themes/link-themes.component';
1111
import { UserManagementComponent } from './user-management/user-management.component';
1212
import { AddUserComponent } from './add-user/add-user.component';
13-
import {PatronsListComponent} from "./patrons-list/patrons-list.component";
13+
import { PatronsListComponent } from "./patrons-list/patrons-list.component";
14+
import { AdminGuardService as AdminGuard } from './service/admin-guard.service';
15+
import { AuthErrorComponent } from './auth-error/auth-error.component';
16+
1417

1518
const routes: Routes = [
16-
{ path: "add-user", component: AddUserComponent, canActivate: [AuthGuard] },
19+
{ path: "add-user", component: AddUserComponent, canActivate: [AuthGuard, AdminGuard] },
20+
{ path: "auth-error", component: AuthErrorComponent, canActivate: [AuthGuard] }
1721
{ path: 'export/:id', component: ExportComponent, canActivate: [AuthGuard] },
18-
{ path: 'user-management', component: UserManagementComponent, canActivate: [AuthGuard] },
22+
{ path: 'user-management', component: UserManagementComponent, canActivate: [AuthGuard, AdminGuard] },
1923
{ path: "add-theme", component: ProposeThemeComponent },
2024
{ path: 'list', component: EpisodeListComponent, canActivate: [AuthGuard] },
21-
{ path: 'link-themes/:id', component: LinkThemesComponent, canActivate: [AuthGuard] },
25+
{ path: 'link-themes/:id', component: LinkThemesComponent, canActivate: [AuthGuard, AdminGuard] },
2226
{ path: 'episode-details/:id', component: EpisodeDetailsComponent, canActivate: [AuthGuard] },
23-
{ path: 'patrons-list', component: PatronsListComponent, canActivate: [AuthGuard] },
27+
{ path: 'patrons-list', component: PatronsListComponent, canActivate: [AuthGuard, AdminGuard] },
2428
{ path: '**', component: LoginComponent }
2529
]
2630

src/app/auth-error/auth-error.component.css

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="container">
2+
<div class="row align-items-center">
3+
<div class="col">
4+
You are not allowed to access this resource! Admin role required!
5+
</div>
6+
</div>
7+
</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Component, OnInit } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-auth-error',
5+
templateUrl: './auth-error.component.html',
6+
styleUrls: ['./auth-error.component.css']
7+
})
8+
export class AuthErrorComponent implements OnInit {
9+
10+
constructor() { }
11+
12+
ngOnInit() {
13+
}
14+
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Injectable } from '@angular/core';
2+
import { CanActivate, Router } from '@angular/router';
3+
import { LoggedUserService } from './logged-user.service';
4+
5+
@Injectable({
6+
providedIn: 'root'
7+
})
8+
export class AdminGuardService implements CanActivate{
9+
10+
constructor(private sessionUserService: LoggedUserService,
11+
private router: Router) { }
12+
13+
canActivate(): boolean {
14+
const user = this.sessionUserService.getSessionUser();
15+
if(!user.isAdmin){
16+
this.router.navigate(['auth-error']);
17+
return false;
18+
}
19+
20+
return true;
21+
}
22+
23+
}

src/app/ui/header/header.component.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
<div class="navbar-expand mr-auto">
44
<div class="navbar-nav">
55
<button *ngIf="isLogged()" type="button" class="btn btn-outline-light mx-2" (click)="goHome()">Home</button>
6-
<button *ngIf="currentUser.isAdmin" type="button" class="btn btn-outline-light mx-2" (click)="patronsList()">Patrons</button>
6+
<button *ngIf="isLogged() && isAdmin()" type="button" class="btn btn-outline-light mx-2" (click)="patronsList()">Patrons</button>
77
</div>
88
</div>
99
<div class="navbar-expand ml-auto navbar-nav">
1010
<div class="navbar-nav">
11-
<button *ngIf="isLogged()" type="button" class="btn btn-outline-light mx-2" (click)="logout()">Logout</button>
1211
<a *ngIf="isLogged()" class="nav-item nav-link">{{ getCurrentUserName() }}</a>
13-
<a class="nav-item nav-link" href="https://github.com/beeman" target="_blank">
14-
<i class="fa fa-github"></i>
15-
</a>
12+
<button *ngIf="isLogged()" type="button" class="btn btn-outline-light mx-2" (click)="logout()">Logout</button>
1613
</div>
1714
</div>
1815
</nav>

src/app/ui/header/header.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export class HeaderComponent implements OnInit {
3838
return this.authSerice.isAuthenticated()
3939
}
4040

41+
isAdmin(): boolean{
42+
return this.sessionUserService.getSessionUser().isAdmin
43+
}
44+
4145
getCurrentUserName(){
4246
return this.sessionUserService.getSessionUser().username
4347
}

0 commit comments

Comments
 (0)