diff --git a/application/Espo/Core/Utils/Database/Schema/Converter.php b/application/Espo/Core/Utils/Database/Schema/Converter.php index a2510953a3..a9e6b6294f 100644 --- a/application/Espo/Core/Utils/Database/Schema/Converter.php +++ b/application/Espo/Core/Utils/Database/Schema/Converter.php @@ -282,6 +282,9 @@ class Converter protected function prepareManyMany($entityName, $relationParams, $tables) { $tableName = Util::toUnderScore($relationParams['relationName']); + $GLOBALS['log']->debug('DBAL: prepareManyMany invoked for ' . $entityName, [ + 'tableName' => $tableName, 'parameters' => $relationParams + ]); if ($this->getSchema()->hasTable($tableName)) { $GLOBALS['log']->debug('DBAL: Table ['.$tableName.'] exists.'); @@ -297,17 +300,22 @@ class Converter //add midKeys to a schema $uniqueIndex = array(); - foreach($relationParams['midKeys'] as $index => $midKey) { - - $columnName = Util::toUnderScore($midKey); - $table->addColumn($columnName, $this->idParams['dbType'], $this->getDbFieldParams(array( - 'type' => 'foreignId', - 'len' => $this->idParams['len'], - ))); - $table->addIndex(array($columnName), SchemaUtils::generateIndexName($columnName)); - - $uniqueIndex[] = $columnName; - } + if (empty($relationParams['midKeys'])) { + $GLOBALS['log']->debug('REBUILD: midKeys are empty!', [ + 'scope' => $entityName, 'tableName' => $tableName, + 'parameters' => $relationParams + ]); + } else { + foreach($relationParams['midKeys'] as $index => $midKey) { + $columnName = Util::toUnderScore($midKey); + $table->addColumn($columnName, $this->idParams['dbType'], $this->getDbFieldParams(array( + 'type' => 'foreignId', + 'len' => $this->idParams['len'], + ))); + $table->addIndex(array($columnName), SchemaUtils::generateIndexName($columnName)); + $uniqueIndex[] = $columnName; + } + } //END: add midKeys to a schema //add additionalColumns diff --git a/application/Espo/Modules/Crm/Repositories/CaseObj.php b/application/Espo/Modules/Crm/Repositories/CaseObj.php index 0ea402367a..b4a4db56a7 100644 --- a/application/Espo/Modules/Crm/Repositories/CaseObj.php +++ b/application/Espo/Modules/Crm/Repositories/CaseObj.php @@ -75,7 +75,7 @@ class CaseObj extends \Espo\Core\ORM\Repositories\RDB ])->findOne(); if ($portalUser) { - $this->getInjection('serviceFactory')->create('Stream')->followEntity($entity, $portalUser->id); + $this->getInjection('serviceFactory')->create('Stream')->followEntity($entity, $portalUser->id, true); } } diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index 9dc347bce5..e363374e58 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -186,7 +186,7 @@ class Stream extends \Espo\Core\Services\Base $sql = " SELECT id FROM subscription WHERE - entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityName()) . " AND + entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityType()) . " AND user_id = " . $pdo->quote($userId) . " "; @@ -267,24 +267,26 @@ class Stream extends \Espo\Core\Services\Base $pdo->query($sql); } - public function followEntity(Entity $entity, $userId) + public function followEntity(Entity $entity, $userId, bool $skipAclCheck = false) { if ($userId == 'system') { return; } - if (!$this->getMetadata()->get('scopes.' . $entity->getEntityName() . '.stream')) { + if (!$this->getMetadata()->get('scopes.' . $entity->getEntityType() . '.stream')) { return false; } - $user = $this->getEntityManager()->getRepository('User') - ->select(['id', 'type', 'isActive']) - ->where([ - 'id' => $userId, - 'isActive' => true, - ])->findOne(); + if (!$skipAclCheck) { + $user = $this->getEntityManager()->getRepository('User') + ->select(['id', 'type', 'isActive']) + ->where([ + 'id' => $userId, + 'isActive' => true, + ])->findOne(); - if (!$user) return false; - if (!$this->getAclManager()->check($user, $entity, 'stream')) return false; + if (!$user) return false; + if (!$this->getAclManager()->check($user, $entity, 'stream')) return false; + } $pdo = $this->getEntityManager()->getPDO(); @@ -293,7 +295,7 @@ class Stream extends \Espo\Core\Services\Base INSERT INTO subscription (entity_id, entity_type, user_id) VALUES - (".$pdo->quote($entity->id) . ", " . $pdo->quote($entity->getEntityName()) . ", " . $pdo->quote($userId).") + (".$pdo->quote($entity->id) . ", " . $pdo->quote($entity->getEntityType()) . ", " . $pdo->quote($userId).") "; $sth = $pdo->prepare($sql)->execute(); } @@ -302,7 +304,7 @@ class Stream extends \Espo\Core\Services\Base public function unfollowEntity(Entity $entity, $userId) { - if (!$this->getMetadata()->get('scopes.' . $entity->getEntityName() . '.stream')) { + if (!$this->getMetadata()->get('scopes.' . $entity->getEntityType() . '.stream')) { return false; } @@ -311,7 +313,7 @@ class Stream extends \Espo\Core\Services\Base $sql = " DELETE FROM subscription WHERE - entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityName()) . " AND + entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityType()) . " AND user_id = " . $pdo->quote($userId) . " "; $sth = $pdo->prepare($sql)->execute(); @@ -1155,7 +1157,7 @@ class Stream extends \Espo\Core\Services\Base if ($from) { $person = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddress($from); if ($person) { - $data['personEntityType'] = $person->getEntityName(); + $data['personEntityType'] = $person->getEntityType(); $data['personEntityName'] = $person->get('name'); $data['personEntityId'] = $person->id; } @@ -1212,7 +1214,7 @@ class Stream extends \Espo\Core\Services\Base } if ($person) { - $data['personEntityType'] = $person->getEntityName(); + $data['personEntityType'] = $person->getEntityType(); $data['personEntityName'] = $person->get('name'); $data['personEntityId'] = $person->id; } diff --git a/client/src/views/fields/link-parent.js b/client/src/views/fields/link-parent.js index 2113c88649..ea6613766f 100644 --- a/client/src/views/fields/link-parent.js +++ b/client/src/views/fields/link-parent.js @@ -106,6 +106,10 @@ define('views/fields/link-parent', 'views/fields/base', function (Dep) { this.foreignScope = this.model.get(this.typeName) || this.foreignScopeList[0]; + if (this.foreignScope && !~this.foreignScopeList.indexOf(this.foreignScope)) { + this.foreignScopeList.unshift(this.foreignScope); + } + this.listenTo(this.model, 'change:' + this.typeName, function () { this.foreignScope = this.model.get(this.typeName) || this.foreignScopeList[0]; }.bind(this)); diff --git a/tests/integration/Espo/User/AclTest.php b/tests/integration/Espo/User/AclTest.php index c7d9cc6d8e..b75442c382 100644 --- a/tests/integration/Espo/User/AclTest.php +++ b/tests/integration/Espo/User/AclTest.php @@ -185,7 +185,7 @@ class AclTest extends \tests\integration\Core\BaseTestCase $this->assertTrue(!property_exists($resultData, 'type') || $resultData->type !== 'admin'); $this->assertTrue( - !property_exists($resultData, 'teamsIds') || !is_array($resultData->teamsIds) || !in_array('id', $$resultData->teamsIds) + !property_exists($resultData, 'teamsIds') || !is_array($resultData->teamsIds) || !in_array('id', $resultData->teamsIds) ); }