'use strict' ; import { Request } from 'express' ; import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter' ; import { Accounts } from '@Entities/Accounts' ; import { AccountEntity } from '@Entities/AccountEntity' ; import { SimpleObject } from '@Tools/Misc' ; import { Logger } from '@Tools/Logging' ; export class RequestScopeFilter extends CriteriaFilter { private _asAdmin = false ; private _targetAcct : string = undefined ; private _accessingAcct : AccountEntity ; private _fromId = 'requestingAccountId' ; private _toId = 'targetAccountId' ; private _doingQuery : boolean = false ; public constructor ( pRequestorAccount , pFrom = 'requestingAccountId' , pTo = 'targetAccountId' ) { super ( ) ; this . _accessingAcct = pRequestorAccount ; this . _fromId = pFrom ; this . _toId = pTo ; return ; } public parametersFromRequest ( pRequest ) { try { if ( pRequest . query . hasOwnProperty ( 'asAdmin' ) ) { const askingForAdmin = pRequest . query . asAdmin === 'true' ; if ( askingForAdmin ) { if ( Accounts . isAdmin ( this . _accessingAcct ) ) { this . _asAdmin = true ; } ; } ; } ; if ( pRequest . query . acct && typeof ( pRequest . query . acct ) === 'string' ) { this . _targetAcct = pRequest . query . account as string ; } ; Logger . cdebug ( 'query-detail' , `RequestScopeFilter.parametersFromRequest: asAdmin=${ this . _asAdmin }, target=${ this . _targetAcct }` ) ; } catch ( e ) { Logger . error ( 'RequestScopeFilter: parameters from request: exception: ' + e ) ; } ; } ; public addResponseFields ( pRequest ) { return ; } ; public AsAdmin ( ) { return this . _asAdmin ; } ; public criteriaTest ( pToTest ) { if ( ! this . _doingQuery ) { if ( pToTest . hasOwnProperty ( this . _fromId ) && pToTest . hasOwnProperty ( this . _toId ) ) { let getAccountId = this . _accessingAcct . id ; if ( this . _asAdmin ) { if ( typeof ( this . _targetAcct ) !== 'undefined' ) { getAccountId = this . _targetAcct ; } else { return true ; } ; } ; return pToTest [ this . _fromId ] === getAccountId || pToTest [ this . _toId ] === getAccountId ; } ; } ; return true ; } ; public async criteriaTestAsync ( pThingy ) { return this . criteriaTest ( pThingy ) ; } ; public criteriaParameters ( ) { this . _doingQuery = true ; let getAccountId = this . _accessingAcct . id ; if ( this . _asAdmin && typeof ( this . _targetAcct ) !== 'undefined' ) { getAccountId = this . _targetAcct ; } ; const criteria = { } ; if ( ! this . _asAdmin ) { criteria . $or = [ SimpleObject ( this . _fromId , getAccountId ) , SimpleObject ( this . _toId , getAccountId ) ] ; } ; return criteria ; } ; public sortCriteriaParameters ( ) { return undefined ; } ; } ;