Add a mathod in community service to check if a user is a manager of a community| change connectAdminLoginGuard to really ask for a community manager| correct some links in properties json

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@51261 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
argiro.kokogiannaki 2018-03-15 09:53:07 +00:00
parent 1d9daca041
commit fbcec793bc
9 changed files with 189 additions and 56 deletions

View File

@ -55,8 +55,7 @@ import{ HtmlProjectReportComponent} from './landingPages/htmlProjectReport/htmlP
const routes: Routes = [
{ path: '', component: SearchComponent, resolve: { envSpecific: EnvironmentSpecificResolver }, canActivate: [ IsRouteEnabled], data: {
redirect: '/error',api: "http://duffy.di.uoa.gr:8080/uoa-admin-tools", community :"openaire"}},
{ path: '', component: SearchComponent, resolve: { envSpecific: EnvironmentSpecificResolver }, canActivate: [ ]},
{ path: 'home', component: SearchComponent, resolve: { envSpecific: EnvironmentSpecificResolver } },
{ path: 'search/publication', component: PublicationComponent , resolve: { envSpecific: EnvironmentSpecificResolver } },
{ path: 'search/dataset', component: DatasetComponent, resolve: { envSpecific: EnvironmentSpecificResolver } },
@ -70,8 +69,7 @@ const routes: Routes = [
{ path: 'participate/deposit-publications', component: DepositPublicationsComponent , resolve: { envSpecific: EnvironmentSpecificResolver } },
{ path: 'participate/deposit-publications-result', component: DepositPublicationsResultComponent, resolve: { envSpecific: EnvironmentSpecificResolver } },
{ path: 'search/find', component: SearchComponent, resolve: { envSpecific: EnvironmentSpecificResolver } },
{ path: 'search/find/publications', component: SearchPublicationsComponent , resolve: { envSpecific: EnvironmentSpecificResolver }, canActivate: [ IsRouteEnabled], data: {
redirect: '/error',api: "http://duffy.di.uoa.gr:8080/uoa-admin-tools", community :"openaire"} },
{ path: 'search/find/publications', component: SearchPublicationsComponent , resolve: { envSpecific: EnvironmentSpecificResolver }, canActivate: [ ] },
{ path: 'search/find/datasets', component:SearchDatasetsComponent, resolve: { envSpecific: EnvironmentSpecificResolver } },
{ path: 'search/find/software', component:SearchSoftwareComponent, resolve: { envSpecific: EnvironmentSpecificResolver } },
{ path: 'search/find/projects', component:SearchProjectsComponent, resolve: { envSpecific: EnvironmentSpecificResolver } },

View File

@ -14,7 +14,9 @@ export class CommunityService {
getCommunity(url: string) {
return this.http.get(url).map(res => <any> res.json()).map(res => this.parseCommunity(res));
}
iscommunityManager(url: string, manager:string){
return this.http.get(url).map(res => <any> res.json()).map(res => this.parseCommunity(res)).map(community => community.managers.indexOf(manager)!=-1);
}
parseCommunity(data:any): CommunityInfo {
let length = Array.isArray(data) ? data.length :1;

View File

@ -0,0 +1,17 @@
import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import {CommunityErrorPageComponent } from './communityErrorPage.component';
import {MetaModule} from '../../sharedComponents/metaService.module';
@NgModule({
imports: [
CommonModule, FormsModule, MetaModule
],
providers:[],
declarations: [
CommunityErrorPageComponent
], exports:[CommunityErrorPageComponent]
})
export class CommunityErrorModule { }

View File

@ -0,0 +1,50 @@
import { Component, Input } from '@angular/core';
import { Location } from '@angular/common';
import { Meta} from '../../sharedComponents/metaService';
import {ActivatedRoute} from '@angular/router';
@Component({
selector: 'community-error',
template: `
<div id="tm-main" class=" uk-section uk-margin-small-top tm-middle" >
<div uk-grid uk-grid>
<div class="tm-main uk-width-1-1@s uk-width-1-1@m uk-width-1-1@l uk-row-first ">
<div class="uk-container">
<h2>
There is no community selected!
</h2>
<br>
<p>
If you are a community manager, click to manage from user menu.
</p>
</div>
</div>
</div>
</div>
`
})
export class CommunityErrorPageComponent {
public page: string;
constructor (private _location: Location, private _meta: Meta,private route: ActivatedRoute) {
this._meta.setTitle("OpenAIRE | Error page");
this.page = _location.path(true);
//this.page = _router.url;
//this.page = location.href;
}
ngOnInit() {
this.route.queryParams.subscribe(data => {
this.page = data['page'];
if(!this.page){
this.page = this._location.path(true);
}
});
}
}

View File

@ -0,0 +1,83 @@
import { Injectable } from '@angular/core';
import { Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {Session} from '../../login/utils/helper.class';
import {ErrorCodes} from '../../login/utils/guardHelper.class';
import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import { mergeMap } from 'rxjs/operators';
@Injectable()
export class ConnectAdminLoginGuard implements CanActivate {
constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService ) {}
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;
let community = (route.queryParams["communityId"]);
if(Session.isLoggedIn()){
loggedIn = true;
if(!Session.isValidAndRemove()){
loggedIn = false;
errorCode = ErrorCodes.NOT_VALID;
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}else if(Session.isPortalAdministrator()) {
console.log("is Admin");
isAdmin = true;
return true;
}else {
// let first = Observable.timer(10,500).map(r => { return {source:1,value:r}; }).take(4);
// let second = Observable.timer(10,500).map(r => { return {source:2,value:r}; }).take(4);
// first.merge(second).subscribe(res => this.mergeStream.push(res));
let obs = this.propertiesService.subscribeEnvironment().map(res=>res["communityAPI"]).mergeMap(url => {
return this.communityService.iscommunityManager(url+community,Session.getUserEmail())});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }));
return obs;
}
}else{
errorCode =ErrorCodes.NOT_LOGGIN;
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
return false;
}
// if(!loggedIn){
// // this.guardHelper.redirect("/user-info",errorCode,state.url);
// this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } });
//
// return false;
// }else{
// return true;
// }
}
/*
constructor(private route: ActivatedRoute,private router: Router, private config: ConfigurationService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
let customRedirect = route.data['redirect'];
let api = route.data['api'];
let community = route.data['community']
if(!community){
community = (route.queryParams["communityId"])?route.queryParams["communityId"]:route.queryParams["community"];
}
if(community){
let isEnabled = this.config.isPageEnabled(api, community,"/"+state.url.split("?")[0].substring(1));
let redirect = !!customRedirect ? customRedirect : '/error';
isEnabled.filter(enabled => !enabled)
.subscribe(() => this.router.navigate([redirect], { queryParams: { "page": state.url } }));
return isEnabled;
}
return true;
}
*/
}

