Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #152 from TomHAnderson/feature/oauth2-validate
Browse files Browse the repository at this point in the history
Added OAuth2 Server to Query resources
  • Loading branch information
TomHAnderson committed Jan 25, 2015
2 parents 57ac47d + cf5db35 commit f4380c7
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 124 deletions.
102 changes: 102 additions & 0 deletions src/Server/Query/CreateFilter/AbstractCreateFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace ZF\Apigility\Doctrine\Server\Query\CreateFilter;

use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use OAuth2\Server as OAuth2Server;
use OAuth2\Request as OAuth2Request;
use ZF\ApiProblem\ApiProblem;
use ZF\Rest\ResourceEvent;

/**
* Class DefaultCreateFilter
*
* @package ZF\Apigility\Doctrine\Server\Query\CreateFilter
*/
abstract class AbstractCreateFilter implements ObjectManagerAwareInterface, QueryCreateFilterInterface
{
/**
* @param string $entityClass
* @param array $data
*
* @return array
*/
abstract public function filter(ResourceEvent $event, $entityClass, $data);

/**
* @var ObjectManager
*/
protected $objectManager;

/**
* @var OAuth2Server
*/
protected $oAuth2Server;

/**
* Set the object manager
*
* @param ObjectManager $objectManager
*/
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* Get the object manager
*
* @return ObjectManager
*/
public function getObjectManager()
{
return $this->objectManager;
}

/**
* Get the OAuth2 server
*
* @return OAuth2Server
*/
public function getOAuth2Server()
{
return $this->oAuth2Server;
}

/**
* Set the OAuth2 server
*
* @param OAuth2Server
*/
public function setOAuth2Server(OAuth2Server $server)
{
$this->oAuth2Server = $server;

return $this;
}

/**
* Validate an OAuth2 request
*
* @param scope
* @return ApiProblem | bool
*/
public function validateOAuth2($scope = null)
{
if (! $this->getOAuth2Server()->verifyResourceRequest(
OAuth2Request::createFromGlobals(),
$response = null,
$scope = null
)) {
$error = $this->getOAuth2Server()->getResponse();
$parameters = $error->getParameters();
$detail = isset($parameters['error_description']) ?
$parameters['error_description']: $error->getStatusText();

return new ApiProblem($error->getStatusCode(), $detail);
}

return true;
}
}
30 changes: 1 addition & 29 deletions src/Server/Query/CreateFilter/DefaultCreateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace ZF\Apigility\Doctrine\Server\Query\CreateFilter;

use ZF\Apigility\Doctrine\Server\Query\CreateFilter\QueryCreateFilterInterface;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use ZF\ApiProblem\ApiProblem;
use ZF\Rest\ResourceEvent;

Expand All @@ -13,33 +10,8 @@
*
* @package ZF\Apigility\Doctrine\Server\Query\CreateFilter
*/
class DefaultCreateFilter implements ObjectManagerAwareInterface, QueryCreateFilterInterface
class DefaultCreateFilter extends AbstractCreateFilter
{
/**
* @var ObjectManager
*/
protected $objectManager;

/**
* Set the object manager
*
* @param ObjectManager $objectManager
*/
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* Get the object manager
*
* @return ObjectManager
*/
public function getObjectManager()
{
return $this->objectManager;
}

/**
* @param string $entityClass
* @param array $data
Expand Down
134 changes: 134 additions & 0 deletions src/Server/Query/Provider/AbstractQueryProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace ZF\Apigility\Doctrine\Server\Query\Provider;

use ZF\Apigility\Doctrine\Server\Paginator\Adapter\DoctrineOrmAdapter;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\Paginator\Adapter\AdapterInterface;
use OAuth2\Request as OAuth2Request;
use OAuth2\Server as OAuth2Server;
use ZF\ApiProblem\ApiProblem;
use ZF\Rest\ResourceEvent;

/**
* Class FetchAllOrm
*
* @package ZF\Apigility\Doctrine\Server\Query\Provider
*/
abstract class AbstractQueryProvider implements ObjectManagerAwareInterface, QueryProviderInterface
{
/**
* @var ObjectManager
*/
protected $objectManager;

/**
* @var OAuth2Server
*/
protected $oAuth2Server;

/**
* Set the object manager
*
* @param ObjectManager $objectManager
*/
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* Get the object manager
*
* @return ObjectManager
*/
public function getObjectManager()
{
return $this->objectManager;
}

/**
* Get the OAuth2 server
*
* @return OAuth2Server
*/
public function getOAuth2Server()
{
return $this->oAuth2Server;
}

/**
* Set the OAuth2 server
*
* @param OAuth2Server
*/
public function setOAuth2Server(OAuth2Server $server)
{
$this->oAuth2Server = $server;

return $this;
}

/**
* @param string $entityClass
* @param array $parameters
*
* @return mixed This will return an ORM or ODM Query\Builder
*/
abstract public function createQuery(ResourceEvent $event, $entityClass, $parameters);

/**
* @param $queryBuilder
*
* @return AdapterInterface
*/
public function getPaginatedQuery($queryBuilder)
{
$adapter = new DoctrineOrmAdapter($queryBuilder->getQuery(), false);

return $adapter;
}

/**
* @param $entityClass
*
* @return int
*/
public function getCollectionTotal($entityClass)
{
$queryBuilder = $this->getObjectManager()->createQueryBuilder();
$cmf = $this->getObjectManager()->getMetadataFactory();
$entityMetaData = $cmf->getMetadataFor($entityClass);

$identifier = $entityMetaData->getIdentifier();
$queryBuilder->select('count(row.' . $identifier[0] . ')')
->from($entityClass, 'row');

return (int) $queryBuilder->getQuery()->getSingleScalarResult();
}

/**
* Validate an OAuth2 request
*
* @param scope
* @return ApiProblem | bool
*/
public function validateOAuth2($scope = null)
{
if (! $this->getOAuth2Server()->verifyResourceRequest(
OAuth2Request::createFromGlobals(),
$response = null,
$scope = null
)) {
$error = $this->getOAuth2Server()->getResponse();
$parameters = $error->getParameters();
$detail = isset($parameters['error_description'])
? $parameters['error_description']: $error->getStatusText();

return new ApiProblem($error->getStatusCode(), $detail);
}

return true;
}
}
33 changes: 3 additions & 30 deletions src/Server/Query/Provider/DefaultOdm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,14 @@

namespace ZF\Apigility\Doctrine\Server\Query\Provider;

use ZF\Apigility\Doctrine\Server\Query\Provider\QueryProviderInterface;
use ZF\Apigility\Doctrine\Server\Paginator\Adapter\DoctrineOdmAdapter;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Zend\ServiceManager\AbstractPluginManager;
use ZF\ApiProblem\ApiProblem;
use ZF\Rest\ResourceEvent;
use OAuth2\Request as OAuth2Request;
use OAuth2\Server as OAuth2Server;

class DefaultOdm implements QueryProviderInterface
class DefaultOdm extends AbstractQueryProvider
{
/**
* @var ObjectManager
*/
protected $objectManager;

/**
* Set the object manager
*
* @param ObjectManager $objectManager
*/
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* Get the object manager
*
* @return ObjectManager
*/
public function getObjectManager()
{
return $this->objectManager;
}

/**
* {@inheritDoc}
*/
Expand Down
Loading

0 comments on commit f4380c7

Please sign in to comment.