<?php
namespace OfficeBrain\Bundle\AccessManagementBundle\Manager\AbstractManager;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
 * @author OfficeBrain 4273 <info@officebrain.com>
 *
 * Description : This Abstract class will manage user Access authorization.
 *
 **/

abstract class AccessAuthorizedExceptionAbstract
{
    protected $entityManager;
    protected $request;
    protected $securityContext;
    
    public function __construct($entityManager, $request, $securityContext)
    {
    	
        $this->entityManager=$entityManager;
        $this->request=$request;
        $this->securityContext=$securityContext;
        
        $this->project_setting=$this->request->get('project_setting');
        $this->project_user_setting=$this->request->get('project_user_setting');
        $this->instance_id=$this->project_setting['instance_id'];
        //$this->{$repoName}=$this->entityManager->getRepository("$bundleName:".$entityName);
        
    }
    
    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->securityContext);
        
    }
    
    /**
     * @author Employee Id: 4273
     * OB eCommerce Product - User Allowed To Access
     * Function created to get flag for user authorization
     * returns boolean
     * @return boolean
     */
    public function  isUserAllowedToAccess($createdUid, $allowUsers=null){
    	
    	if($this->securityContext->getToken() && is_object($this->securityContext->getToken()->getUser())){
    		$userObj = $this->securityContext->getToken()->getUser();
    		if($createdUid==$userObj->getId() || 
    				(!empty($userObj->getParent()) && is_array($allowUsers) && in_array($userObj->getUserType(), $allowUsers) && $userObj->getParent()->getId()==$createdUid)){
	    		return true;
	    	}
    	}
    	return true;
    }
    
    /**
     * @author Employee Id: 4273
     * OB eCommerce Product - throw Access exception
     * Function created to get flag for user authorization
     * returns error code
     * @return error code
     */
    public function  throwAccessException(){
    	throw new NotFoundHttpException('Sorry record not existing!');
    }
    

}
