portal access filter ref, no distinct
This commit is contained in:
@@ -34,6 +34,10 @@ use Espo\Core\Select\Helpers\FieldHelper;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Modules\Crm\Entities\Account;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\ORM\Query\Part\Condition as Cond;
|
||||
use Espo\ORM\Query\Part\Expression as Expr;
|
||||
use Espo\ORM\Query\Part\Where\OrGroup;
|
||||
use Espo\ORM\Query\Part\WhereClause;
|
||||
use Espo\ORM\Query\SelectBuilder as QueryBuilder;
|
||||
|
||||
class PortalOnlyAccount implements Filter
|
||||
@@ -45,63 +49,92 @@ class PortalOnlyAccount implements Filter
|
||||
|
||||
public function apply(QueryBuilder $queryBuilder): void
|
||||
{
|
||||
$orGroup = [];
|
||||
$orBuilder = OrGroup::createBuilder();
|
||||
|
||||
$accountIdList = $this->user->getLinkMultipleIdList('accounts');
|
||||
$contactId = $this->user->get('contactId');
|
||||
|
||||
if (count($accountIdList)) {
|
||||
if ($this->fieldHelper->hasAccountField()) {
|
||||
$orGroup['accountId'] = $accountIdList;
|
||||
$orBuilder->add(
|
||||
Cond::in(
|
||||
Expr::column('accountId'),
|
||||
$accountIdList
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->fieldHelper->hasAccountsRelation()) {
|
||||
$queryBuilder
|
||||
->leftJoin('accounts', 'accountsAccess')
|
||||
->distinct();
|
||||
$defs = $this->fieldHelper->getRelationDefs('accounts');
|
||||
|
||||
$orGroup['accountsAccess.id'] = $accountIdList;
|
||||
$orBuilder->add(
|
||||
Cond::in(
|
||||
Expr::column('id'),
|
||||
QueryBuilder::create()
|
||||
->from(ucfirst($defs->getRelationshipName()), 'm')
|
||||
->select($defs->getMidKey())
|
||||
->where([$defs->getForeignMidKey() => $accountIdList])
|
||||
->build()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->fieldHelper->hasParentField()) {
|
||||
$orGroup[] = [
|
||||
'parentType' => Account::ENTITY_TYPE,
|
||||
'parentId' => $accountIdList,
|
||||
];
|
||||
$orBuilder->add(
|
||||
WhereClause::fromRaw([
|
||||
'parentType' => Account::ENTITY_TYPE,
|
||||
'parentId' => $accountIdList,
|
||||
])
|
||||
);
|
||||
|
||||
if ($contactId) {
|
||||
$orGroup[] = [
|
||||
'parentType' => Contact::ENTITY_TYPE,
|
||||
'parentId' => $contactId,
|
||||
];
|
||||
$orBuilder->add(
|
||||
WhereClause::fromRaw([
|
||||
'parentType' => Contact::ENTITY_TYPE,
|
||||
'parentId' => $contactId,
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($contactId) {
|
||||
if ($this->fieldHelper->hasContactField()) {
|
||||
$orGroup['contactId'] = $contactId;
|
||||
$orBuilder->add(
|
||||
WhereClause::fromRaw(['contactId' => $contactId])
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->fieldHelper->hasContactsRelation()) {
|
||||
$queryBuilder
|
||||
->leftJoin('contacts', 'contactsAccess')
|
||||
->distinct();
|
||||
$defs = $this->fieldHelper->getRelationDefs('contacts');
|
||||
|
||||
$orGroup['contactsAccess.id'] = $contactId;
|
||||
$orBuilder->add(
|
||||
Cond::in(
|
||||
Expr::column('id'),
|
||||
QueryBuilder::create()
|
||||
->from(ucfirst($defs->getRelationshipName()), 'm')
|
||||
->select($defs->getMidKey())
|
||||
->where([$defs->getForeignMidKey() => $contactId])
|
||||
->build()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->fieldHelper->hasCreatedByField()) {
|
||||
$orGroup['createdById'] = $this->user->getId();
|
||||
$orBuilder->add(
|
||||
WhereClause::fromRaw(['createdById' => $this->user->getId()])
|
||||
);
|
||||
}
|
||||
|
||||
if (empty($orGroup)) {
|
||||
$orGroup = $orBuilder->build();
|
||||
|
||||
if ($orGroup->getItemCount() === 0) {
|
||||
$queryBuilder->where(['id' => null]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$queryBuilder->where(['OR' => $orGroup]);
|
||||
$queryBuilder->where($orGroup);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ use Espo\Core\Select\AccessControl\Filter;
|
||||
use Espo\Core\Select\Helpers\FieldHelper;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\ORM\Query\Part\Condition as Cond;
|
||||
use Espo\ORM\Query\Part\Expression as Expr;
|
||||
use Espo\ORM\Query\Part\Where\OrGroup;
|
||||
use Espo\ORM\Query\Part\WhereClause;
|
||||
use Espo\ORM\Query\SelectBuilder as QueryBuilder;
|
||||
|
||||
class PortalOnlyContact implements Filter
|
||||
@@ -44,41 +48,56 @@ class PortalOnlyContact implements Filter
|
||||
|
||||
public function apply(QueryBuilder $queryBuilder): void
|
||||
{
|
||||
$orGroup = [];
|
||||
$orBuilder = OrGroup::createBuilder();
|
||||
|
||||
$contactId = $this->user->get('contactId');
|
||||
|
||||
if ($contactId) {
|
||||
if ($this->fieldHelper->hasContactField()) {
|
||||
$orGroup['contactId'] = $contactId;
|
||||
$orBuilder->add(
|
||||
WhereClause::fromRaw(['contactId' => $contactId])
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->fieldHelper->hasContactsRelation()) {
|
||||
$queryBuilder
|
||||
->leftJoin('contacts', 'contactsAccess')
|
||||
->distinct();
|
||||
$defs = $this->fieldHelper->getRelationDefs('contacts');
|
||||
|
||||
$orGroup['contactsAccess.id'] = $contactId;
|
||||
$orBuilder->add(
|
||||
Cond::in(
|
||||
Expr::column('id'),
|
||||
QueryBuilder::create()
|
||||
->from(ucfirst($defs->getRelationshipName()), 'm')
|
||||
->select($defs->getMidKey())
|
||||
->where([$defs->getForeignMidKey() => $contactId])
|
||||
->build()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->fieldHelper->hasParentField()) {
|
||||
$orGroup[] = [
|
||||
'parentType' => Contact::ENTITY_TYPE,
|
||||
'parentId' => $contactId,
|
||||
];
|
||||
$orBuilder->add(
|
||||
WhereClause::fromRaw([
|
||||
'parentType' => Contact::ENTITY_TYPE,
|
||||
'parentId' => $contactId,
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->fieldHelper->hasCreatedByField()) {
|
||||
$orGroup['createdById'] = $this->user->getId();
|
||||
$orBuilder->add(
|
||||
WhereClause::fromRaw(['createdById' => $this->user->getId()])
|
||||
);
|
||||
}
|
||||
|
||||
if (empty($orGroup)) {
|
||||
$orGroup = $orBuilder->build();
|
||||
|
||||
if ($orGroup->getItemCount() === 0) {
|
||||
$queryBuilder->where(['id' => null]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$queryBuilder->where(['OR' => $orGroup]);
|
||||
$queryBuilder->where($orGroup);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
|
||||
namespace Espo\Core\Select\Helpers;
|
||||
|
||||
use Espo\ORM\Defs\RelationDefs;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Espo\ORM\Entity;
|
||||
use Espo\ORM\BaseEntity;
|
||||
@@ -125,6 +126,14 @@ class FieldHelper
|
||||
$this->getRelationParam($this->getSeed(), 'accounts', 'entity') === 'Account';
|
||||
}
|
||||
|
||||
public function getRelationDefs(string $name): RelationDefs
|
||||
{
|
||||
return $this->entityManager
|
||||
->getDefs()
|
||||
->getEntity($this->entityType)
|
||||
->getRelation($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user