Giter Club home page Giter Club logo

api-tools-doctrine-query-provider's Introduction

Laminas API Tools Doctrine Query Provider

The Doctrine integration with Laminas API Tools includes Query Providers used to create filtering of data for each REST endpoint. This may take into account the current user's permissions and add filtering to a Doctrine QueryBuilder object.

Query Providers act much like an event listener but are hard-coded into a Laminas API Tools Doctrine REST event.

Writing your own Query Providers is not difficult but this library exists to give you a jump start to writing custom Query Providers by supplying a plugin manager and abstract query providers for both ORM and ODM. Included are Query requets based tools for filtering and ordering request responses. This filtering is provided by api-tools-doctrine-orm-querybuilder and api-tools-doctrine-odm-querybuilder

Requirements

Please see the composer.json file.

Installation

Run the following composer command:

$ composer require "laminas-api-tools/api-tools-doctrine-query-provider"

Alternately, manually add the following to your composer.json, in the require section:

"require": {
    "laminas-api-tools/api-tools-doctrine-query-provider": "^1.0"
}

And then run composer update to ensure the module is installed.

Finally, add the module name to your project's config/application.config.php under the modules key:

return [
    /* ... */
    'modules' => [
        /* ... */
        'Laminas\ApiTools\Doctrine\QueryProvider',
    ],
    /* ... */
];

Configuration

Configuration is done inside the doctrine-connected section of the api-tools-doctrine configuration.

return [
    'zf-laminas-api-tools' => [
        'doctrine-connected' => [
            V1\Rest\PerformanceMerge\PerformanceMergeResource::class => [
                'query_providers' => [
  //                  'default' => QueryProvider\PerformanceMergeDefault::class, // applies to all 
                    'fetch' => QueryProvider\PerformanceMergeFetch::class,
                    'fetch_all' => QueryProvider\PerformanceMergeFetch::class,
                    'patch' => QueryProvider\PerformanceMergePatch::class,
                    'update' => QueryProvider\PerformanceMergeUpdate::class,
                    'delete' => QueryProvider\PerformanceMergeDelete::class,
                ],
            ],

Note patch_all, update_all, and delete_all do not exist. For similar functionality to query providers for these actions use the provided events. See custom events.

What's in the box

Included is filtering and sorting of REST responses and authentication through the Zend\Authentication\AuthenticationService library. This authentication ties in directly with api-tools-mvc-auth.

Example

namespace DbApi\QueryProvider;

use ZF\Rest\ResourceEvent;
use Laminas\ApiTools\Doctrine\QueryProvider\AbstractORMQueryProvider;
use Db\Fixture\RoleFixture;

final class PerformanceCorrectionPatch extends AbstractQueryProvider
{
    public function createQuery(ResourceEvent $event, $entityClass, $parameters)
    {
        $queryBuilder = parent::createQuery($event, $entityClass, $parameters);

        // Always allow admin
        if ($this->getAuthentication()->getIdentity()->getUser()->hasRole(RoleFixture::ADMIN)) {
            return $queryBuilder;
        }

        // Filter so the query provider only returns results belonging to this user
        $queryBuilder
            ->andWhere($queryBuilder->expr()->eq('row.user', ':user'))
            ->setParameter('user', $this->getAuthentication()->getIdentity()->getUser())
            ;

        return $queryBuilder;
    }
}

api-tools-doctrine-query-provider's People

Contributors

tomhanderson avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.