SHARE
TWEET

Untitled

a guest Aug 19th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Injectable} from "@angular/core";
  2. import {Router, CanActivate, ActivatedRouteSnapshot, ActivatedRoute} from "@angular/router";
  3. import {User} from "../_models/user";
  4. import {UsersService} from "../_services/users.service";
  5. import {NavigationSharedService} from "../_services/navigation.shared.service";
  6. import {PathPermission} from "../_models/path.permission";
  7.  
  8. @Injectable()
  9. export class AuthGuard implements CanActivate {
  10.   pathPermissionMap: PathPermission[];
  11.   currentUser: User;
  12.  
  13.   constructor(private router: Router,
  14.               private route: ActivatedRoute,
  15.               private userService: UsersService,
  16.               public navigationSharedService: NavigationSharedService) {
  17.  
  18.     this.pathPermissionMap = [];
  19.     this.currentUser = UsersService.getLoggedInUser() ? UsersService.getLoggedInUser() : this.authenticate();
  20.     this.pathPermissionMap.push(new PathPermission('valiskasutaja-vordlus-esindaja', 'permissions.uc8.comparison.external', 'EXTERNAL_USER'));
  21.     this.pathPermissionMap.push(new PathPermission('valiskasutaja-vordlus-esindaja', 'permissions.uc8.comparison.official.external', 'INTERNAL'));
  22.     this.pathPermissionMap.push(new PathPermission('paketid', 'permissions.uc4.packagelist.view', null));
  23.     this.pathPermissionMap.push(new PathPermission('paketid-lisa', 'permissions.uc4.packagelist.insert', null));
  24.     this.pathPermissionMap.push(new PathPermission('paketid-muuda', 'permissions.uc4.packagelist.view', null));
  25.     this.pathPermissionMap.push(new PathPermission('paasuoigus-loobu', 'permissions.uc20.accesspermission.discard', 'INTERNAL'));
  26.     this.pathPermissionMap.push(new PathPermission('paasuoigus-muuda', 'permissions.uc3.accesspermission.update', null));
  27.     this.pathPermissionMap.push(new PathPermission('paasuoigus-lisa', 'permissions.uc3.accesspermission.insert', null));
  28.     this.pathPermissionMap.push(new PathPermission('valiskasutaja-paasuoigused', 'permissions.uc24.externaluser', null));
  29.     this.pathPermissionMap.push(new PathPermission('valiskasutaja-nimele-antud', 'permissions.uc24.externaluser', null));
  30.     this.pathPermissionMap.push(new PathPermission('valisametniku-paasuoigused', 'permissions.uc26.externalofficial', null));
  31.     this.pathPermissionMap.push(new PathPermission('valisametniku-nimele-antud', 'permissions.uc26.externalofficial', null));
  32.     this.pathPermissionMap.push(new PathPermission('profiilid-nimekiri', 'permissions.uc23.profilelist.view', null));
  33.     this.pathPermissionMap.push(new PathPermission('profiilid-vaatamine-muutmine', 'permissions.uc23.profilelist.edit', null));
  34.     this.pathPermissionMap.push(new PathPermission('ametnik-vordlus-valik', 'permissions.uc8.comparison.official.official', null));
  35.     this.pathPermissionMap.push(new PathPermission('valiskasutaja-vordlus-esindatav', 'permissions.uc8.comparison.official.external', null));
  36.     this.pathPermissionMap.push(new PathPermission('isiku-nimele-antud', 'permissions.uc2.principalaccesspermissions.view', null));
  37.     this.pathPermissionMap.push(new PathPermission('mulle-esitatud-taotlused', 'permissions.uc32.applications', null));
  38.     this.pathPermissionMap.push(new PathPermission('minu-tehtud-taotlused', 'permissions.uc31.applications', null));
  39.     this.pathPermissionMap.push(new PathPermission('sobralist', 'permissions.uc28.favourites', null));
  40.     this.pathPermissionMap.push(new PathPermission('ametniku-paasuoigused', 'permissions.uc25.official', null));
  41.     this.pathPermissionMap.push(new PathPermission('ametniku-nimele-antud', 'permissions.uc25.official', null));
  42.     this.pathPermissionMap.push(new PathPermission('keskne-logi', 'permissions.uc37.accesspermission.view.central.log', null));
  43.     this.pathPermissionMap.push(new PathPermission('keskne-logi-esindajad', 'permissions.uc18.accesspermission.view.representatives.central.log', null));
  44.  
  45.   }
  46.  
  47.   checkPermissions(route: ActivatedRouteSnapshot) {
  48.     if(this.currentUser) {
  49.       let loaderName = 'hasPermission_' + (new Date()).getTime() / 1000;
  50.       this.navigationSharedService.startLoading(loaderName);
  51.       if (this.currentUser.jwt == null) {
  52.         this.navigationSharedService.stopLoading(loaderName);
  53.         console.log("soustin")
  54.         this.router.navigate(['']);
  55.         return false;
  56.       } else {
  57.         for (let pathPermission of this.pathPermissionMap) {
  58.           if (route.routeConfig.path === pathPermission.path) {
  59.             let doCheck: boolean = false;
  60.             if (pathPermission.checkOnlyForUserType === 'INTERNAL' && this.currentUser.userType === 'INTERNAL') {
  61.               doCheck = true;
  62.             } else if (pathPermission.checkOnlyForUserType === 'EXTERNAL_USER' && this.currentUser.userType === 'EXTERNAL_USER') {
  63.               doCheck = true;
  64.             } else if (pathPermission.checkOnlyForUserType === 'EXTERNAL_OFFICIAL' && this.currentUser.userType === 'EXTERNAL_OFFICIAL') {
  65.               doCheck = true;
  66.             } else if (pathPermission.checkOnlyForUserType == null) {
  67.               doCheck = true;
  68.             }
  69.  
  70.             if (doCheck) {
  71.               return this.userService.hasPermission(pathPermission.permission).then(result => {
  72.                 this.navigationSharedService.stopLoading(loaderName);
  73.                 return true;
  74.               }).catch((err: any) => {
  75.                 this.navigationSharedService.stopLoading(loaderName);
  76.                 this.router.navigate(['/oigused-puuduvad'], {relativeTo: this.route, replaceUrl: true});
  77.                 return false;
  78.               });
  79.             }
  80.           }
  81.         }
  82.  
  83.       }
  84.  
  85.       this.navigationSharedService.stopLoading(loaderName);
  86.       return true;
  87.     }
  88.   }
  89.  
  90.   canActivate(route: ActivatedRouteSnapshot)  {
  91.     return this.checkPermissions(route);
  92.   }
  93.  
  94.   authenticate(){
  95.     let userPreferences = this.navigationSharedService.getUserPreferences();
  96.     let accessToken = (<any>window).accessToken;
  97.     let role = (<any>window).role;
  98.     let sessionId = (<any>window).sessionId;
  99.  
  100.    return new User(this.userService.getCurrentUser(accessToken, role, sessionId, "0", "0", userPreferences.languageIso639_2).then(user => {
  101.       this.currentUser = user;
  102.       return this.currentUser;
  103.     }));
  104.  
  105.   }
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top