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

164 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-07-01 15:38:22 +01:00
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
2022-02-08 14:22:32 +01:00
import { AuthService } from '../services/auth.service';
import { PermissionService } from '../services/permission.service';
2021-07-18 20:20:30 +01:00
import { LocalstoreService } from '../store/localstore.service';
2021-08-27 13:39:52 +01:00
import { SessionStore } from '../store/session.service';
2022-04-08 13:52:00 +01:00
import { RouteService } from 'src/app/services/route.service'
2023-01-03 21:08:49 +01:00
import { FirstEnterService } from 'src/app/services/first-enter.service'
// import { ModalController } from '@ionic/angular';
import { AlertController, Platform } from '@ionic/angular';
2021-07-01 15:38:22 +01:00
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(
2021-07-18 20:20:30 +01:00
private router:Router,
public p: PermissionService,
2023-01-03 21:08:49 +01:00
private RouteService: RouteService,
private FirstEnterService: FirstEnterService,
private alertController: AlertController,
2021-07-01 15:38:22 +01:00
){}
2021-07-01 15:38:22 +01:00
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
2022-02-16 13:52:32 +01:00
2022-12-26 15:41:11 +01:00
console.log('hire auth!', state.url)
2022-12-22 11:01:58 +01:00
2022-12-21 15:08:55 +01:00
// 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(['/']);
2022-12-26 15:41:11 +01:00
// console.log('goto login page')
2022-12-21 15:08:55 +01:00
}
2021-07-01 15:38:22 +01:00
return false
} else {
2022-04-08 15:44:11 +01:00
const pathname = state.url
2022-04-08 13:52:00 +01:00
2022-04-08 15:44:11 +01:00
if(pathname.startsWith('/home/agenda')) {
if(this.p.userPermission(this.p.permissionList.Agenda.access)) {
2023-01-03 21:08:49 +01:00
if((SessionStore.user.OwnerCalendars.length != 0 || SessionStore.user.SharedCalendars.length != 0)) {
return true
}
if(['/pin','/login', '/inactivity' , '', '/'].includes(window.location.pathname)) {
this.FirstEnterService.enter()
setTimeout(() => {
// this.modalController.create({
// component: InformationPage,
// componentProps: {
// },
// cssClass: 'discart-expedient-modal',
// backdropDismiss: false
// });
// this.alertController.create({
// cssClass: 'my-custom-class',
// header: 'utilizador não tem calendário',
// buttons: [{
// text: 'Ok',
// handler: () => {
// }
// }]
// }).then( async (alertPopup) => {
// await alertPopup.present();
// })
}, 1000)
}
return false;
2022-04-08 14:23:04 +01:00
} else {
this.router.navigate(['/login']);
return false;
}
} else if ( pathname.startsWith('/home/gabinete-digital')) {
2022-12-19 17:04:21 +01:00
2022-04-08 13:52:00 +01:00
2022-04-08 14:23:04 +01:00
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)) {
2023-01-03 21:08:49 +01:00
if(!SessionStore.user.ChatData?.data) {
if(['/pin','/login', '/inactivity' , '', '/'].includes(window.location.pathname)) {
this.FirstEnterService.enter()
setTimeout(() => {
// this.modalController.create({
// component: InformationPage,
// componentProps: {
// },
// cssClass: 'discart-expedient-modal',
// backdropDismiss: false
// });
// this.alertController.create({
// cssClass: 'my-custom-class',
// header: 'Serviço temporariamente indisponível',
// buttons: [{
// text: 'Ok',
// handler: () => {
// }
// }]
// }).then( async (alertPopup) => {
// await alertPopup.present();
// })
}, 1000)
}
return false
}
2022-04-08 14:23:04 +01:00
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')) {
2022-04-08 15:44:11 +01:00
if(SessionStore.user.OwnerCalendars.length >= 1 || this.p.userPermission([this.p.permissionList.Gabinete.access])) {
2022-04-08 14:23:04 +01:00
return true
} else {
this.router.navigate(['/login']);
return false
}
2022-04-08 13:52:00 +01:00
} else if (pathname == '/') {
2022-04-08 15:44:11 +01:00
this.router.navigate(['/login']);
return false
2022-04-08 13:52:00 +01:00
} else {
2022-04-08 14:23:04 +01:00
this.router.navigate(['/login']);
return false
2022-04-08 13:52:00 +01:00
}
2021-07-01 15:38:22 +01:00
}
2022-12-19 17:04:21 +01:00
return true
2021-07-01 15:38:22 +01:00
}
2021-07-01 15:38:22 +01:00
}