[Trunk|Library]: connectCommunityGuard has been added it for check if type is community. Change community error page message

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@54965 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2019-03-07 15:43:54 +00:00
parent 780c5c15bd
commit 10051a303e
9 changed files with 119 additions and 106 deletions

View File

@ -15,35 +15,41 @@ export class CommunityService {
}
updateCommunity(url: string, community: any) {
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
let body = JSON.stringify(community);
return this.http.post(url, body, options)
const headers = new Headers({'Content-Type': 'application/json'});
const options = new RequestOptions({headers: headers});
const body = JSON.stringify(community);
return this.http.post(url, body, options);
/*.map(res => res.json())*/
}
iscommunityManager(properties:EnvProperties, url: string, manager:string){
isCommunityManager(properties: EnvProperties, url: string, manager: string) {
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
.map(res => <any> res.json()).map(res => this.parseCommunity(res)).map(community => community.managers.indexOf(manager)!=-1);
.map(res => <any> res.json()).map(res =>
this.parseCommunity(res)).map(community => community.managers.indexOf(manager) !== -1);
}
iscommunityRI(properties:EnvProperties, url: string){
isRIType(properties: EnvProperties, url: string) {
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
.map(res => <any> res.json()).map(res => this.parseCommunity(res)).map(community => (community && community.type && community.type !="community"));
.map(res => <any> res.json()).map(res =>
this.parseCommunity(res)).map(community =>
(community && community.type && community.type === 'ri'));
}
isCommunityType(properties: EnvProperties, url: string) {
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
.map(res => <any> res.json()).map(res =>
this.parseCommunity(res)).map(community =>
(community && community.type && community.type === 'community'));
}
isSubscribedToCommunity(pid: string, email: string, url: string) {
return this.http.get(url+"/community/"+pid+"/subscribers")
.map(res => ((<any>res =="")?{}:<any> res.json()))
return this.http.get(url + '/community/' + pid + '/subscribers')
.map(res => ((<any>res === '') ? {} : <any> res.json()))
.map(res => {
if (res.subscribers && res.subscribers != null) {
for(var i =0; i< res.subscribers.length; i++ ){
if(res.subscribers[i]!=null && res.subscribers[i].email == email){
for (let i = 0; i < res.subscribers.length; i++ ) {
if (res.subscribers[i] != null && res.subscribers[i].email === email) {
return true;
}
}
@ -55,9 +61,9 @@ export class CommunityService {
private parseCommunity(data: any): CommunityInfo {
let resData = Array.isArray(data) ? data[0] : data;
const resData = Array.isArray(data) ? data[0] : data;
let community: CommunityInfo = new CommunityInfo();
const community: CommunityInfo = new CommunityInfo();
community['title'] = resData.name;
community['shortTitle'] = resData.shortName;
community['communityId'] = resData.id;
@ -66,12 +72,12 @@ export class CommunityService {
community['description'] = resData.description;
community['date'] = resData.creationDate;
community['zenodoCommunity'] = resData.zenodoCommunity;
community['status'] = "all";
community['status'] = 'all';
if (resData.hasOwnProperty('status')) {
community['status'] = resData.status;
let status = ["all","hidden","manager"];
if(status.indexOf(community['status']) ==-1){
community['status'] = "hidden";
const status = ['all', 'hidden', 'manager'];
if (status.indexOf(community['status']) === -1) {
community['status'] = 'hidden';
}
}
if (resData.type != null) {
@ -79,29 +85,29 @@ export class CommunityService {
}
if (resData.managers != null) {
if(community['managers'] == undefined) {
if (community['managers'] === undefined) {
community['managers'] = new Array<string>();
}
let managers = resData.managers;
let length = Array.isArray(managers) ? managers.length : 1;
const managers = resData.managers;
const length = Array.isArray(managers) ? managers.length : 1;
for (let i = 0; i < length; i++) {
let manager = Array.isArray(managers) ? managers[i] : managers;
const manager = Array.isArray(managers) ? managers[i] : managers;
community.managers[i] = manager;
}
}
if (resData.subjects != null) {
if(community['subjects'] == undefined) {
if (community['subjects'] === undefined) {
community['subjects'] = new Array<string>();
}
let subjects = resData.subjects;
let length = Array.isArray(subjects) ? subjects.length : 1;
const subjects = resData.subjects;
const length = Array.isArray(subjects) ? subjects.length : 1;
for (let i = 0; i < length; i++) {
let subject = Array.isArray(subjects) ? subjects[i] : subjects;
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
community.subjects[i] = subject;
}
}

View File

@ -1,4 +1,4 @@
import {Component, Input} from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {Location} from '@angular/common';
import {ActivatedRoute} from '@angular/router';
import {Title, Meta} from '@angular/platform-browser';
@ -12,7 +12,7 @@ import {Title, Meta} from '@angular/platform-browser';
<div class="uk-container">
<h2>
There is no community selected!
There is no community selected or the requested page is not available at this type of community!
</h2>
<br>
<p>
@ -25,19 +25,17 @@ import {Title, Meta} from '@angular/platform-browser';
`
})
export class CommunityErrorPageComponent {
export class CommunityErrorPageComponent implements OnInit {
public page: string;
constructor (private _location: Location, private _meta: Meta,
private _title: Title, private route: ActivatedRoute) {
var title = "OpenAIRE | Error page";
const title = 'OpenAIRE | Error page';
this._meta.updateTag({content: title}, "property='og:title'");
this._title.setTitle(title);
this.page = _location.path(true);
//this.page = _router.url;
//this.page = location.href;
}
ngOnInit() {

View File

@ -26,7 +26,7 @@ export class ConnectAdminLoginGuard implements CanActivate, CanLoad {
return true;
} else {
const obs = this.propertiesService.subscribeEnvironment().map(res => res).mergeMap(properties => {
return this.communityService.iscommunityManager(properties, properties['communityAPI'] + community, Session.getUserEmail());
return this.communityService.isCommunityManager(properties, properties['communityAPI'] + community, Session.getUserEmail());
});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], {

View File

@ -0,0 +1,40 @@
import { Injectable } from '@angular/core';
import {
Router,
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanLoad,
Route
} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connectHelper';
@Injectable()
export class ConnectCommunityGuard implements CanActivate, CanLoad {
constructor(private router: Router,
private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService) {
}
check(community: string): Observable<boolean> | boolean {
const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
return this.communityService.isCommunityType(properties, properties['communityAPI'] + community);
});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['errorcommunity']));
return obs;
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId']);
}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
const path = '/' + route.path + document.location.search;
return this.check(ConnectHelper.getCommunityFromPath(path));
}
}

View File

@ -8,8 +8,6 @@ import {
Route
} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import {ConnectHelper} from '../connectHelper';
@ -22,37 +20,21 @@ export class ConnectRIGuard implements CanActivate, CanLoad {
private propertiesService: EnvironmentSpecificService) {
}
check(community: string, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN;
if (Session.isLoggedIn()) {
if (Session.isPortalAdministrator()) {
return true;
} else {
check(community: string): Observable<boolean> | boolean {
const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
return this.communityService.iscommunityRI(properties, properties['communityAPI'] + community);
return this.communityService.isRIType(properties, properties['communityAPI'] + community);
});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], {
queryParams: {
'errorCode': errorCode,
'redirectUrl': path
}
}));
.subscribe(() => this.router.navigate(['errorcommunity']));
return obs;
}
} else {
errorCode = LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
return false;
}
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route.queryParams['communityId'], state.url);
return this.check(route.queryParams['communityId']);
}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
const path = '/' + route.path + document.location.search;
return this.check(ConnectHelper.getCommunityFromPath(path), path);
return this.check(ConnectHelper.getCommunityFromPath(path));
}
}

View File

@ -31,7 +31,6 @@ export class IsCommunity implements CanActivate, CanLoad {
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
const path = '/' + route.path + document.location.search;
console.log(path)
return this.check(ConnectHelper.getCommunityFromPath(path));
}
}

View File

@ -15,7 +15,7 @@ export class ConnectHelper {
} else {
domain = domain.substr(0, domain.indexOf('.'));
}
if(domain == "connect" || domain == "explore" || domain == "monitor"){
if (domain === 'connect' || domain === 'explore' || domain === 'monitor'){
return null;
}
return domain;

View File

@ -30,12 +30,12 @@ export class IsRouteEnabled implements CanActivate, CanLoad {
if (!community) {
community = ConnectHelper.getCommunityFromDomain(res.domain);
}
return res["adminToolsAPIURL"]
return res['adminToolsAPIURL'];
}).mergeMap(url => {
if (!community) { // no community to check - return true
return Observable.of(true);
}
return this.config.isPageEnabled(url, community,"/"+path.split("?")[0].substring(1))
return this.config.isPageEnabled(url, community, '/' + path.split('?')[0].substring(1));
});
obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate([redirect], {queryParams: {'page': path}}));

View File

@ -1,21 +1,9 @@
import { Router} from '@angular/router';
// export class GuardHelper{
// constructor(private router: Router) {}
//
// redirect(url:string, errorCode:number, redirectUrl:string){
// this.router.navigate([url], { queryParams: { "errorCode": errorCode, "redirectUrl": redirectUrl } });
//
// }
//
// }
export class LoginErrorCodes {
public static NOT_LOGIN: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;
public static NOT_SUBSCRIBER:number =6;
public static NOT_LOGIN = 1;
public static NOT_ADMIN = 2;
public static NOT_VALID = 3;
public static NOT_CONNECT_ADMIN = 4;
public static NO_COMMUNITY = 5;
public static NOT_SUBSCRIBER = 6;
}