import { Injectable } from '@angular/core' ; import { Router } from "@angular/router" ; import { Headers , RequestOptions } from "@angular/http" ; @ Injectable ( ) export class ContextService { public user ; public allUsers ; public allGroups ; constructor ( private router ) { } httpHeaders ( ) { let headers = new Headers ( ) ; headers . set ( 'Authorization' , this . getAuthKey ( ) ) ; return headers ; } httpOptions ( ) { let options = new RequestOptions ( { headers : this . httpHeaders ( ) } ) ; return options ; } setLoginCredentials ( userId , authorization ) { localStorage [ 'hyper-directory' ] = JSON . stringify ( { userId : userId , authorization : authorization } ) ; } purge ( ) { this . user = null ; this . allUsers = null ; this . allGroups = null ; localStorage . removeItem ( 'hyper-directory' ) ; } loggedIn ( ) { return ! ! localStorage [ 'hyper-directory' ] ; } setUser ( user ) { this . user = user ; } setAllUsers ( userList ) { this . allUsers = userList ; } setAllGroups ( allGroups ) { this . allGroups = allGroups ; } getUser ( ) { return this . user ; } getAllUsers ( ) { return this . allUsers ; } getAllGroups ( ) { return this . allGroups ; } getUserId ( ) { return JSON . parse ( localStorage [ 'hyper-directory' ] ) . userId ; } getAuthKey ( ) { if ( localStorage [ 'hyper-directory' ] ) { return JSON . parse ( localStorage [ 'hyper-directory' ] ) . authorization ; } } }