Skip to content

Commit 6a9185a

Browse files
authored
Merge pull request #31855 from nextcloud/backport/31594/stable22
[stable22] Log in audit log federated shares events
2 parents 379d0ca + 7033663 commit 6a9185a

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

apps/federatedfilesharing/lib/Controller/RequestHandlerController.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCP\AppFramework\OCS\OCSForbiddenException;
3737
use OCP\AppFramework\OCSController;
3838
use OCP\Constants;
39+
use OCP\EventDispatcher\IEventDispatcher;
3940
use OCP\Federation\Exceptions\ProviderCouldNotAddShareException;
4041
use OCP\Federation\Exceptions\ProviderDoesNotExistsException;
4142
use OCP\Federation\ICloudFederationFactory;
@@ -44,6 +45,7 @@
4445
use OCP\IDBConnection;
4546
use OCP\IRequest;
4647
use OCP\IUserManager;
48+
use OCP\Log\Audit\CriticalActionPerformedEvent;
4749
use OCP\Share;
4850
use OCP\Share\Exceptions\ShareNotFound;
4951
use Psr\Log\LoggerInterface;
@@ -83,6 +85,9 @@ class RequestHandlerController extends OCSController {
8385
/** @var ICloudFederationProviderManager */
8486
private $cloudFederationProviderManager;
8587

88+
/** @var IEventDispatcher */
89+
private $eventDispatcher;
90+
8691
public function __construct(string $appName,
8792
IRequest $request,
8893
FederatedShareProvider $federatedShareProvider,
@@ -94,7 +99,8 @@ public function __construct(string $appName,
9499
ICloudIdManager $cloudIdManager,
95100
LoggerInterface $logger,
96101
ICloudFederationFactory $cloudFederationFactory,
97-
ICloudFederationProviderManager $cloudFederationProviderManager
102+
ICloudFederationProviderManager $cloudFederationProviderManager,
103+
IEventDispatcher $eventDispatcher
98104
) {
99105
parent::__construct($appName, $request);
100106

@@ -108,6 +114,7 @@ public function __construct(string $appName,
108114
$this->logger = $logger;
109115
$this->cloudFederationFactory = $cloudFederationFactory;
110116
$this->cloudFederationProviderManager = $cloudFederationProviderManager;
117+
$this->eventDispatcher = $eventDispatcher;
111118
}
112119

113120
/**
@@ -156,6 +163,11 @@ public function createShare() {
156163
try {
157164
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
158165
$provider->shareReceived($share);
166+
if ($sharedByFederatedId === $ownerFederatedId) {
167+
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('A new federated share with "%s" was created by "%s" and shared with "%s"', [$name, $ownerFederatedId, $shareWith]));
168+
} else {
169+
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('A new federated share with "%s" was shared by "%s" (resource owner is: "%s") and shared with "%s"', [$name, $sharedByFederatedId, $ownerFederatedId, $shareWith]));
170+
}
159171
} catch (ProviderDoesNotExistsException $e) {
160172
throw new OCSException('Server does not support federated cloud sharing', 503);
161173
} catch (ProviderCouldNotAddShareException $e) {
@@ -243,6 +255,7 @@ public function acceptShare($id) {
243255
try {
244256
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
245257
$provider->notificationReceived('SHARE_ACCEPTED', $id, $notification);
258+
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('Federated share with id "%s" was accepted', [$id]));
246259
} catch (ProviderDoesNotExistsException $e) {
247260
throw new OCSException('Server does not support federated cloud sharing', 503);
248261
} catch (ShareNotFound $e) {
@@ -275,6 +288,7 @@ public function declineShare($id) {
275288
try {
276289
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
277290
$provider->notificationReceived('SHARE_DECLINED', $id, $notification);
291+
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('Federated share with id "%s" was declined', [$id]));
278292
} catch (ProviderDoesNotExistsException $e) {
279293
throw new OCSException('Server does not support federated cloud sharing', 503);
280294
} catch (ShareNotFound $e) {
@@ -307,6 +321,7 @@ public function unshare($id) {
307321
$provider = $this->cloudFederationProviderManager->getCloudFederationProvider('file');
308322
$notification = ['sharedSecret' => $token];
309323
$provider->notificationReceived('SHARE_UNSHARED', $id, $notification);
324+
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('Federated share with id "%s" was unshared', [$id]));
310325
} catch (\Exception $e) {
311326
$this->logger->debug('processing unshare notification failed: ' . $e->getMessage(), ['exception' => $e]);
312327
}
@@ -381,6 +396,7 @@ public function updatePermissions($id) {
381396
$ocmPermissions = $this->ncPermissions2ocmPermissions((int)$ncPermissions);
382397
$notification = ['sharedSecret' => $token, 'permission' => $ocmPermissions];
383398
$provider->notificationReceived('RESHARE_CHANGE_PERMISSION', $id, $notification);
399+
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent('Federated share with id "%s" has updated permissions "%s"', [$id, implode(', ', $ocmPermissions)]));
384400
} catch (\Exception $e) {
385401
$this->logger->debug($e->getMessage(), ['exception' => $e]);
386402
throw new OCSBadRequestException();

apps/federatedfilesharing/tests/Controller/RequestHandlerControllerTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use OCP\Federation\ICloudFederationProviderManager;
3535
use OCP\Federation\ICloudFederationShare;
3636
use OCP\Federation\ICloudIdManager;
37+
use OCP\EventDispatcher\IEventDispatcher;
3738
use OCP\IDBConnection;
3839
use OCP\IRequest;
3940
use OCP\IUserManager;
@@ -100,6 +101,9 @@ class RequestHandlerControllerTest extends \Test\TestCase {
100101
/** @var ICloudFederationShare|\PHPUnit\Framework\MockObject\MockObject */
101102
private $cloudFederationShare;
102103

104+
/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
105+
private $eventDispatcher;
106+
103107
protected function setUp(): void {
104108
$this->share = $this->getMockBuilder(IShare::class)->getMock();
105109
$this->federatedShareProvider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
@@ -124,7 +128,8 @@ protected function setUp(): void {
124128
$this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
125129
$this->cloudFederationProvider = $this->createMock(ICloudFederationProvider::class);
126130
$this->cloudFederationShare = $this->createMock(ICloudFederationShare::class);
127-
131+
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
132+
$this->eventDispatcher->expects($this->any())->method('dispatchTyped');
128133

129134
$this->logger = $this->createMock(LoggerInterface::class);
130135

@@ -140,7 +145,8 @@ protected function setUp(): void {
140145
$this->cloudIdManager,
141146
$this->logger,
142147
$this->cloudFederationFactory,
143-
$this->cloudFederationProviderManager
148+
$this->cloudFederationProviderManager,
149+
$this->eventDispatcher
144150
);
145151
}
146152

0 commit comments

Comments
 (0)