简体   繁体   中英

How to get documents checking against a permissions junction table with query builder in symfony doctrine?

I'm curious how to build a doctrine query with query builder in Symfony which can exclude items based on a fairly standardized permissions junction table.

In my case,

User entity

Template entity

userTemplatePermissionTable

In userTemplatePermissions Entity, we have the following properties:

userToCheck , template , read , write , delete

And I'd like something the equivalent of:

return $this->createQueryBuilder('t')
[where count of t.userDocumentPermissions > 0
  where canRead = true, canWrite = true, canDelete = true
AND userDocumentpermissions.user = :user]

This ended up being a fairly simple setup of left joins and exclusion checks, I wound up with the following in my TemplateRespository.php

public function findUserAuthorized($user, $read = null, $write = null, $delete = null)
{
    $query = $this->createQueryBuilder('t')
    ->leftJoin('t.userTemplatePermissions', 'utp')
    ->where('utp.userToCheck = :user')
    ->setParameter('user',$user);

    if ($read !== null) {
        $query->andWhere('utp.read = :read')
        ->setParameter('read',$read);
    }
    if ($write !== null) {
        $query->andWhere('utp.write = :write')
        ->setParameter('write',$write);
    }
    if ($delete !== null) {
        $query->andWhere('utp.delete = :delete')
        ->setParameter('delete',$delete);
    }
    return $query->getQuery()->getResult();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM