Skip to content

Commit 98aacc6

Browse files
committed
Add guard to configuration component
1 parent 8095d55 commit 98aacc6

File tree

6 files changed

+46
-15
lines changed

6 files changed

+46
-15
lines changed

cloudapp/src/app/app-routing.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { StoreComponent } from './store/store.component';
1212
import { TranslateComponent } from './translate/translate.component';
1313
import { ConfigurationComponent, ConfigurationGuard } from './configuration/configuration.component';
1414
import { MultiSelectComponent } from './multi-select/multi-select.component';
15+
import { ErrorComponent } from './static/error.component';
1516

1617
const routes: Routes = [
1718
{ path: '', component: MainComponent },
@@ -26,6 +27,7 @@ const routes: Routes = [
2627
{ path: 'translate', component: TranslateComponent },
2728
{ path: 'multi-select', component: MultiSelectComponent },
2829
{ path: 'configuration', component: ConfigurationComponent, canActivate: [ConfigurationGuard] },
30+
{ path: 'error', component: ErrorComponent }
2931
];
3032

3133
@NgModule({

cloudapp/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { ConfigurationComponent } from './configuration/configuration.component'
2323
import { MultiSelectComponent } from './multi-select/multi-select.component';
2424
import { SelectEntitiesComponent } from './multi-select/select-entities/select-entities.component';
2525
import { LightboxComponent } from './external/lightbox/lightbox.component'
26+
import { ErrorComponent } from './static/error.component';
2627

2728
export function getToastrModule() {
2829
return ToastrModule.forRoot({
@@ -48,7 +49,8 @@ export function getToastrModule() {
4849
ConfigurationComponent,
4950
MultiSelectComponent,
5051
SelectEntitiesComponent,
51-
LightboxComponent
52+
LightboxComponent,
53+
ErrorComponent
5254
],
5355
imports: [
5456
MaterialModule,

cloudapp/src/app/configuration/configuration.component.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { AppService } from '../app.service';
33
import { FormBuilder, FormGroup } from '@angular/forms';
44
import { CloudAppConfigService, CloudAppEventsService } from '@exlibris/exl-cloudapp-angular-lib';
55
import { ToastrService } from 'ngx-toastr';
6-
import { CanActivate, Router, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
6+
import { CanActivate, Router } from '@angular/router';
77
import { Observable } from 'rxjs';
88
import { map } from 'rxjs/operators';
9+
import { ErrorMessages } from '../static/error.component';
910

1011
@Component({
1112
selector: 'app-configuration',
@@ -61,15 +62,14 @@ export class ConfigurationGuard implements CanActivate {
6162
private eventsService: CloudAppEventsService,
6263
private router: Router
6364
) {}
64-
canActivate(
65-
next: ActivatedRouteSnapshot,
66-
state: RouterStateSnapshot): Observable<boolean> {
67-
return this.eventsService.getInitData().pipe(map( data => {
68-
if (!data.user.isAdmin) {
69-
this.router.navigate(['/']);
70-
return false;
71-
}
72-
return true;
73-
}))
65+
canActivate(): Observable<boolean> {
66+
return this.eventsService.getInitData().pipe(map( data => {
67+
if (!data.user.isAdmin) {
68+
this.router.navigate(['/error'],
69+
{ queryParams: { error: ErrorMessages.NO_ACCESS}});
70+
return false;
71+
}
72+
return true;
73+
}))
7474
}
75-
}
75+
}

cloudapp/src/app/main/main.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ <h1>
1414
<li><a [routerLink]="['store']">Using the Store Service</a></li>
1515
<li><a [routerLink]="['translate']">Translating your app</a></li>
1616
<li><a [routerLink]="['multi-select']">Selecting multiple entities</a></li>
17-
<li *ngIf="isAdmin"><a [routerLink]="['configuration']">Using the Configuration Service</a></li>
17+
<li><a [routerLink]="['configuration']">Using the Configuration Service</a></li>
1818
</ul>
1919
</section>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Component } from "@angular/core";
2+
import { ActivatedRoute } from "@angular/router";
3+
4+
@Component({
5+
template: "{{error}}"
6+
})
7+
export class ErrorComponent {
8+
error: string;
9+
constructor(
10+
private route: ActivatedRoute,
11+
) {}
12+
13+
ngOnInit() {
14+
this.route.queryParams.subscribe(params => {
15+
this.error = errorMessages[params['error']] || 'An error has occurred.';
16+
});
17+
}
18+
}
19+
20+
const errorMessages = {
21+
'NO_ACCESS': "You don't have access to this page."
22+
}
23+
24+
export enum ErrorMessages {
25+
NO_ACCESS = 'NO_ACCESS'
26+
}

manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"title": "Cloud App Tutorials",
44
"description": "For more information on these tutorials, see https://developers.exlibrisgroup.com/cloudapps/tutorials/.",
55
"subtitle": "Accompanying code for the Cloud App tutorials in the Developer Network",
6-
"author": "Josh Weisman",
6+
"author": "Ex Libris",
77
"contentSecurity": {
88
"connectSrc": [
99
"https://catalog.hathitrust.org/",
@@ -16,6 +16,7 @@
1616
},
1717
"pages": {
1818
"settings": "/#/settings",
19+
"config": "/#/configuration",
1920
"help": "https://github.com/ExLibrisGroup/cloudapp-tutorials"
2021
},
2122
"icon": {

0 commit comments

Comments
 (0)