Skip to content

Commit

Permalink
SHIBUI-1058 Implemented route guard for non-admins
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jan 22, 2019
1 parent 64b130c commit c39e573
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 26 deletions.
2 changes: 1 addition & 1 deletion ui/src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('AppComponent', () => {

it('should create the app', async(() => {
expect(app).toBeTruthy();
expect(store.dispatch).toHaveBeenCalledTimes(2);
expect(store.dispatch).toHaveBeenCalledTimes(3);
}));

it(`should have as title 'Shib-UI'`, async(() => {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/core/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ export const getVersionError = createSelector(getVersionState, fromVersion.getVe
export const getConfigState = createSelector(getCoreFeature, getConfigStateFn);
export const getRoles = createSelector(getConfigState, fromConfig.getRoles);

export const isCurrentUserAdmin = createSelector(getUser, user => user.role === 'ROLE_ADMIN');
export const isCurrentUserAdmin = createSelector(getUser, user => user ? user.role === 'ROLE_ADMIN' : null);
18 changes: 5 additions & 13 deletions ui/src/app/core/reducer/user.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ describe('User Reducer', () => {
};

const user: User = {
id: '1',
username: 'foo',
role: 'admin',
name: {
first: 'foo',
last: 'bar'
}
firstName: 'somebody',
lastName: 'nobody',
emailAddress: 'email@edu.edu'
};

describe('undefined action', () => {
Expand Down Expand Up @@ -59,14 +58,7 @@ describe('User Reducer', () => {

describe('User Selectors', () => {
const state = {
user: {
id: '1',
role: 'admin',
name: {
first: 'foo',
last: 'bar'
}
},
user: { ...user },
fetching: true,
error: { message: 'foo', type: 'bar' }
} as fromUser.UserState;
Expand Down
37 changes: 37 additions & 0 deletions ui/src/app/core/service/admin.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { Store } from '@ngrx/store';

import * as fromCore from '../reducer';
import { Observable } from 'rxjs';
import { filter, catchError, take } from 'rxjs/operators';


@Injectable({
providedIn: 'root',
})
export class AdminGuard implements CanActivate {

constructor(
private store: Store<fromCore.CoreState>,
private router: Router
) {}

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
const isAdminObs = this.checkIsAdmin().pipe(take(1));
isAdminObs.subscribe(authed => {
if (!authed) {
this.router.navigate(['/']);
}
});
return isAdminObs;
}

checkIsAdmin(): Observable<boolean> {
return this.store
.select(fromCore.isCurrentUserAdmin)
.pipe(
filter(isAdmin => isAdmin !== null),
);
}
}
13 changes: 3 additions & 10 deletions ui/src/app/core/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { User } from '../model/user';
import { HttpClient } from '@angular/common/http';
import { catchError } from 'rxjs/operators';
import { catchError, map } from 'rxjs/operators';

@Injectable()
export class UserService {
Expand All @@ -21,15 +21,8 @@ export class UserService {

getCurrentUser(): Observable<User> {
return this.http.get<User>(
`${this.base}/user`
).pipe(
catchError(err => of({
username: 'abc123',
firstName: 'Foo',
lastName: 'Bar',
role: 'ROLE_USER',
emailAddress: 'foo@unicon.net'
} as User))
`${this.base}/admin/users/current`
);
// .pipe(map(user => ({ ...user, role: 'ROLE_USER' })));
}
} /* istanbul ignore next */
4 changes: 3 additions & 1 deletion ui/src/app/dashboard/dashboard.routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DashboardProvidersListComponent } from '../metadata/manager/container/d
import { UserPageComponent } from '../user/user.component';
import { AdminComponent } from '../user/admin/admin.component';
import { AdminManagementPageComponent } from '../user/admin/container/admin-management.component';
import { AdminGuard } from '../core/service/admin.guard';

const routes: Routes = [
{
Expand All @@ -26,14 +27,15 @@ const routes: Routes = [
children: [
{ path: '', redirectTo: 'resolvers', pathMatch: 'prefix' },
{ path: 'resolvers', component: DashboardResolversListComponent },
{ path: 'providers', component: DashboardProvidersListComponent },
{ path: 'providers', component: DashboardProvidersListComponent, canActivate: [AdminGuard] },
]
}
]
},
{
path: 'users',
component: UserPageComponent,
canActivate: [AdminGuard],
children: [
{ path: '', redirectTo: 'admin', pathMatch: 'prefix' },
{
Expand Down
2 changes: 2 additions & 0 deletions ui/src/app/metadata/provider/provider.routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import { SelectFilterComponent } from '../filter/container/select-filter.compone
import { EditFilterComponent } from '../filter/container/edit-filter.component';
import { CanDeactivateGuard } from '../../core/service/can-deactivate.guard';
import { FilterComponent } from '../filter/container/filter.component';
import { AdminGuard } from '../../core/service/admin.guard';

export const ProviderRoutes: Routes = [
{
path: 'provider',
component: ProviderComponent,
canActivate: [AdminGuard],
children: [
{
path: 'wizard',
Expand Down

0 comments on commit c39e573

Please sign in to comment.