Skip to content

Instantly share code, notes, and snippets.

@TomHAnderson
Last active October 12, 2017 05:11
Show Gist options
  • Save TomHAnderson/0f80cb353f2eb3df171874dd7352ad21 to your computer and use it in GitHub Desktop.
Save TomHAnderson/0f80cb353f2eb3df171874dd7352ad21 to your computer and use it in GitHub Desktop.
A Query Provider for Updating a Source
namespace DbApi\Query\Provider\Source;

use ZF\Rest\ResourceEvent;
use DbApi\Query\Provider\AbstractQueryProvider;
use Db\Fixture\RoleFixture;

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

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

        // The source admin must be an admin for this source artist
        $queryBuilder
            ->innerJoin('row.performance', 'performance')
            ->innerJoin('performance.artist', 'artist')
            ->innerJoin('artist.artistGroup', 'artistGroup')
            ->andWhere($queryBuilder->expr()->isMemberOf(':user', 'artistGroup.user'))
            ->setParameter('user', $this->getAuthentication()->getIdentity()->getUser())
            ;

        return $queryBuilder;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment