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

93 lines
2.8 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'
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,
2022-02-08 14:22:32 +01:00
private localstoreService: LocalstoreService,
private authService: AuthService,
public p: PermissionService,
2022-04-08 13:52:00 +01:00
private RouteService: RouteService
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-02-07 15:51:21 +01:00
// if user not active
2021-08-30 10:24:46 +01:00
if(!SessionStore.user.Inactivity) {
2021-09-03 17:11:13 +01:00
this.router.navigate(['/']);
return false
2021-08-30 10:24:46 +01:00
}
2022-02-07 15:51:21 +01:00
// if session doesn't exit
2021-09-03 17:11:13 +01:00
else if(!SessionStore.exist) {
2021-07-01 15:38:22 +01:00
this.router.navigate(['/']);
return false
} else {
2022-04-08 14:23:04 +01:00
if(this.p.userPermission(this.p.permissionList.Chat.access) == true) {
2022-12-13 17:21:48 +01:00
// this.authService.loginChat();
}
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')) {
2022-04-08 14:23:04 +01:00
2022-04-08 15:44:11 +01:00
if(this.p.userPermission(this.p.permissionList.Agenda.access)) {
2022-04-08 14:23:04 +01:00
return true;
} else {
this.router.navigate(['/login']);
return false;
}
} else if ( pathname.startsWith('/home/gabinete-digital')) {
2022-12-19 15:48:40 +01:00
console.log('gabinete');
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
}
}
2021-07-01 15:38:22 +01:00
}