[Library | Trunk]: Add canActivateChild on Monitor and login Guards

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@60537 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2021-03-01 18:19:53 +00:00
parent 39dada9c53
commit 24f3f8fe82
4 changed files with 58 additions and 15 deletions

View File

@ -9,6 +9,7 @@ import {Role, Session, User} from "../../../login/utils/helper.class";
import {UserManagementService} from "../../../services/user-management.service"; import {UserManagementService} from "../../../services/user-management.service";
import {Router} from "@angular/router"; import {Router} from "@angular/router";
import {StringUtils} from "../../../utils/string-utils.class"; import {StringUtils} from "../../../utils/string-utils.class";
import {NotificationService} from "../../../notifications/notification.service";
declare var UIkit; declare var UIkit;
@ -32,6 +33,8 @@ export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
public message: string = null; public message: string = null;
@Input() @Input()
public emailComposer: Function; public emailComposer: Function;
@Input()
public notificationFn: Function;
public user: User = null; public user: User = null;
public active: any[]; public active: any[];
public pending: any[]; public pending: any[];
@ -51,15 +54,16 @@ export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
constructor(private userRegistryService: UserRegistryService, constructor(private userRegistryService: UserRegistryService,
private userManagementService: UserManagementService, private userManagementService: UserManagementService,
private notificationService: NotificationService,
private router: Router, private router: Router,
private fb: FormBuilder) { private fb: FormBuilder) {
} }
ngOnInit() { ngOnInit() {
this.updateLists(); this.updateLists();
this.userManagementService.getUserInfo().subscribe(user => { this.userManagementService.getUserInfo().subscribe(user => {
this.user = user; this.user = user;
}); });
} }
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
@ -141,7 +145,7 @@ export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
deleteActive() { deleteActive() {
this.loadActive = true; this.loadActive = true;
this.userRegistryService.remove(this.type, this.id, this.selectedUser, this.role).subscribe(() => { this.subs.push(this.userRegistryService.remove(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
this.active = this.active.filter(user => user.email != this.selectedUser); this.active = this.active.filter(user => user.email != this.selectedUser);
this.userManagementService.updateUserInfo(); this.userManagementService.updateUserInfo();
UIkit.notification(this.selectedUser + ' <b>is no longer</b> ' + this.role + ' of ' + this.name + ' Dashboard', { UIkit.notification(this.selectedUser + ' <b>is no longer</b> ' + this.role + ' of ' + this.name + ' Dashboard', {
@ -157,12 +161,12 @@ export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
pos: 'bottom-right' pos: 'bottom-right'
}); });
this.loadActive = false; this.loadActive = false;
}); }));
} }
deletePending() { deletePending() {
this.loadPending = true; this.loadPending = true;
this.userRegistryService.cancelInvitation(this.type, this.id, this.selectedUser, this.role).subscribe(() => { this.subs.push(this.userRegistryService.cancelInvitation(this.type, this.id, this.selectedUser, this.role).subscribe(() => {
this.pending = this.pending.filter(user => user != this.selectedUser); this.pending = this.pending.filter(user => user != this.selectedUser);
UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>canceled</b>', { UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>canceled</b>', {
status: 'success', status: 'success',
@ -177,7 +181,7 @@ export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
pos: 'bottom-right' pos: 'bottom-right'
}); });
this.loadPending = false; this.loadPending = false;
}); }));
} }
invite() { invite() {
@ -188,10 +192,25 @@ export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
link: this.link, link: this.link,
email: this.emailComposer(this.name, this.invited.value, this.role) email: this.emailComposer(this.name, this.invited.value, this.role)
} }
this.userRegistryService.invite(this.type, this.id, details, this.role).subscribe(() => { this.subs.push(this.userRegistryService.invite(this.type, this.id, details, this.role).subscribe(invitation => {
if (!this.pending.includes(this.invited.value)) { if (!this.pending.includes(this.invited.value)) {
this.pending.push(this.invited.value); this.pending.push(this.invited.value);
} }
if(this.notificationFn) {
this.subs.push(this.notificationService.sendNotification(this.notificationFn(this.name, this.invited.value, this.role, invitation)).subscribe(notification => {
UIkit.notification('A notification has been <b>sent</b> successfully', {
status: 'success',
timeout: 6000,
pos: 'bottom-right'
});
}, error => {
UIkit.notification('An error has occurred. Please try again later', {
status: 'danger',
timeout: 6000,
pos: 'bottom-right'
});
}));
}
UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>sent</b>', { UIkit.notification(StringUtils.capitalize(this.role) + ' invitation to ' + this.selectedUser + ' has been <b>sent</b>', {
status: 'success', status: 'success',
timeout: 6000, timeout: 6000,
@ -205,7 +224,7 @@ export class RoleUsersComponent implements OnInit, OnDestroy, OnChanges {
pos: 'bottom-right' pos: 'bottom-right'
}); });
this.loadPending = false; this.loadPending = false;
}) }));
} }
createGroup() { createGroup() {

View File

@ -10,9 +10,10 @@ import {IconsModule} from "../../../utils/icons/icons.module";
import {InputModule} from "../../../sharedComponents/input/input.module"; import {InputModule} from "../../../sharedComponents/input/input.module";
import {PageContentModule} from "../../sharedComponents/page-content/page-content.module"; import {PageContentModule} from "../../sharedComponents/page-content/page-content.module";
import {SafeHtmlPipeModule} from "../../../utils/pipes/safeHTMLPipe.module"; import {SafeHtmlPipeModule} from "../../../utils/pipes/safeHTMLPipe.module";
import {NotifyFormModule} from "../../../notifications/notify-form/notify-form.module";
@NgModule({ @NgModule({
imports: [CommonModule, AlertModalModule, ReactiveFormsModule, LoadingModule, IconsModule, InputModule, PageContentModule, SafeHtmlPipeModule], imports: [CommonModule, AlertModalModule, ReactiveFormsModule, LoadingModule, IconsModule, InputModule, PageContentModule, SafeHtmlPipeModule, NotifyFormModule],
declarations: [RoleUsersComponent], declarations: [RoleUsersComponent],
exports: [RoleUsersComponent] exports: [RoleUsersComponent]
}) })

View File

@ -1,5 +1,12 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router'; import {
ActivatedRouteSnapshot,
CanActivate,
CanActivateChild,
Router,
RouterStateSnapshot,
UrlTree
} from '@angular/router';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import {Session} from './utils/helper.class'; import {Session} from './utils/helper.class';
import {LoginErrorCodes} from './utils/guardHelper.class'; import {LoginErrorCodes} from './utils/guardHelper.class';
@ -7,7 +14,7 @@ import {UserManagementService} from "../services/user-management.service";
import {map, tap} from "rxjs/operators"; import {map, tap} from "rxjs/operators";
@Injectable() @Injectable()
export class AdminLoginGuard implements CanActivate { export class AdminLoginGuard implements CanActivate, CanActivateChild {
constructor(private router: Router, constructor(private router: Router,
private userManagementService: UserManagementService) { private userManagementService: UserManagementService) {
@ -37,4 +44,8 @@ export class AdminLoginGuard implements CanActivate {
return this.check(state.url); return this.check(state.url);
} }
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.check(state.url);
}
} }

View File

@ -1,5 +1,13 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, CanActivate, CanLoad, Route, Router, RouterStateSnapshot} from '@angular/router'; import {
ActivatedRouteSnapshot,
CanActivate,
CanActivateChild,
CanLoad,
Route,
Router,
RouterStateSnapshot, UrlTree
} from '@angular/router';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {Session} from './utils/helper.class'; import {Session} from './utils/helper.class';
import {LoginErrorCodes} from './utils/guardHelper.class'; import {LoginErrorCodes} from './utils/guardHelper.class';
@ -7,7 +15,7 @@ import {map, tap} from "rxjs/operators";
import {UserManagementService} from "../services/user-management.service"; import {UserManagementService} from "../services/user-management.service";
@Injectable() @Injectable()
export class LoginGuard implements CanActivate, CanLoad { export class LoginGuard implements CanActivate, CanLoad, CanActivateChild {
constructor(private router: Router, constructor(private router: Router,
private userManagementService: UserManagementService) { private userManagementService: UserManagementService) {
@ -37,6 +45,10 @@ export class LoginGuard implements CanActivate, CanLoad {
return this.check(state.url); return this.check(state.url);
} }
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.check(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('/' + route.path);
} }