[Trunk|Library]: CanLoad does not work in connect guards, maybe it works in angular 6

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@54912 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2019-02-26 15:25:52 +00:00
parent 0bddc14435
commit 75533e17bf
9 changed files with 163 additions and 216 deletions

View File

@ -1,7 +1,5 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http'; import {Http} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import {Observable} from 'rxjs/Rx';
import {CommunityInfo} from '../community/communityInfo'; import {CommunityInfo} from '../community/communityInfo';
import {EnvProperties} from '../../utils/properties/env-properties'; import {EnvProperties} from '../../utils/properties/env-properties';
@ -18,67 +16,13 @@ export class CommunitiesService {
} }
parseCommunities(data: any): CommunityInfo[] { parseCommunities(data: any): CommunityInfo[] {
let communities: CommunityInfo[] = []; const communities: CommunityInfo[] = [];
let length = Array.isArray(data) ? data.length :1; const length = Array.isArray(data) ? data.length : 1;
for (let i = 0; i < length; i++) { for (let i = 0; i < length; i++) {
let resData = Array.isArray(data) ? data[i] : data; const resData = Array.isArray(data) ? data[i] : data;
const result: CommunityInfo = new CommunityInfo();
var result: CommunityInfo = new CommunityInfo();
//TODO @Sofia remove this if/else
// if there is case to be array do with resData what you do with data ( let resData = Array.isArray(data) ? data[i] : data;)
//don;t repeat the same code
if(Array.isArray(resData)) {
result['title'] = resData[0].name;
result['shortTitle'] = resData[0].shortName;
result['communityId'] = resData[0].id;
result['queryId'] = resData[0].queryId;
result['logoUrl'] = resData[0].logoUrl;
result['description'] = resData[0].description;
result['date'] = resData[0].creationDate;
result['status'] = "all";
if(resData[0].hasOwnProperty('status')){
result['status'] = resData[0].status;
var status = ["all","hidden","manager"];
if(status.indexOf(result['status']) ==-1){
result['status'] = "hidden";
}
}
if (resData[0].type != null) {
result['type'] = resData[0].type;
}
if(resData[0].managers != null) {
if(result['managers'] == undefined) {
result['managers'] = new Array<string>();
}
let managers = resData[0].managers;
let length = Array.isArray(managers) ? managers.length : 1;
for(let i=0; i<length; i++) {
let manager = Array.isArray(managers) ? managers[i] : managers;
result.managers[i] = manager;
}
}
if(resData[0].subjects != null) {
if(result['subjects'] == undefined) {
result['subjects'] = new Array<string>();
}
let subjects = resData[0].subjects;
let length = Array.isArray(subjects) ? subjects.length : 1;
for(let i=0; i<length; i++) {
let subject = Array.isArray(subjects) ? subjects[i] : subjects;
result.subjects[i] = subject;
}
}
} else {
result['title'] = resData.name; result['title'] = resData.name;
result['shortTitle'] = resData.shortName; result['shortTitle'] = resData.shortName;
result['communityId'] = resData.id; result['communityId'] = resData.id;
@ -86,12 +30,12 @@ export class CommunitiesService {
result['logoUrl'] = resData.logoUrl; result['logoUrl'] = resData.logoUrl;
result['description'] = resData.description; result['description'] = resData.description;
result['date'] = resData.creationDate; result['date'] = resData.creationDate;
result['status'] = "all"; result['status'] = 'all';
if (resData.hasOwnProperty('status')) { if (resData.hasOwnProperty('status')) {
result['status'] = resData.status; result['status'] = resData.status;
var status = ["all","hidden","manager"]; const status = ['all', 'hidden', 'manager'];
if(status.indexOf(result['status']) ==-1){ if (status.indexOf(result['status']) === -1) {
result['status'] = "hidden"; result['status'] = 'hidden';
} }
} }
if (resData.type != null) { if (resData.type != null) {
@ -99,38 +43,36 @@ export class CommunitiesService {
} }
if (resData.managers != null) { if (resData.managers != null) {
if(result['managers'] == undefined) { if (result['managers'] === undefined) {
result['managers'] = new Array<string>(); result['managers'] = new Array<string>();
} }
let managers = resData.managers; const managers = resData.managers;
let length = Array.isArray(managers) ? managers.length : 1; const lengthManagers = Array.isArray(managers) ? managers.length : 1;
for(let i=0; i<length; i++) { for (let j = 0; j < lengthManagers; j++) {
let manager = Array.isArray(managers) ? managers[i] : managers; const manager = Array.isArray(managers) ? managers[j] : managers;
result.managers[i] = manager; result.managers[j] = manager;
} }
} }
if (resData.subjects != null) { if (resData.subjects != null) {
if(result['subjects'] == undefined) { if (result['subjects'] === undefined) {
result['subjects'] = new Array<string>(); result['subjects'] = new Array<string>();
} }
let subjects = resData.subjects; const subjects = resData.subjects;
let length = Array.isArray(subjects) ? subjects.length : 1; const lengthSubjects = Array.isArray(subjects) ? subjects.length : 1;
for(let i=0; i<length; i++) { for (let j = 0; j < lengthSubjects; j++) {
let subject = Array.isArray(subjects) ? subjects[i] : subjects; const subject = Array.isArray(subjects) ? subjects[i] : subjects;
result.subjects[i] = subject; result.subjects[j] = subject;
} }
} }
} if (result['type'] === 'community' || result['type'] === 'ri') {
if (result['type'] == 'community' || result['type'] == "ri") {
communities.push(result); communities.push(result);
} }
} }
return communities; return communities;
} }
} }

