Skip to content

Commit c506b90

Browse files
author
Your Name
committed
angular security course
1 parent 681706c commit c506b90

File tree

3 files changed

+106
-32
lines changed

3 files changed

+106
-32
lines changed

src/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<li>
1010
<a routerLink="/lessons">Lessons</a>
1111
</li>
12-
<li>
12+
<li *rbacAllow="['ADMIN']">
1313
<a routerLink="/admin">Admin</a>
1414
</li>
1515
<li *ngIf="isLoggedOut$ | async">

src/app/app.module.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { BrowserModule } from '@angular/platform-browser';
1+
import {BrowserModule} from '@angular/platform-browser';
22
import {NgModule, Provider} from '@angular/core';
33
import {HttpClientModule, HttpClientXsrfModule} from '@angular/common/http';
44

5-
import { AppComponent } from './app.component';
6-
import { LessonsComponent } from './lessons/lessons.component';
7-
import { LoginComponent } from './login/login.component';
8-
import { SignupComponent } from './signup/signup.component';
5+
import {AppComponent} from './app.component';
6+
import {LessonsComponent} from './lessons/lessons.component';
7+
import {LoginComponent} from './login/login.component';
8+
import {SignupComponent} from './signup/signup.component';
99
import {routesConfig} from "./routes.config";
1010
import {LessonsService} from "./services/lessons.service";
1111
import {ReactiveFormsModule} from "@angular/forms";
1212

1313
import {AuthService} from "./services/auth.service";
14-
import { AdminComponent } from './admin/admin.component';
15-
import { RouterModule} from "@angular/router";
14+
import {AdminComponent} from './admin/admin.component';
15+
import {RouterModule} from "@angular/router";
1616

1717

1818
import 'rxjs/add/operator/switchMap';
@@ -23,33 +23,33 @@ import 'rxjs/add/operator/filter';
2323
import 'rxjs/add/operator/catch';
2424
import 'rxjs/add/operator/first';
2525
import 'rxjs/add/observable/of';
26-
27-
26+
import {RbacAllowDirective} from "./common/rbac-allow.directive";
2827

2928

3029
@NgModule({
31-
declarations: [
32-
AppComponent,
33-
LessonsComponent,
34-
LoginComponent,
35-
SignupComponent,
36-
AdminComponent
37-
],
38-
imports: [
39-
BrowserModule,
40-
HttpClientModule,
41-
HttpClientXsrfModule.withOptions({
42-
cookieName: 'XSRF-TOKEN',
43-
headerName: 'x-xsrf-token'
44-
}),
45-
RouterModule.forRoot(routesConfig),
46-
ReactiveFormsModule
47-
],
48-
providers: [
49-
LessonsService,
50-
AuthService
51-
],
52-
bootstrap: [AppComponent]
30+
declarations: [
31+
AppComponent,
32+
LessonsComponent,
33+
LoginComponent,
34+
SignupComponent,
35+
AdminComponent,
36+
RbacAllowDirective
37+
],
38+
imports: [
39+
BrowserModule,
40+
HttpClientModule,
41+
HttpClientXsrfModule.withOptions({
42+
cookieName: 'XSRF-TOKEN',
43+
headerName: 'x-xsrf-token'
44+
}),
45+
RouterModule.forRoot(routesConfig),
46+
ReactiveFormsModule
47+
],
48+
providers: [
49+
LessonsService,
50+
AuthService
51+
],
52+
bootstrap: [AppComponent]
5353
})
5454
export class AppModule {
5555

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
3+
4+
import {Directive, Input, OnDestroy, TemplateRef, ViewContainerRef} from "@angular/core";
5+
import {AuthService} from "../services/auth.service";
6+
import {User} from "../model/user";
7+
import {Subscription} from "rxjs/Subscription";
8+
import * as _ from 'lodash';
9+
10+
@Directive({
11+
selector:"[rbacAllow]"
12+
})
13+
export class RbacAllowDirective implements OnDestroy {
14+
15+
allowedRoles:string[];
16+
user:User;
17+
18+
sub:Subscription;
19+
20+
constructor(
21+
private templateRef: TemplateRef<any>,
22+
private viewContainer: ViewContainerRef,
23+
private authService: AuthService) {
24+
25+
this.sub = authService.user$.subscribe(
26+
user => {
27+
this.user = user;
28+
this.showIfUserAllowed();
29+
});
30+
}
31+
32+
ngOnDestroy() {
33+
this.sub.unsubscribe();
34+
}
35+
36+
@Input()
37+
set rbacAllow(allowedRoles: string[]) {
38+
this.allowedRoles = allowedRoles;
39+
this.showIfUserAllowed();
40+
}
41+
42+
showIfUserAllowed() {
43+
44+
if (!this.allowedRoles || this.allowedRoles.length === 0 ||
45+
!this.user) {
46+
this.viewContainer.clear();
47+
return;
48+
}
49+
50+
const isUserAllowed =
51+
_.intersection(this.allowedRoles, this.user.roles).length > 0;
52+
53+
54+
if (isUserAllowed) {
55+
this.viewContainer.createEmbeddedView(this.templateRef);
56+
}
57+
else {
58+
this.viewContainer.clear();
59+
}
60+
61+
}
62+
63+
}
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+

0 commit comments

Comments
 (0)