From dc0a616ec8a011d85f0e65058eaec36808e29016 Mon Sep 17 00:00:00 2001 From: yuri Date: Fri, 11 Dec 2015 14:43:02 +0200 Subject: [PATCH] select manager changes --- application/Espo/Core/SelectManagers/Base.php | 93 ++++++++++++++++--- application/Espo/ORM/DB/Query/Base.php | 4 +- application/Espo/SelectManagers/Email.php | 37 +++++--- 3 files changed, 108 insertions(+), 26 deletions(-) diff --git a/application/Espo/Core/SelectManagers/Base.php b/application/Espo/Core/SelectManagers/Base.php index 1b57b94347..aeced8d61d 100644 --- a/application/Espo/Core/SelectManagers/Base.php +++ b/application/Espo/Core/SelectManagers/Base.php @@ -337,34 +337,88 @@ class Base protected function accessOnlyOwn(&$result) { - if ($this->getSeed()->hasField('assignedUserId')) { + if ($this->hasAssignedUsersField()) { + $this->setDistinct(true, $result); + $this->addLeftJoin('assignedUsers', $result); + $result['whereClause'][] = array( + 'assignedUsers.id' => $this->getUser()->id + ); + return; + } + + if ($this->hasAssignedUserField()) { $result['whereClause'][] = array( 'assignedUserId' => $this->getUser()->id ); return; } - if ($this->getSeed()->hasField('createdById')) { + if ($this->hasCreatedByField()) { $result['whereClause'][] = array( 'createdById' => $this->getUser()->id ); - return; } } protected function accessOnlyTeam(&$result) { - if (!$this->getSeed()->hasField('teamsIds')) { + if (!$this->hasTeamsField()) { return; } + $this->setDistinct(true, $result); - $this->addLeftJoin('teams', $result); - $result['whereClause'][] = array( - 'OR' => array( - 'teams.id' => $this->user->get('teamsIds'), - 'assignedUserId' => $this->getUser()->id - ) + $this->addLeftJoin(['teams', 'teamsAccess'], $result); + + if ($this->hasAssignedUsersField()) { + $this->addLeftJoin(['assignedUsers', 'assignedUsersAccess'], $result); + $result['whereClause'][] = array( + 'OR' => array( + 'teamsAccess.id' => $this->getUser()->getLinkMultipleIdList('teams'), + 'assignedUsersAccess.id' => $this->getUser()->id + ) + ); + return; + } + + $d = array( + 'teamsAccess.id' => $this->getUser()->getLinkMultipleIdList('teams') ); + if ($this->hasAssignedUserField()) { + $d['assignedUserId'] = $this->getUser()->id; + } else if ($this->hasCreatedByField()) { + $d['createdById'] = $this->getUser()->id; + } + $result['whereClause'][] = array( + 'OR' => $d + ); + } + + protected function hasAssignedUsersField() + { + if ($this->getSeed()->hasRelation('assignedUsers') && $this->getSeed()->hasField('assignedUsersIds')) { + return true; + } + } + + protected function hasAssignedUserField() + { + if ($this->getSeed()->hasField('assignedUserId')) { + return true; + } + } + + protected function hasCreatedByField() + { + if ($this->getSeed()->hasField('createdById')) { + return true; + } + } + + protected function hasTeamsField() + { + if ($this->getSeed()->hasRelation('teams') && $this->getSeed()->hasField('teamsIds')) { + return true; + } } public function getAclParams() @@ -798,13 +852,23 @@ class Base $this->textFilter($textFilter, $result); } + public function hasJoin($join, &$result) + { + return in_array($join, $result['joins']); + } + + public function hasLeftJoin($leftJoin, &$result) + { + return in_array($leftJoin, $result['leftJoin']); + } + public function addJoin($join, &$result) { if (empty($result['joins'])) { $result['joins'] = []; } - if (!in_array($join, $result['joins'])) { + if (is_array($join) || !in_array($join, $result['joins'])) { $result['joins'][] = $join; } } @@ -815,11 +879,16 @@ class Base $result['leftJoins'] = []; } - if (!in_array($leftJoin, $result['leftJoins'])) { + if (!is_array($leftJoin) || !in_array($leftJoin, $result['leftJoins'])) { $result['leftJoins'][] = $leftJoin; } } + public function setJoinCondition($join, $condition, &$result) + { + $result['joinConditions'][$join] = $condition; + } + public function setDistinct($distinct, &$result) { $result['distinct'] = (bool) $distinct; diff --git a/application/Espo/ORM/DB/Query/Base.php b/application/Espo/ORM/DB/Query/Base.php index c909b4a070..5f17b4a0d2 100644 --- a/application/Espo/ORM/DB/Query/Base.php +++ b/application/Espo/ORM/DB/Query/Base.php @@ -150,7 +150,7 @@ abstract class Base } if (!empty($params['joins']) && is_array($params['joins'])) { - $params['joins'] = array_unique($params['joins']); + // TODO array unique $joinsRelated = $this->getJoins($entity, $params['joins'], false, $params['joinConditions']); if (!empty($joinsRelated)) { if (!empty($joinsPart)) { @@ -161,7 +161,7 @@ abstract class Base } if (!empty($params['leftJoins']) && is_array($params['leftJoins'])) { - $params['leftJoins'] = array_unique($params['leftJoins']); + // TODO array unique $joinsRelated = $this->getJoins($entity, $params['leftJoins'], true, $params['joinConditions']); if (!empty($joinsRelated)) { if (!empty($joinsPart)) { diff --git a/application/Espo/SelectManagers/Email.php b/application/Espo/SelectManagers/Email.php index 305595e7a3..6dc6e1730d 100644 --- a/application/Espo/SelectManagers/Email.php +++ b/application/Espo/SelectManagers/Email.php @@ -35,31 +35,30 @@ class Email extends \Espo\Core\SelectManagers\Base { $result = parent::getSelectParams($params, $withAcl); - if (!in_array('users', $result['joins']) && !in_array('users', $result['leftJoins'])) { - $result['leftJoins'][] = 'users'; - $result['joinConditions']['users'] = array( + if (!$this->hasJoin('users', $result) && !$this->hasLeftJoin('users', $result)) { + $this->addLeftJoin('users', $result); + $this->setJoinCondition('users', array( 'userId' => $this->getUser()->id - ); + )); } - if (!isset($result['select'])) { - $result['additionalSelectColumns']['usersMiddle.is_read'] = 'isRead'; - $result['additionalSelectColumns']['usersMiddle.is_important'] = 'isImportant'; - $result['additionalSelectColumns']['usersMiddle.in_trash'] = 'inTrash'; - } + $this->addUsersColumns($result); return $result; } protected function boolFilterOnlyMy(&$result) { - if (!in_array('users', $result['joins'])) { - $result['joins'][] = 'users'; - } + $this->addJoin('users', $result); $result['whereClause'][] = array( 'usersMiddle.userId' => $this->getUser()->id ); + $this->addUsersColumns($result); + } + + protected function addUsersColumns(&$result) + { if (!isset($result['select'])) { $result['additionalSelectColumns']['usersMiddle.is_read'] = 'isRead'; $result['additionalSelectColumns']['usersMiddle.is_important'] = 'isImportant'; @@ -122,6 +121,20 @@ class Email extends \Espo\Core\SelectManagers\Base $this->boolFilterOnlyMy($result); } + protected function accessOnlyTeam(&$result) + { + $this->setDistinct(true, $result); + $this->addLeftJoin(['teams', 'teamsAccess'], $result); + $this->addLeftJoin(['users', 'usersAccess'], $result); + + $result['whereClause'][] = array( + 'OR' => array( + 'teamsAccess.id' => $this->getUser()->getLinkMultipleIdList('teams'), + 'usersAccess.id' => $this->getUser()->id + ) + ); + } + protected function textFilter($textFilter, &$result) { $d = array();