Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { Principal } from '../';
import { StateStorageService } from './state-storage.service';
@Injectable()
export class UserRouteAccessService implements CanActivate {
constructor(private router: Router,
private principal: Principal,
private stateStorageService: StateStorageService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Promise<boolean> {
const authorities = route.data['authorities'];
// We need to call the checkLogin / and so the principal.identity() function, to ensure,
// that the client has a principal too, if they already logged in by the server.
// This could happen on a page refresh.
return this.checkLogin(authorities, state.url);
}
checkLogin(authorities: string[], url: string): Promise<boolean> {
const principal = this.principal;
return Promise.resolve(principal.identity().then((account) => {
if (!authorities || authorities.length === 0) {
return true;
}
if (account) {
return principal.hasAnyAuthority(authorities).then((response) => {
if (response) {
return true;
}
return false;
});
}
this.stateStorageService.storeUrl(url);
this.router.navigate(['accessdenied']).then(() => {
// only show the login dialog, if the user hasn't logged in yet
if (!account) {
}
});
return false;
}));
}
}