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

92 lines
2.6 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';
import { PermissionService } from '../services/permission.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 { 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,
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-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(['/']);
}
2023-09-28 09:02:15 +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-09-28 09:02:15 +01:00
2023-01-12 15:27:09 +01:00
return true;
2022-04-08 14:23:04 +01:00
} else {
this.router.navigate(['/login']);
return false;
}
2023-09-28 09:02:15 +01:00
2022-04-08 14:23:04 +01:00
} else if ( pathname.startsWith('/home/gabinete-digital')) {
2023-09-28 09:02:15 +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)) {
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
}