{ "version": 3, "sources": ["src/app/core/auth/guards/app-access.guard.ts"], "sourcesContent": ["// why use a route guard instead of a resolve?\n// a. we don't need a resolve because we are using a store\n// b. it gives us a benefit of controlling navigation to navigate away, etc\nimport { Injectable } from '@angular/core';\n\nimport { Store } from '@ngrx/store';\nimport { Observable, of } from 'rxjs';\nimport { catchError, filter, map, switchMap, take, withLatestFrom } from 'rxjs/operators';\nimport { UserService } from 'src/app/core/services/user.service';\nimport * as fromActions from 'src/app/root-store/global-store/store/actions';\nimport * as fromReducers from 'src/app/root-store/global-store/store/reducers';\nimport * as fromSelectors from 'src/app/root-store/global-store/store/selectors';\nimport { getAppModuleDisplayName } from '../../../shared/helpers/app-modules.helper';\n\n@Injectable()\nexport class AppAccessGuard {\n constructor(private _store: Store) {}\n\n canActivate(): Observable {\n return this.checkStore().pipe(\n // returns observable of true if things have gone correctly\n switchMap(() => of(true)),\n // else returns observable of false if things have gone awry\n catchError(() => of(false))\n );\n }\n\n canActivateChild(): Observable {\n return this.canActivate();\n }\n\n checkStore(): Observable {\n return this.waitForPermissionsToLoad().pipe(switchMap(() => this.hasAppAccess()));\n }\n\n hasAppAccess(): Observable {\n return this._store.select(fromSelectors.getUserPermissions).pipe(\n withLatestFrom(this._store.select(fromSelectors.getSystemInfoActiveAppModule)),\n map(([permissions, activeModule]) => {\n const hasAccess = UserService.hasAppModuleAccess(permissions, activeModule);\n\n if (!hasAccess) {\n const moduleName = getAppModuleDisplayName(activeModule);\n const errorMsg = `You lack access to ${moduleName}`;\n this._store.dispatch(new fromActions.RouteToHomePage(errorMsg));\n throw new Error('no access');\n }\n\n return hasAccess;\n }),\n take(1)\n );\n }\n\n /**\n * This method creates an observable that waits for the `loaded` property\n * of the collection state to turn `true`, emitting one time once loading\n * has finished.\n */\n waitForPermissionsToLoad(): Observable {\n return this._store.select(fromSelectors.getUserPermissionsLoaded).pipe(\n filter((loaded) => loaded),\n take(1)\n );\n }\n}\n"], "mappings": "0JAeA,IAAaA,GAAc,IAAA,CAArB,MAAOA,CAAc,CACzBC,YAAoBC,EAAuC,CAAvC,KAAAA,OAAAA,CAA0C,CAE9DC,aAAW,CACT,OAAO,KAAKC,WAAU,EAAGC,KAEvBC,EAAU,IAAMC,EAAG,EAAI,CAAC,EAExBC,EAAW,IAAMD,EAAG,EAAK,CAAC,CAAC,CAE/B,CAEAE,kBAAgB,CACd,OAAO,KAAKN,YAAW,CACzB,CAEAC,YAAU,CACR,OAAO,KAAKM,yBAAwB,EAAGL,KAAKC,EAAU,IAAM,KAAKK,aAAY,CAAE,CAAC,CAClF,CAEAA,cAAY,CACV,OAAO,KAAKT,OAAOU,OAAqBC,CAAkB,EAAER,KAC1DS,EAAe,KAAKZ,OAAOU,OAAqBG,CAA4B,CAAC,EAC7EC,EAAI,CAAC,CAACC,EAAaC,CAAY,IAAK,CAClC,IAAMC,EAAYC,EAAYC,mBAAmBJ,EAAaC,CAAY,EAE1E,GAAI,CAACC,EAAW,CAEd,IAAMG,EAAW,sBADEC,EAAwBL,CAAY,CACN,GACjD,WAAKhB,OAAOsB,SAAS,IAAgBC,EAAgBH,CAAQ,CAAC,EACxD,IAAII,MAAM,WAAW,CAC7B,CAEA,OAAOP,CACT,CAAC,EACDQ,EAAK,CAAC,CAAC,CAEX,CAOAjB,0BAAwB,CACtB,OAAO,KAAKR,OAAOU,OAAqBgB,CAAwB,EAAEvB,KAChEwB,EAAQC,GAAWA,CAAM,EACzBH,EAAK,CAAC,CAAC,CAEX,iDAjDW3B,GAAc+B,EAAAC,CAAA,CAAA,CAAA,CAAA,iCAAdhC,EAAciC,QAAdjC,EAAckC,SAAA,CAAA,CAAA,SAAdlC,CAAc,GAAA", "names": ["AppAccessGuard", "constructor", "_store", "canActivate", "checkStore", "pipe", "switchMap", "of", "catchError", "canActivateChild", "waitForPermissionsToLoad", "hasAppAccess", "select", "getUserPermissions", "withLatestFrom", "getSystemInfoActiveAppModule", "map", "permissions", "activeModule", "hasAccess", "UserService", "hasAppModuleAccess", "errorMsg", "getAppModuleDisplayName", "dispatch", "RouteToHomePage", "Error", "take", "getUserPermissionsLoaded", "filter", "loaded", "\u0275\u0275inject", "Store", "factory", "\u0275fac"] }