From 7f4e5dd01af7f71c95ef190164dfe0a01ab30bf4 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 10 Sep 2018 11:59:57 +0300 Subject: [PATCH] note acl populate script --- application/Espo/Services/App.php | 72 +++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/application/Espo/Services/App.php b/application/Espo/Services/App.php index 4467296e99..50f9f60492 100644 --- a/application/Espo/Services/App.php +++ b/application/Espo/Services/App.php @@ -261,6 +261,7 @@ class App extends \Espo\Core\Services\Base } } + // TODO remove in 5.5.0 public function jobPopulateArrayValues() { $scopeList = array_keys($this->getMetadata()->get(['scopes'])); @@ -310,4 +311,75 @@ class App extends \Espo\Core\Services\Base } } } + + // TODO remove in 5.5.0 + public function jobPopulateNotesTeamUser() + { + $aclManager = $this->getInjection('container')->get('aclManager'); + + $sql = $this->getEntityManager()->getQuery()->createSelectQuery('Note', [ + 'whereClause' => [ + 'parentId!=' => null, + 'type=' => ['Relate', 'CreateRelated', 'EmailReceived', 'EmailSent', 'Assign', 'Create'], + ], + 'limit' => 100000, + 'orderBy' => [['number', 'DESC']] + ]); + $sth = $this->getEntityManager()->getPdo()->prepare($sql); + $sth->execute(); + + $i = 0; + while ($dataRow = $sth->fetch(\PDO::FETCH_ASSOC)) { + $i++; + $note = $this->getEntityManager()->getEntityFactory()->create('Note'); + $note->set($dataRow); + $note->setAsFetched(); + + if ($note->get('relatedId') && $note->get('relatedType')) { + $targetType = $note->get('relatedType'); + $targetId = $note->get('relatedId'); + } else if ($note->get('parentId') && $note->get('parentType')) { + $targetType = $note->get('parentType'); + $targetId = $note->get('parentId'); + } else { + continue; + } + + if (!$this->getEntityManager()->hasRepository($targetType)) continue; + + try { + $entity = $this->getEntityManager()->getEntity($targetType, $targetId); + if (!$entity) continue; + $ownerUserIdAttribute = $aclManager->getImplementation($targetType)->getOwnerUserIdAttribute($entity); + $toSave = false; + if ($ownerUserIdAttribute) { + if ($entity->getAttributeParam($ownerUserIdAttribute, 'isLinkMultipleIdList')) { + $link = $entity->getAttributeParam($ownerUserIdAttribute, 'relation'); + $userIdList = $entity->getLinkMultipleIdList($link); + } else { + $userId = $entity->get($ownerUserIdAttribute); + if ($userId) { + $userIdList = [$userId]; + } else { + $userIdList = []; + } + } + if (!empty($userIdList)) { + $note->set('usersIds', $userIdList); + $toSave = true; + } + } + if ($entity->hasLinkMultipleField('teams')) { + $teamIdList = $entity->getLinkMultipleIdList('teams'); + if (!empty($teamIdList)) { + $note->set('teamsIds', $teamIdList); + $toSave = true; + } + } + if ($toSave) { + $this->getEntityManager()->saveEntity($note); + } + } catch (\Exception $e) {} + } + } }