View File

@ -12,47 +12,45 @@ import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
import {CommunityService} from '../community/community.service'; import {CommunityService} from '../community/community.service';
import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service'; import { EnvironmentSpecificService} from '../../utils/properties/environment-specific.service';
import { mergeMap } from 'rxjs/operators';
@Injectable() @Injectable()
export class ConnectAdminLoginGuard implements CanActivate, CanLoad{ export class ConnectAdminLoginGuard implements CanActivate, CanLoad{
constructor(private router: Router, constructor(private router: Router,
private communityService: CommunityService, private communityService: CommunityService,
private propertiesService:EnvironmentSpecificService, private propertiesService: EnvironmentSpecificService) {}
private route: ActivatedRoute) {}
check(path: string): Observable<boolean> | boolean { check(route: ActivatedRouteSnapshot, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN; let errorCode = LoginErrorCodes.NOT_LOGIN;
let community = this.route.queryParams["communityId"]; const community = route.queryParams['communityId'];
if (Session.isLoggedIn()) { if (Session.isLoggedIn()) {
if (Session.isPortalAdministrator() || Session.isCommunityCurator()) { if (Session.isPortalAdministrator() || Session.isCommunityCurator()) {
return true; return true;
} else { } else {
let obs = this.propertiesService.subscribeEnvironment().map(res => res).mergeMap(properties => { 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) obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], { .subscribe(() => this.router.navigate(['/user-info'], {
queryParams: { queryParams: {
"errorCode": errorCode, 'errorCode': errorCode,
"redirectUrl": path 'redirectUrl': path
} }
})); }));
return obs; return obs;
} }
} else { } else {
errorCode = LoginErrorCodes.NOT_LOGIN; errorCode = LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": path}}); this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
return false; return false;
} }
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(state.url); return this.check(route, state.url);
} }
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check('/' + route.path); return this.check(null, '/' + route.path);
} }
} }

View File

