Newer
Older

Alex ORLUC
committed
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { HttpClient } from '@angular/common/http';
import { SignaturesContentService } from './signatures.service';
import { TranslateService } from '@ngx-translate/core';
import { AuthService } from './auth.service';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { LocalStorageService } from './local-storage.service';
import { AlertComponent } from '../plugins/alert.component';
import { MatDialog } from '@angular/material/dialog';
import { MenuController } from '@ionic/angular';

Alex ORLUC
committed
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private translate: TranslateService,
public http: HttpClient,
private router: Router,
public signaturesService: SignaturesContentService,
private cookieService: CookieService,
public authService: AuthService,
private localStorage: LocalStorageService,
public dialog: MatDialog,
private menu: MenuController) { }

Alex ORLUC
committed
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
if (route.url.join('/') === 'login') {
if (this.authService.isAuth()) {
this.router.navigate(['/home']);
return false;
} else {
this.http.get('../rest/authenticationInformations').pipe(
map((data: any) => {
this.authService.loginMessage = data.loginMessage;
this.authService.authMode = data.connection;
this.authService.changeKey = data.changeKey;
this.authService.coreUrl = data.coreUrl;
Guillaume Heurtier
committed
this.authService.mailServerOnline = data.mailServerOnline;
this.localStorage.setAppSession(data.instanceId);
})
). subscribe();
} else if (!this.authService.authFailed) {
this.menu.enable(true, 'left-menu');
this.menu.enable(false, 'right-menu');
let tokenInfo = this.authService.getToken();
if (tokenInfo !== null && !this.authService.authFailed) {
if (this.authService.user.id === undefined) {
Guillaume Heurtier
committed
const tokenData = JSON.parse(atob(tokenInfo.split('.')[1]));
this.authService.user = tokenData.user;
this.authService.authMode = tokenData.connection;
this.translate.use(this.authService.user.preferences.lang);
this.cookieService.set('maarchParapheurLang', this.authService.user.preferences.lang);
return true;
} else {
return this.http.get('../rest/authenticationInformations')
.pipe(
map((data: any) => {
this.authService.authMode = data.connection;
this.authService.changeKey = data.changeKey;
this.authService.coreUrl = data.coreUrl;
Guillaume Heurtier
committed
this.authService.mailServerOnline = data.mailServerOnline;
this.localStorage.setAppSession(data.instanceId);
tokenInfo = this.authService.getToken();
if (tokenInfo !== null) {
this.authService.user = JSON.parse(atob(tokenInfo.split('.')[1])).user;
this.translate.use(this.authService.user.preferences.lang);
this.cookieService.set('maarchParapheurLang', this.authService.user.preferences.lang);
if (this.authService.changeKey) {
this.dialog.open(AlertComponent, { autoFocus: false, disableClose: true, data: { mode: 'warning', title: 'lang.warnPrivateKeyTitle', msg: 'lang.warnPrivateKey' } });
}
return true;
} else {
Guillaume Heurtier
committed
this.authService.setCachedUrl(state.url.replace(/^\//g, ''));
this.authService.logout();
return false;

Alex ORLUC
committed
}

Alex ORLUC
committed
}
private setData() {
if (this.authService.signatureRoles.length === 0) {
this.http.get('../rest/signatureModes')
.subscribe((dataModes: any) => {
this.authService.signatureRoles = [
{
id: 'visa',
type: 'visa',
color: '#135F7F'
},
];
this.authService.signatureRoles = this.authService.signatureRoles.concat(dataModes.map((item: any) => ({
...item,
type: item.id === 'note' ? 'note' : 'sign'
});
}
if (this.signaturesService.signaturesList.length === 0) {
this.http.get('../rest/users/' + this.authService.user.id + '/signatures')
.subscribe((dataSign: any) => {
this.signaturesService.signaturesList = dataSign.signatures;
});
}
if (this.authService.user.picture === undefined) {
this.http.get('../rest/users/' + this.authService.user.id + '/picture')
.subscribe((dataPic: any) => {
this.authService.user.picture = dataPic.picture;
});
}
}