Files
doneit-web/src/app/guards/inactivity.guard.ts
T

118 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-08-27 13:39:52 +01:00
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { SessionStore } from '../store/session.service';
2021-09-02 12:17:14 +01:00
import { AlertController, Platform } from '@ionic/angular';
import { PermissionService } from '../services/permission.service';
2022-12-26 15:41:11 +01:00
import { FirstEnterService } from '../services/first-enter.service';
2021-08-27 13:39:52 +01:00
@Injectable({
providedIn: 'root'
})
export class InactivityGuard implements CanActivate {
constructor(
2021-09-02 12:17:14 +01:00
private router:Router,
private platform: Platform,
2022-04-07 15:57:22 +01:00
public p: PermissionService,
2022-12-26 15:41:11 +01:00
private alertController: AlertController,
private FirstEnterService: FirstEnterService
2022-04-02 09:40:09 +01:00
) {}
2021-08-27 13:39:52 +01:00
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
2022-12-19 17:04:21 +01:00
console.log('hire inactinity!', state.url)
2022-04-02 09:40:09 +01:00
2022-12-21 15:08:55 +01:00
if ( (this.platform.is('desktop') || this.platform.is('mobileweb')) ) {
2022-12-26 15:41:11 +01:00
2022-12-26 17:04:20 +01:00
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();
})
}
2022-04-07 15:57:22 +01:00
2021-09-02 12:17:14 +01:00
return false
} else if(SessionStore.exist && SessionStore.user.Inactivity && !SessionStore.hasPin ) {
2022-12-20 17:14:23 +01:00
// set pin
2021-08-27 13:39:52 +01:00
return true
2022-12-20 17:14:23 +01:00
} else if(SessionStore.exist && !SessionStore.user.Inactivity && SessionStore.user.LoginPreference == 'Pin') {
// inactivity login
2021-08-27 13:39:52 +01:00
return true
2022-12-21 15:08:55 +01:00
} else if (!SessionStore.user.Inactivity && this.platform.is('mobile')) {
// try to login
return true
} else if (SessionStore.user.LoginPreference != 'Pin' && SessionStore.hasPin) {
2022-12-20 17:14:23 +01:00
// no right to be in this page
this.router.navigateByUrl('/', { replaceUrl: true });
return false
}//Mobile or Tablet without session
else {
2022-04-02 09:40:09 +01:00
2022-12-21 14:53:01 +01:00
if((SessionStore?.user?.Inactivity)) {
2022-12-26 17:04:20 +01:00
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
2023-01-12 15:27:09 +01:00
if(this.p.userPermission(this.p.permissionList.Agenda.access) && SessionStore.user.OwnerCalendars.length == 0) {
2022-12-26 17:04:20 +01:00
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();
})
}
2022-12-26 15:41:11 +01:00
2022-12-21 14:53:01 +01:00
return false
}
2022-04-07 15:57:22 +01:00
2022-12-21 14:53:01 +01:00
2021-08-27 13:39:52 +01:00
}
2021-08-27 13:39:52 +01:00
}
2021-08-27 13:39:52 +01:00
}