import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router'; import { Observable } from 'rxjs'; import { PermissionService } from '../services/permission.service'; import { SessionStore } from '../store/session.service'; import { RouteService } from 'src/app/services/route.service' import { FirstEnterService } from 'src/app/services/first-enter.service' import { AlertController, Platform } from '@ionic/angular'; @Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate { constructor( private router:Router, public p: PermissionService, ){} canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { // if user not active or no session if(!SessionStore.user.Inactivity || !SessionStore.exist) { if(SessionStore.user.LoginPreference == 'Pin') { this.router.navigate(['/pin']); } else { this.router.navigate(['/']); } return false } else { const pathname = state.url if(pathname.startsWith('/home/agenda')) { if(this.p.userPermission(this.p.permissionList.Agenda.access)) { return true; } else { this.router.navigate(['/login']); return false; } } else if ( pathname.startsWith('/home/gabinete-digital')) { if(this.p.userPermission(this.p.permissionList.Gabinete.access)) { return true; } else { this.router.navigate(['/login']); return false; } } else if(pathname.startsWith('/home/chat')) { if(this.p.userPermission(this.p.permissionList.Chat.access)) { return true; } else { this.router.navigate(['/login']); return false; } } else if(pathname.startsWith('/home/publications')) { if(this.p.userPermission(this.p.permissionList.Actions.access)) { return true } else { this.router.navigate(['/login']); return false } } else if (pathname.startsWith('/home/events')) { if(SessionStore.user.OwnerCalendars.length >= 1 || this.p.userPermission([this.p.permissionList.Gabinete.access])) { return true } else { this.router.navigate(['/login']); return false } } else if (pathname == '/') { this.router.navigate(['/login']); return false } else { this.router.navigate(['/login']); return false } } return true } }