Skip to content

Commit a6730c7

Browse files
committed
Angular Security course
1 parent 44f6b96 commit a6730c7

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

server/security.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function createSessionToken(user: DbUser) {
2828
},
2929
RSA_PRIVATE_KEY, {
3030
algorithm: 'RS256',
31-
expiresIn: 240,
31+
expiresIn: 7200,
3232
subject: user.id.toString()
3333
});
3434
}

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'rxjs/add/observable/of';
2323
import { AdminComponent } from './admin/admin.component';
2424
import {AuthorizationGuard} from "./services/auth.guard";
2525
import {Router, RouterModule} from "@angular/router";
26+
import {RbacAllow} from "./common/rbac-allow.directive";
2627

2728

2829

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

src/app/services/auth.guard.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export class AuthorizationGuard implements CanActivate {
1515
state:RouterStateSnapshot):Observable<boolean> {
1616

1717
return this.authService.user$
18-
.do(user => console.log(user, this.allowedRoles, _.intersection(user.roles, this.allowedRoles)))
1918
.map(user => _.intersection(user.roles, this.allowedRoles).length > 0)
2019
.first()
2120
.do(allowed => {

0 commit comments

Comments
 (0)