View File

@ -0,0 +1,20 @@
import { Injectable } from '@angular/core';
import {ActivatedRoute, Router,CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/filter';
@Injectable()
export class IsCommunity implements CanActivate {
constructor(private route: ActivatedRoute,private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
let community = (route.queryParams["communityId"]);
if(community && community!="undefined"){
return true;
}else{
this.router.navigate(['errorcommunity']);
}
return false;
}
}

View File

@ -1,50 +0,0 @@
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

@ -145,7 +145,7 @@ export class Session{
// console.log("Is admin:"+ isAdmin)
return (isAdmin);
}
public static isConnectAdminUser():boolean {
public static isPortalAdministrator():boolean {
var isAdmin = false;
if(Session.isLoggedIn()){
var roles = ["urn:mace:openminted.eu:aai.openminted.eu:group:OpenAIRE+Portal+Administrator"]

View File

@ -55,6 +55,19 @@ export class MyComponent {
return Promise.resolve(this.envSpecificNull);
}
public subscribeEnvironment() {
// Only want to do this once - if root page is revisited, it calls this again.
if (this.envSpecific === null || this.envSpecific === undefined) {
console.log('Loading env-properties.json');
return this.http.get(this.propertiesUrl)
.map((data) => data.json());
}
return Observable.of(this.envSpecific);
}
public setEnvProperties(es: EnvProperties) {
// This has already been set so bail out.