import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; import { Observable } from 'rxjs'; import { SessionStore } from '../store/session.service'; import { AlertController, Platform } from '@ionic/angular'; import { PermissionService } from '../services/permission.service'; import { FirstEnterService } from '../services/first-enter.service'; @Injectable({ providedIn: 'root' }) export class InactivityGuard implements CanActivate { constructor( private router:Router, private platform: Platform, public p: PermissionService, private alertController: AlertController, private FirstEnterService: FirstEnterService ) {} canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { console.log('hire inactinity!', state.url) if ( (this.platform.is('desktop') || this.platform.is('mobileweb')) ) { if(this.p.userPermission(this.p.permissionList.Agenda.access) || this.p.userPermission(this.p.permissionList.Gabinete.access)) { //When user has got access to Agenda but does not have their own calendar, goes to Agenda if(this.p.userPermission(this.p.permissionList.Agenda.access) && SessionStore.user.OwnerCalendars.length == 0){ this.router.navigate(['/home/agenda']); } else{ this.router.navigate(['/home/events']); } } else if(this.p.userPermission(this.p.permissionList.Chat.access) && this.p.userPermission(this.p.permissionList.Actions.access)){ this.router.navigate(['/home/chat']); } else if(this.p.userPermission(this.p.permissionList.Actions.access)) { this.router.navigate(['/home/publications']); } else { this.alertController.create({ cssClass: 'my-custom-class', header: 'Utilizador sem acesso a aplicação', buttons: [{ text: 'Ok', handler: () => { } }] }).then( async (alertPopup) => { await alertPopup.present(); }) } return false } else if(SessionStore.exist && SessionStore.user.Inactivity && !SessionStore.hasPin ) { // set pin return true } else if(SessionStore.exist && !SessionStore.user.Inactivity && SessionStore.user.LoginPreference == 'Pin') { // inactivity login return true } else if (!SessionStore.user.Inactivity && this.platform.is('mobile')) { // try to login return true } else if (SessionStore.user.LoginPreference != 'Pin' && SessionStore.hasPin) { // no right to be in this page this.router.navigateByUrl('/', { replaceUrl: true }); return false }//Mobile or Tablet without session else { if((SessionStore?.user?.Inactivity)) { if(this.p.userPermission(this.p.permissionList.Agenda.access) || this.p.userPermission(this.p.permissionList.Gabinete.access)){ //When user has got access to Agenda but does not have their own calendar, goes to Agenda if(this.p.userPermission(this.p.permissionList.Agenda.access) && SessionStore.user.OwnerCalendars.length == 0) { this.router.navigate(['/home/agenda']); } else{ this.router.navigate(['/home/events']); } } else if(this.p.userPermission(this.p.permissionList.Chat.access) && this.p.userPermission(this.p.permissionList.Actions.access)){ this.router.navigate(['/home/chat']); } else if(this.p.userPermission(this.p.permissionList.Actions.access)){ this.router.navigate(['/home/publications']); } else { this.alertController.create({ cssClass: 'my-custom-class', header: 'Utilizador sem acesso a aplicação', buttons: [{ text: 'Ok', handler: () => { } }] }).then( async (alertPopup)=>{ await alertPopup.present(); }) } return false } } } }