creating a first draft for connect admin guard

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@50771 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
argiro.kokogiannaki 2018-02-16 10:24:00 +00:00
parent 4fac0720b2
commit 4420cce2f4
4 changed files with 83 additions and 10 deletions

View File

@ -0,0 +1,50 @@
import { Injectable } from '@angular/core';
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {Session} from './utils/helper.class';
import {ErrorCodes} from './utils/guardHelper.class';
@Injectable()
export class ConnectAdminLoginGuard implements CanActivate {
constructor(private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
console.log("aaa");
console.log(state.url);
var user;
var loggedIn = false;
var isAdmin = false;
var errorCode = ErrorCodes.NOT_LOGGIN;
if(Session.isLoggedIn()){
loggedIn = true;
if(!Session.isValidAndRemove()){
loggedIn = false;
errorCode = ErrorCodes.NOT_VALID;
}else {
isAdmin = Session.isConnectAdminUser();
if(!isAdmin){
errorCode = ErrorCodes.NOT_CONNECT_ADMIN;
}
// if(state.url.indexOf("community")==-1){
// errorCode = ErrorCodes.NO_COMMUNITY;
// }
}
}else{
errorCode =ErrorCodes.NOT_LOGGIN;
}
if(!loggedIn){
// this.guardHelper.redirect("/user-info",errorCode,state.url);
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}else if(!isAdmin){
// this.guardHelper.redirect("/user-info",errorCode,state.url);
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}else{
return true;
}
}
}

View File

@ -5,7 +5,12 @@
<div *ngIf="!server" class="uk-margin-top uk-container uk-container-small uk-position-relative">
<div *ngIf="loggedIn">
<div class="uk-alert uk-alert-success">
Hello {{user.fullname}}!
</div>
<!-- <button (click)="logout()" class=" uk-button uk-button-default">Log out</button> -->
</div>
<!--form *ngIf="!loggedIn" class=" ">
<h3>Welcome to OpenAIRE's Discover Portal</h3>
@ -26,9 +31,9 @@
</div>
</form-->
<div *ngIf="errorCode == '1'" class="uk-alert uk-alert-warning">
The requested page requires authentication. Please sign in.
The requested page requires authentication.
<span *ngIf="!loggedIn">
<a class="loginLink" (click)="logIn()" >Sign in</a>
<a class="loginLink" (click)="logIn()" >Please Sign in</a>
</span>
</div>
<div *ngIf="errorCode == '2'" class="uk-alert uk-alert-warning">
@ -40,14 +45,15 @@
<a class="loginLink" [href]="loginUrl" >Sign in</a>
</span>
</div>
<div *ngIf="errorCode == '4'" class="uk-alert uk-alert-warning">
You are not authorized to use the requested page
</div>
<div *ngIf="errorCode == '5'" class="uk-alert uk-alert-warning">
There is no community selected
</div>
<div *ngIf="!loggedIn && errorMessage.length > 0" class="uk-alert uk-alert-danger">{{errorMessage}}</div>
<div *ngIf="loggedIn">
<div class="uk-alert uk-alert-success">
Hello {{user.fullname}}!
</div>
<!-- <button (click)="logout()" class=" uk-button uk-button-default">Log out</button> -->
</div>
</div>
</div>
</div>

View File

@ -14,6 +14,7 @@ export class ErrorCodes {
public static NOT_LOGGIN:number =1;
public static NOT_ADMIN:number =2;
public static NOT_VALID:number =3;
public static NOT_CONNECT_ADMIN:number =4;
public static NO_COMMUNITY:number =5;
}

View File

@ -145,6 +145,22 @@ export class Session{
// console.log("Is admin:"+ isAdmin)
return (isAdmin);
}
public static isConnectAdminUser():boolean {
var isAdmin = false;
if(Session.isLoggedIn()){
var roles = ["urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+Portal+Administrator"]
for (var i = 0; i < roles.length; i++) {
if ((Session.getUser().role).indexOf(roles[i]) > -1) {
isAdmin = true;
break;
}
}
// console.log("Is admin:"+ isAdmin)
return (isAdmin);
}
// console.log("Is admin:"+ isAdmin)
return (isAdmin);
}
public static isRegisteredUser():boolean {
var isRegisteredUser = false;
if(Session.isLoggedIn()){