@ -16,41 +16,44 @@ import { EnvironmentSpecificService} from '../../utils/properties/environment-sp
@Injectable() @Injectable()
export class ConnectRIGuard implements CanActivate, CanLoad { export class ConnectRIGuard implements CanActivate, CanLoad {
constructor(private router: Router, private communityService: CommunityService, private propertiesService:EnvironmentSpecificService, private route:ActivatedRoute) {} constructor(private router: Router,
private communityService: CommunityService,
private propertiesService: EnvironmentSpecificService) {
}
check(path: string) : Observable<boolean> | boolean { check(route: ActivatedRouteSnapshot, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_LOGIN; let errorCode = LoginErrorCodes.NOT_LOGIN;
let community = (this.route.queryParams["communityId"]); const community = (route.queryParams['communityId']);
if (Session.isLoggedIn()) { if (Session.isLoggedIn()) {
if (Session.isPortalAdministrator()) { if (Session.isPortalAdministrator()) {
return true; return true;
} else { } else {
let obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
return this.communityService.iscommunityRI(properties, properties["communityAPI"] + community) return this.communityService.iscommunityRI(properties, properties['communityAPI'] + community);
}); });
obs.filter(enabled => !enabled) obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], { .subscribe(() => this.router.navigate(['/user-info'], {
queryParams: { queryParams: {
"errorCode": errorCode, 'errorCode': errorCode,
"redirectUrl": path 'redirectUrl': path
} }
})); }));
return obs; return obs;
} }
} else { } else {
errorCode = LoginErrorCodes.NOT_LOGIN; errorCode = LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": path } }); this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
return false; return false;
} }
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(state.url); return this.check(route, state.url);
} }
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check('/' + route.path); return this.check(null, '/' + route.path);
} }
} }

View File

@ -1,5 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute} from '@angular/router'; import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {Session} from '../../login/utils/helper.class'; import {Session} from '../../login/utils/helper.class';
import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class';
@ -8,36 +8,42 @@ import { EnvironmentSpecificService} from '../../utils/properties/environment-sp
import {ConnectHelper} from '../connectHelper'; import {ConnectHelper} from '../connectHelper';
@Injectable() @Injectable()
export class ConnectSubscriberGuard implements CanActivate { export class ConnectSubscriberGuard implements CanActivate, CanLoad{
constructor(private router: Router, constructor(private router: Router,
private communityService: CommunityService, private communityService: CommunityService,
private propertiesService:EnvironmentSpecificService, private propertiesService: EnvironmentSpecificService) {}
private route: ActivatedRoute) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { check(route: ActivatedRouteSnapshot, path: string): Observable<boolean> | boolean {
let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; let errorCode = LoginErrorCodes.NOT_SUBSCRIBER;
let community = this.route.queryParams["communityId"]; let community = route.queryParams['communityId'];
if (!community && (typeof document !== 'undefined')) { if (!community && (typeof document !== 'undefined')) {
community = ConnectHelper.getCommunityFromDomain(document.location.hostname); community = ConnectHelper.getCommunityFromDomain(document.location.hostname);
} }
if (!Session.isLoggedIn()) { if (!Session.isLoggedIn()) {
errorCode = LoginErrorCodes.NOT_LOGIN; errorCode = LoginErrorCodes.NOT_LOGIN;
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, "redirectUrl": state.url } }); this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
return false; return false;
} } else {
else { const obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => {
let obs = this.propertiesService.subscribeEnvironment().mergeMap(properties => { return this.communityService.isSubscribedToCommunity(community, Session.getUserEmail(), properties['adminToolsAPIURL']);
return this.communityService.isSubscribedToCommunity(community, Session.getUserEmail(), properties["adminToolsAPIURL"])
}); });
obs.filter(enabled => !enabled) obs.filter(enabled => !enabled)
.subscribe(() => this.router.navigate(['/user-info'], { .subscribe(() => this.router.navigate(['/user-info'], {
queryParams: { queryParams: {
"errorCode": errorCode, 'errorCode': errorCode,
"redirectUrl": state.url 'redirectUrl': path
} }
})); }));
return obs; return obs;
} }
} }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(route, state.url);
}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check(null, '/' + route.path);
}
} }

View File

@ -1,6 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { import {
ActivatedRoute,
Router, Router,
CanActivate, CanActivate,
ActivatedRouteSnapshot, ActivatedRouteSnapshot,
@ -13,24 +12,25 @@ import 'rxjs/add/operator/filter';
@Injectable() @Injectable()
export class IsCommunity implements CanActivate, CanLoad { export class IsCommunity implements CanActivate, CanLoad {
constructor(private router: Router, constructor(private router: Router) {
private route: ActivatedRoute) {} }
check(): Observable<boolean> | boolean { check(route: ActivatedRouteSnapshot): Observable<boolean> | boolean {
let community = this.route.queryParams["communityId"]; const community = route.queryParams['communityId'];
if(community && community!="undefined"){ console.log(community);
if (community && community !== 'undefined') {
return true; return true;
} else { } else {
this.router.navigate(['errorcommunity']); this.router.navigate(['errorcommunity']);
}
return false; return false;
} }
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.check(); return this.check(route);
} }
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean { canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
return this.check(); return this.check(null);
} }
} }

