after link record hooks
This commit is contained in:
+18
-41
@@ -27,55 +27,32 @@
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Services;
|
||||
namespace Espo\Classes\RecordHooks\Team;
|
||||
|
||||
use Espo\Core\Acl\Cache\Clearer as AclCacheClearer;
|
||||
use Espo\Entities\User as UserEntity;
|
||||
use Espo\Core\Di;
|
||||
use Espo\Core\Acl\Cache\Clearer;
|
||||
use Espo\Core\DataManager;
|
||||
use Espo\Core\Record\Hook\LinkHook;
|
||||
use Espo\Entities\Team;
|
||||
use Espo\Entities\User;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
/**
|
||||
* @extends Record<\Espo\Entities\Team>
|
||||
* @implements LinkHook<Team>
|
||||
*/
|
||||
class Team extends Record implements
|
||||
|
||||
Di\DataManagerAware
|
||||
class ClearCacheAfterLink implements LinkHook
|
||||
{
|
||||
use Di\DataManagerSetter;
|
||||
public function __construct(
|
||||
private Clearer $clearer,
|
||||
private DataManager $dataManager
|
||||
) {}
|
||||
|
||||
public function link(string $id, string $link, string $foreignId): void
|
||||
public function process(Entity $entity, string $link, Entity $foreignEntity): void
|
||||
{
|
||||
parent::link($id, $link, $foreignId);
|
||||
|
||||
if ($link === 'users') {
|
||||
/** @var ?UserEntity $user */
|
||||
$user = $this->entityManager->getEntityById(UserEntity::ENTITY_TYPE, $foreignId);
|
||||
|
||||
if ($user) {
|
||||
$this->createAclCacheClearer()->clearForUser($user);
|
||||
}
|
||||
|
||||
$this->dataManager->updateCacheTimestamp();
|
||||
if ($link !== 'users' || !$foreignEntity instanceof User) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function unlink(string $id, string $link, string $foreignId): void
|
||||
{
|
||||
parent::unlink($id, $link, $foreignId);
|
||||
|
||||
if ($link === 'users') {
|
||||
/** @var ?UserEntity $user */
|
||||
$user = $this->entityManager->getEntityById(UserEntity::ENTITY_TYPE, $foreignId);
|
||||
|
||||
if ($user) {
|
||||
$this->createAclCacheClearer()->clearForUser($user);
|
||||
}
|
||||
|
||||
$this->dataManager->updateCacheTimestamp();
|
||||
}
|
||||
}
|
||||
|
||||
private function createAclCacheClearer(): AclCacheClearer
|
||||
{
|
||||
return $this->injectableFactory->create(AclCacheClearer::class);
|
||||
$this->clearer->clearForUser($foreignEntity);
|
||||
$this->dataManager->updateCacheTimestamp();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2024 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* 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\Classes\RecordHooks\Team;
|
||||
|
||||
use Espo\Core\Acl\Cache\Clearer;
|
||||
use Espo\Core\DataManager;
|
||||
use Espo\Core\Record\Hook\UnlinkHook;
|
||||
use Espo\Entities\Team;
|
||||
use Espo\Entities\User;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
/**
|
||||
* @implements UnlinkHook<Team>
|
||||
*/
|
||||
class ClearCacheAfterUnlink implements UnlinkHook
|
||||
{
|
||||
public function __construct(
|
||||
private Clearer $clearer,
|
||||
private DataManager $dataManager
|
||||
) {}
|
||||
|
||||
public function process(Entity $entity, string $link, Entity $foreignEntity): void
|
||||
{
|
||||
if ($link !== 'users' || !$foreignEntity instanceof User) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->clearer->clearForUser($foreignEntity);
|
||||
$this->dataManager->updateCacheTimestamp();
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,8 @@ class Provider
|
||||
Type::AFTER_DELETE => [DeleteHook::class],
|
||||
Type::BEFORE_LINK => [LinkHook::class],
|
||||
Type::BEFORE_UNLINK => [UnlinkHook::class],
|
||||
Type::AFTER_LINK => [LinkHook::class],
|
||||
Type::AFTER_UNLINK => [UnlinkHook::class],
|
||||
];
|
||||
|
||||
private BindingContainer $bindingContainer;
|
||||
|
||||
@@ -43,4 +43,6 @@ class Type
|
||||
|
||||
public const BEFORE_LINK = 'beforeLink';
|
||||
public const BEFORE_UNLINK = 'beforeUnlink';
|
||||
public const AFTER_LINK = 'afterLink';
|
||||
public const AFTER_UNLINK = 'afterUnlink';
|
||||
}
|
||||
|
||||
@@ -165,6 +165,20 @@ class HookManager
|
||||
}
|
||||
}
|
||||
|
||||
public function processAfterLink(Entity $entity, string $link, Entity $foreignEntity): void
|
||||
{
|
||||
foreach ($this->getAfterLinkHookList($entity->getEntityType()) as $hook) {
|
||||
$hook->process($entity, $link, $foreignEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public function processAfterUnlink(Entity $entity, string $link, Entity $foreignEntity): void
|
||||
{
|
||||
foreach ($this->getAfterUnlinkHookList($entity->getEntityType()) as $hook) {
|
||||
$hook->process($entity, $link, $foreignEntity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ReadHook<Entity>[]
|
||||
*/
|
||||
@@ -245,4 +259,22 @@ class HookManager
|
||||
/** @var UnlinkHook<Entity>[] */
|
||||
return $this->provider->getList($entityType, Type::BEFORE_UNLINK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LinkHook<Entity>[]
|
||||
*/
|
||||
private function getAfterLinkHookList(string $entityType): array
|
||||
{
|
||||
/** @var LinkHook<Entity>[] */
|
||||
return $this->provider->getList($entityType, Type::AFTER_LINK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UnlinkHook<Entity>[]
|
||||
*/
|
||||
private function getAfterUnlinkHookList(string $entityType): array
|
||||
{
|
||||
/** @var UnlinkHook<Entity>[] */
|
||||
return $this->provider->getList($entityType, Type::AFTER_UNLINK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1275,6 +1275,8 @@ class Service implements Crud,
|
||||
$this->getRepository()
|
||||
->getRelation($entity, $link)
|
||||
->relate($foreignEntity, null, [SaveOption::API => true]);
|
||||
|
||||
$this->getRecordHookManager()->processAfterLink($entity, $link, $foreignEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1331,6 +1333,8 @@ class Service implements Crud,
|
||||
$this->getRepository()
|
||||
->getRelation($entity, $link)
|
||||
->unrelate($foreignEntity, [SaveOption::API => true]);
|
||||
|
||||
$this->getRecordHookManager()->processAfterUnlink($entity, $link, $foreignEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -68,6 +68,8 @@ class Builder
|
||||
['recordDefs', self::ANY_KEY, 'afterDeleteHookClassNameList'],
|
||||
['recordDefs', self::ANY_KEY, 'beforeLinkHookClassNameList'],
|
||||
['recordDefs', self::ANY_KEY, 'beforeUnlinkHookClassNameList'],
|
||||
['recordDefs', self::ANY_KEY, 'afterLinkHookClassNameList'],
|
||||
['recordDefs', self::ANY_KEY, 'afterUnlinkHookClassNameList'],
|
||||
];
|
||||
|
||||
private const ANY_KEY = '__ANY__';
|
||||
|
||||
@@ -9,5 +9,11 @@
|
||||
],
|
||||
"beforeLinkHookClassNameList": [
|
||||
"Espo\\Classes\\RecordHooks\\Team\\BeforeLinkUserCheck"
|
||||
],
|
||||
"afterLinkHookClassNameList": [
|
||||
"Espo\\Classes\\RecordHooks\\Team\\ClearCacheAfterLink"
|
||||
],
|
||||
"afterUnlinkHookClassNameList": [
|
||||
"Espo\\Classes\\RecordHooks\\Team\\ClearCacheAfterUnlink"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -242,6 +242,20 @@
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"afterLinkHookClassNameList": {
|
||||
"description": "After-link hooks. Should implement the Espo\\Core\\Record\\Hook\\LinkHook interface. As of v8.2.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"afterUnlinkHookClassNameList": {
|
||||
"description": "After-unlink hooks. Should implement the Espo\\Core\\Record\\Hook\\UnlinkHook interface. As of v8.2.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user