Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Tools\Stream\FollowerRecordService;
/**
* @noinspection PhpUnused
*/
class DeleteFollowers implements Action
{
public function __construct(
private FollowerRecordService $service,
private Acl $acl
) {}
public function process(Request $request): Response
{
$entityType = $request->getRouteParam('entityType');
$id = $request->getRouteParam('id');
$data = $request->getParsedBody();
if (!$entityType || !$id) {
throw new BadRequest();
}
if (!$this->acl->check($entityType)) {
throw new Forbidden();
}
$ids = $data->ids ?? (isset($data->id) ? [$data->id] : []);
if ($ids === [] || !is_array($ids)) {
throw new BadRequest("No ids.");
}
foreach ($ids as $userId) {
if (!is_string($userId)) {
throw new BadRequest("Bad id item.");
}
}
foreach ($ids as $userId) {
$this->service->unlink($entityType, $id, $userId);
}
return ResponseComposer::json(true);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Record\EntityProvider;
use Espo\Entities\Note;
use Espo\Tools\Stream\MyReactionsService;
/**
* @noinspection PhpUnused
*/
class DeleteMyReactions implements Action
{
public function __construct(
private EntityProvider $entityProvider,
private MyReactionsService $myReactionsService,
) {}
public function process(Request $request): Response
{
$id = $request->getRouteParam('id') ?? throw new BadRequest();
$type = $request->getRouteParam('type') ?? throw new BadRequest();
$note = $this->entityProvider->getByClass(Note::class, $id);
$this->myReactionsService->unReact($note, $type);
return ResponseComposer::json(true);
}
}

View File

@@ -0,0 +1,114 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Entities\Note;
use Espo\ORM\EntityManager;
/**
* @noinspection PhpUnused
*/
class DeleteNotePin implements Action
{
public function __construct(
private EntityManager $entityManager,
private Acl $acl
) {}
/**
* @inheritDoc
*/
public function process(Request $request): Response
{
$id = $request->getRouteParam('id');
if (!$id) {
throw new BadRequest();
}
$note = $this->getNote($id);
$this->checkParent($note);
if ($note->isPinned()) {
$note->setIsPinned(false);
$this->entityManager->saveEntity($note);
}
return ResponseComposer::json(true);
}
/**
* @throws Forbidden
* @throws NotFound
*/
private function getNote(string $id): Note
{
$note = $this->entityManager->getRDBRepositoryByClass(Note::class)->getById($id);
if (!$note) {
throw new NotFound();
}
if (!$this->acl->checkEntityRead($note)) {
throw new Forbidden("No read access.");
}
return $note;
}
/**
* @throws Forbidden
*/
private function checkParent(Note $note): void
{
if (!$note->getParentType() || !$note->getParentId()) {
throw new Forbidden("No parent.");
}
$parent = $this->entityManager->getEntityById($note->getParentType(), $note->getParentId());
if (!$parent) {
throw new Forbidden("Parent not found.");
}
if (!$this->acl->checkEntityEdit($parent)) {
throw new Forbidden("No parent edit access.");
}
}
}

View File

@@ -0,0 +1,72 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Record\SearchParamsFetcher;
use Espo\Tools\Stream\FollowerRecordService;
/**
* @noinspection PhpUnused
*/
class GetFollowers implements Action
{
public function __construct(
private FollowerRecordService $service,
private SearchParamsFetcher $searchParamsFetcher,
private Acl $acl
) {}
public function process(Request $request): Response
{
$entityType = $request->getRouteParam('entityType');
$id = $request->getRouteParam('id');
if (!$entityType || !$id) {
throw new BadRequest();
}
if (!$this->acl->check($entityType)) {
throw new Forbidden();
}
$searchParams = $this->searchParamsFetcher->fetch($request);
$collection = $this->service->find($entityType, $id, $searchParams);
return ResponseComposer::json($collection->toApiOutput());
}
}

View File

@@ -0,0 +1,107 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Name\Field;
use Espo\Core\Record\SearchParamsFetcher;
use Espo\Core\Select\SearchParams;
use Espo\Core\Select\Where\Item as WhereItem;
use Espo\Tools\Stream\GlobalRecordService;
/**
* @noinspection PhpUnused
*/
class GetGlobal implements Action
{
public function __construct(
private Acl $acl,
private SearchParamsFetcher $searchParamsFetcher,
private GlobalRecordService $service
) {}
public function process(Request $request): Response
{
if (!$this->acl->checkScope(GlobalRecordService::SCOPE_NAME)) {
throw new Forbidden();
}
$searchParams = $this->fetchSearchParams($request);
$result = $this->service->find($searchParams);
return ResponseComposer::json($result->toApiOutput());
}
/**
* @throws BadRequest
* @throws Forbidden
*/
private function fetchSearchParams(Request $request): SearchParams
{
$searchParams = $this->searchParamsFetcher->fetch($request);
$after = $request->getQueryParam('after');
if ($after) {
$searchParams = $searchParams
->withWhereAdded(
WhereItem
::createBuilder()
->setAttribute(Field::CREATED_AT)
->setType(WhereItem\Type::AFTER)
->setValue($after)
->build()
);
}
$beforeNumber = $request->getQueryParam('beforeNumber');
if ($beforeNumber) {
$searchParams = $searchParams
->withWhereAdded(
WhereItem
::createBuilder()
->setAttribute('number')
->setType(WhereItem\Type::LESS_THAN)
->setValue($beforeNumber)
->build()
);
}
return $searchParams;
}
}

View File

@@ -0,0 +1,106 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Record\EntityProvider;
use Espo\Core\Record\SearchParamsFetcher;
use Espo\Core\Select\SelectBuilderFactory;
use Espo\Entities\Note;
use Espo\Entities\User;
use Espo\Entities\UserReaction;
use Espo\ORM\EntityManager;
use Espo\ORM\Query\Part\Condition;
use Espo\ORM\Query\Part\Expression;
use Espo\ORM\Query\SelectBuilder;
/**
* @noinspection PhpUnused
*/
class GetNoteReactors implements Action
{
public function __construct(
private EntityProvider $entityProvider,
private SearchParamsFetcher $searchParamsFetcher,
private SelectBuilderFactory $selectBuilderFactory,
private EntityManager $entityManager,
) {}
public function process(Request $request): Response
{
$id = $request->getRouteParam('id') ?? throw new BadRequest();
$type = $request->getRouteParam('type') ?? throw new BadRequest();
$note = $this->entityProvider->getByClass(Note::class, $id);
$searchParams = $this->searchParamsFetcher->fetch($request);
$query = $this->selectBuilderFactory
->create()
->from(User::ENTITY_TYPE)
->withSearchParams($searchParams)
->withStrictAccessControl()
->withDefaultOrder()
->buildQueryBuilder()
->select([
'id',
'name',
'userName',
])
->where(
Condition::in(
Expression::column('id'),
SelectBuilder::create()
->from(UserReaction::ENTITY_TYPE)
->select('userId')
->where([
'type' => $type,
'parentId' => $note->getId(),
'parentType' => $note->getEntityType(),
])
->build()
)
)
->build();
$repository = $this->entityManager->getRDBRepositoryByClass(User::class);
$users = $repository->clone($query)->find();
$count = $repository->clone($query)->count();
return ResponseComposer::json([
'list' => $users->getValueMapList(),
'total' => $count,
]);
}
}

View File

@@ -0,0 +1,75 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Record\SearchParamsFetcher;
use Espo\Core\Select\SearchParams;
use Espo\Tools\Stream\UserRecordService;
/**
* @noinspection PhpUnused
*/
class GetOwn implements Action
{
public function __construct(
private SearchParamsFetcher $searchParamsFetcher,
private UserRecordService $service
) {}
public function process(Request $request): Response
{
$userId = $request->getRouteParam('id');
if (!$userId) {
throw new BadRequest();
}
$searchParams = $this->fetchSearchParams($request);
$collection = $this->service->findOwn($userId, $searchParams);
return ResponseComposer::json($collection->toApiOutput());
}
/**
* @throws BadRequest
* @throws Forbidden
*/
private function fetchSearchParams(Request $request): SearchParams
{
return $this->searchParamsFetcher->fetch($request);
}
}

View File

@@ -0,0 +1,85 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Record\EntityProvider;
use Espo\Core\Record\SearchParamsFetcher;
use Espo\ORM\Entity;
use Espo\Tools\Stream\RecordService;
/**
* @noinspection PhpUnused
*/
class GetStreamAttachments implements Action
{
public function __construct(
private EntityProvider $entityProvider,
private Acl $acl,
private SearchParamsFetcher $searchParamsFetcher,
private RecordService $service,
) {}
public function process(Request $request): Response
{
$entity = $this->getEntity($request);
$searchParams = $this->searchParamsFetcher->fetch($request);
$result = $this->service->findAttachments($entity, $searchParams);
return ResponseComposer::json($result->toApiOutput());
}
/**
* @throws Forbidden
* @throws NotFound
* @throws BadRequest
*/
private function getEntity(Request $request): Entity
{
$entityType = $request->getRouteParam('entityType') ?? throw new BadRequest();
$id = $request->getRouteParam('id') ?? throw new BadRequest();
$entity = $this->entityProvider->get($entityType, $id);
if (!$this->acl->checkEntityStream($entity)) {
throw new Forbidden("No 'stream' access.");
}
return $entity;
}
}

View File

@@ -0,0 +1,84 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Tools\Stream\FollowerRecordService;
/**
* @noinspection PhpUnused
*/
class PostFollowers implements Action
{
public function __construct(
private FollowerRecordService $service,
private Acl $acl
) {}
public function process(Request $request): Response
{
$entityType = $request->getRouteParam('entityType');
$id = $request->getRouteParam('id');
$data = $request->getParsedBody();
if (!$entityType || !$id) {
throw new BadRequest("No entityType or id.");
}
if (!$this->acl->check($entityType)) {
throw new Forbidden("No access to $entityType.");
}
$ids = $data->ids ?? (isset($data->id) ? [$data->id] : []);
if ($ids === [] || !is_array($ids)) {
throw new BadRequest("No ids.");
}
foreach ($ids as $userId) {
if (!is_string($userId)) {
throw new BadRequest("Bad id item.");
}
}
foreach ($ids as $userId) {
$this->service->link($entityType, $id, $userId);
}
return ResponseComposer::json(true);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Record\EntityProvider;
use Espo\Entities\Note;
use Espo\Tools\Stream\MyReactionsService;
/**
* @noinspection PhpUnused
*/
class PostMyReactions implements Action
{
public function __construct(
private EntityProvider $entityProvider,
private MyReactionsService $myReactionsService,
) {}
public function process(Request $request): Response
{
$id = $request->getRouteParam('id') ?? throw new BadRequest();
$type = $request->getRouteParam('type') ?? throw new BadRequest();
$note = $this->entityProvider->getByClass(Note::class, $id);
$this->myReactionsService->react($note, $type);
return ResponseComposer::json(true);
}
}

View File

@@ -0,0 +1,164 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Stream\Api;
use Espo\Core\Acl;
use Espo\Core\Api\Action;
use Espo\Core\Api\Request;
use Espo\Core\Api\Response;
use Espo\Core\Api\ResponseComposer;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Error\Body;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\Utils\Config;
use Espo\Entities\Note;
use Espo\ORM\EntityManager;
/**
* @noinspection PhpUnused
*/
class PostNotePin implements Action
{
private const PINNED_MAX_COUNT = 5;
public function __construct(
private Config $config,
private EntityManager $entityManager,
private Acl $acl
) {}
/**
* @inheritDoc
*/
public function process(Request $request): Response
{
$id = $request->getRouteParam('id');
if (!$id) {
throw new BadRequest();
}
$note = $this->getNote($id);
$this->checkCanBePinned($note);
$this->checkParent($note);
$this->checkPinnedCount($note);
return ResponseComposer::json(true);
}
/**
* @throws Forbidden
* @throws NotFound
*/
private function getNote(string $id): Note
{
$note = $this->entityManager->getRDBRepositoryByClass(Note::class)->getById($id);
if (!$note) {
throw new NotFound();
}
if (!$this->acl->checkEntityRead($note)) {
throw new Forbidden("No read access.");
}
$note->setIsPinned(true);
$this->entityManager->saveEntity($note);
return $note;
}
/**
* @throws Forbidden
*/
private function checkPinnedCount(Note $entity): void
{
$maxCount = $this->config->get('notePinnedMaxCount') ?? self::PINNED_MAX_COUNT;
$count = $this->entityManager
->getRDBRepositoryByClass(Note::class)
->where([
'parentId' => $entity->getParentId(),
'parentType' => $entity->getParentType(),
'isPinned' => true,
])
->count();
if ($count < $maxCount) {
return;
}
throw Forbidden::createWithBody(
'Pinned notes max count exceeded.',
Body::create()->withMessageTranslation('pinnedMaxCountExceeded', 'Note', ['count' => (string) $count])
);
}
/**
* @throws Forbidden
*/
private function checkParent(Note $note): void
{
if (!$note->getParentType() || !$note->getParentId()) {
throw new Forbidden("No parent.");
}
$parent = $this->entityManager->getEntityById($note->getParentType(), $note->getParentId());
if (!$parent) {
throw new Forbidden("Parent not found.");
}
if (!$this->acl->checkEntityEdit($parent)) {
throw new Forbidden("No parent edit access.");
}
}
/**
* @throws Forbidden
*/
private function checkCanBePinned(Note $note): void
{
if (!$this->isEditableType($note)) {
throw new Forbidden("Cannot pin note.");
}
}
private function isEditableType(Note $entity): bool
{
return in_array($entity->getType(), [
Note::TYPE_POST,
Note::TYPE_EMAIL_RECEIVED,
Note::TYPE_EMAIL_SENT,
]);
}
}