View File

@ -7,7 +7,8 @@ import {LoginErrorCodes} from './utils/guardHelper.class';
@Injectable() @Injectable()
export class AdminLoginGuard implements CanActivate, CanLoad { export class AdminLoginGuard implements CanActivate, CanLoad {
constructor(private router: Router) {} constructor(private router: Router) {
}
check(path: string): boolean { check(path: string): boolean {
let loggedIn = false; let loggedIn = false;
@ -21,7 +22,7 @@ export class AdminLoginGuard implements CanActivate, CanLoad {
} }
} }
if (!loggedIn || !isAdmin) { if (!loggedIn || !isAdmin) {
this.router.navigate(['/user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": path}}); this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
} }
return loggedIn && isAdmin; return loggedIn && isAdmin;
} }

View File

@ -7,7 +7,8 @@ import {LoginErrorCodes} from './utils/guardHelper.class';
@Injectable() @Injectable()
export class ClaimsCuratorGuard implements CanActivate, CanLoad { export class ClaimsCuratorGuard implements CanActivate, CanLoad {
constructor(private router: Router) {} constructor(private router: Router) {
}
check(path: string): boolean { check(path: string): boolean {
let loggedIn = false; let loggedIn = false;
@ -18,14 +19,12 @@ export class ClaimsCuratorGuard implements CanActivate, CanLoad {
loggedIn = true; loggedIn = true;
if (Session.isClaimsCurator() || Session.isPortalAdministrator()) { if (Session.isClaimsCurator() || Session.isPortalAdministrator()) {
isAuthorized = true; isAuthorized = true;
} } else {
else {
errorCode = LoginErrorCodes.NOT_ADMIN; errorCode = LoginErrorCodes.NOT_ADMIN;
} }
} }
if (!loggedIn || !isAuthorized) { if (!loggedIn || !isAuthorized) {
// this.guardHelper.redirect("/user-info",errorCode,state.url); this.router.navigate(['/user-info'], {queryParams: {'errorCode': errorCode, 'redirectUrl': path}});
this.router.navigate(['/user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": path}});
} }
return loggedIn && isAuthorized; return loggedIn && isAuthorized;
} }

View File

@ -13,13 +13,13 @@ import {LoginErrorCodes} from './utils/guardHelper.class';
@Injectable() @Injectable()
export class FreeGuard implements CanActivate, CanLoad { export class FreeGuard implements CanActivate, CanLoad {
constructor(private router: Router) {} constructor(private router: Router) {
}
check(path: string): boolean { check(path: string): boolean {
let valid = true; const valid = true;
let errorCode = LoginErrorCodes.NOT_LOGIN;
if (!valid) { if (!valid) {
this.router.navigate(['/user-info'], {queryParams: {"errorCode": errorCode, "redirectUrl": path}}); this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': path}});
} }
return valid; return valid;
} }

View File

@ -7,18 +7,16 @@ import {LoginErrorCodes} from './utils/guardHelper.class';
@Injectable() @Injectable()
export class LoginGuard implements CanActivate, CanLoad { export class LoginGuard implements CanActivate, CanLoad {
constructor(private router: Router) {} constructor(private router: Router) {
}
check(path: string): boolean { check(path: string): boolean {
let loggedIn = false; let loggedIn = false;
let errorCode = LoginErrorCodes.NOT_LOGIN;
if (Session.isLoggedIn()) { if (Session.isLoggedIn()) {
loggedIn = true; loggedIn = true;
} }
if (!loggedIn) { if (!loggedIn) {
// this.guardHelper.redirect("/user-info",errorCode,state.url); this.router.navigate(['/user-info'], {queryParams: {'errorCode': LoginErrorCodes.NOT_LOGIN, path}});
this.router.navigate(['/user-info'], { queryParams: { "errorCode": errorCode, path} });
} }
return loggedIn; return loggedIn;
} }