Skip to content

Commit

Permalink
HIPCMS-424 updated guards, updated menu
Browse files Browse the repository at this point in the history
  • Loading branch information
maltehol committed Aug 30, 2016
1 parent f70b157 commit 36acdc9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 161 deletions.
73 changes: 0 additions & 73 deletions app/app.routes.ts

This file was deleted.

55 changes: 16 additions & 39 deletions app/shared/auth/admin-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,28 @@ import {
} from '@angular/router';

import { UserService } from './../user/user.service';
import { User } from '../user/user.model';
import { CmsApiService } from '../api/cms-api.service';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class AdminGuard implements CanActivate {
constructor(private cmsApiService: CmsApiService, private router: Router) {
constructor(private userService: UserService, private router: Router) {
}

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.cmsApiService.getUrl('/api/Users/Current', {})
.map(
value => {
if ((<User> value.json()).role === 'Administrator') {
return true;
} else {
this.router.navigate(['/dashboard']);
return false;
}
return this.userService.getCurrent().then(
user => {
if (user.role === 'Administrator') {
return true;
} else {
this.router.navigate(['/dashboard']);
return false;
}
).catch(this.handleError);
/*
this.userService.getCurrent().then(
user => {
if (user.role === 'Administrator') {
return true;
} else {
this.router.navigate(['/dashboard']);
return false;
}
}
).catch(
error => {
console.log(error);
this.router.navigate(['/dashboard']);
return false;
}
); */
}

handleError(error: any) {
console.log(error);
this.router.navigate(['/dashboard']);
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
return Observable.throw(errMsg);
}
).catch(
error => {
console.log(error);
this.router.navigate(['/dashboard']);
return false;
}
);
}
}
2 changes: 1 addition & 1 deletion app/shared/auth/auth-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) { }

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (!this.authService.loggedIn) {
if (!this.authService.isLoggedIn()) {
this.router.navigate(['/login']);
return false;
}
Expand Down
60 changes: 16 additions & 44 deletions app/shared/auth/supervisor-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,28 @@ import {
} from '@angular/router';

import { UserService } from './../user/user.service';
import { User } from '../user/user.model';
import { CmsApiService } from '../api/cms-api.service';
import { Observable } from 'rxjs/Rx';

@Injectable()
export class SupervisorGuard implements CanActivate {
constructor(private cmsApiService: CmsApiService, private router: Router) {
constructor(private userService: UserService, private router: Router) {
}

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return this.cmsApiService.getUrl('/api/Users/Current', {})
.map(
value => {
console.log(value.json());
if ((<User> value.json()).role === 'Supervisor' || (<User> value.json()).role === 'Administrator') {
return true;
} else {
this.router.navigate(['/dashboard']);
return false;
}
return this.userService.getCurrent().then(
user => {
if (user.role === 'Supervisor' || user.role === 'Administrator') {
return true;
} else {
this.router.navigate(['/dashboard']);
return false;
}
).catch(this.handleError);

// Use code below as soon we updated Angular
/*
this.userService.getCurrent().then(
user => {
if (user.role === 'Supervisor') {
return true;
} else {
console.log('ELSE');
this.router.navigate(['/dashboard']);
return false;
}
}
).catch(
error => {
console.log(error);
this.router.navigate(['/dashboard']);
return false;
}
);
*/
}

handleError(error: any) {
console.log(error);
this.router.navigate(['/dashboard']);
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
return Observable.throw(errMsg);
}
).catch(
error => {
console.log(error);
this.router.navigate(['/dashboard']);
return false;
}
);
}
}
17 changes: 14 additions & 3 deletions app/shared/sidenav/sidenav.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ import { TranslateService } from '../translate/translate.service';
export class SidenavComponent implements OnInit {
opened = false;
mode = 'side';
additionalMenuAdded = false;

navigation = [
navigation: any[] = [];
studentNavigation = [
{
'link': '/dashboard',
'name': 'Dashboard'
Expand Down Expand Up @@ -65,14 +67,14 @@ export class SidenavComponent implements OnInit {

ngOnInit() {
this.isOpened();
this.addAdditionalMenu();
window.onresize = (e) => {
this.ngZone.run(() => {
this.isOpened();
});
};
this.router.events.subscribe(() => {
this.isOpened();
this.addAdditionalMenu();
});
}

Expand All @@ -85,9 +87,18 @@ export class SidenavComponent implements OnInit {
}

addAdditionalMenu() {
if (!this.authService.isLoggedIn()) {
return;
}
this.userService.getCurrent().then(
user => {
if (user.role === 'Supervisor') {
this.navigation = [];
if (user.role === 'Student' || user.role === 'Supervisor' || user.role === 'Administrator') {
for (let element of this.studentNavigation) {
this.navigation.push(element);
}
}
if (user.role === 'Supervisor' || user.role === 'Administrator') {
for (let element of this.supervisorNavigation) {
this.navigation.push(element);
}
Expand Down
2 changes: 1 addition & 1 deletion app/topics/show-topic/show-topic.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class ShowTopicComponent implements OnInit {
}
this.userService.getCurrent().then(
user => {
this.disableEditing = user.role !== 'Supervisor';
this.disableEditing = (user.role !== 'Supervisor' || user.role !== 'Administrator');
}
);
console.log(this.topic);
Expand Down

0 comments on commit 36acdc9

Please sign in to comment.