-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(encryption): Migrate from hooks to events
Signed-off-by: Côme Chilliet <[email protected]>
- Loading branch information
Showing
9 changed files
with
132 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
namespace OC\Encryption; | ||
|
||
use OC\Files\Filesystem; | ||
use OC\Files\SetupManager; | ||
use OC\Files\View; | ||
use OCA\Files_Trashbin\Events\NodeRestoredEvent; | ||
use OCP\Encryption\IFile; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventDispatcher; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\Files\Events\Node\NodeRenamedEvent; | ||
use OCP\Files\Folder; | ||
use OCP\IUser; | ||
use OCP\IUserSession; | ||
use OCP\Share\Events\ShareCreatedEvent; | ||
use OCP\Share\Events\ShareDeletedEvent; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** @template-implements IEventListener<NodeRenamedEvent|ShareCreatedEvent|ShareDeletedEvent|NodeRestoredEvent> */ | ||
class EncryptionEventListener implements IEventListener { | ||
private ?Update $updater = null; | ||
|
||
public function __construct( | ||
private IUserSession $userSession, | ||
private SetupManager $setupManager, | ||
) { | ||
} | ||
|
||
public static function register(IEventDispatcher $dispatcher): void { | ||
$dispatcher->addServiceListener(NodeRenamedEvent::class, static::class); | ||
$dispatcher->addServiceListener(ShareCreatedEvent::class, static::class); | ||
$dispatcher->addServiceListener(ShareDeletedEvent::class, static::class); | ||
$dispatcher->addServiceListener(NodeRestoredEvent::class, static::class); | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if ($event instanceof NodeRenamedEvent) { | ||
$this->getUpdate()->postRename($event->getSource()->getNode() instanceof Folder, $event->getSource()->getPath(), $event->getTarget()->getPath()); | ||
Check failure on line 47 in lib/private/Encryption/EncryptionEventListener.php GitHub Actions / static-code-analysisUndefinedInterfaceMethod
|
||
} elseif ($event instanceof ShareCreatedEvent) { | ||
$this->getUpdate()->postShared($event->getShare()->getNodeType(), $event->getShare()->getNodeId()); | ||
} elseif ($event instanceof ShareDeletedEvent) { | ||
// In case the unsharing happens in a background job, we don't have | ||
// a session and we load instead the user from the UserManager | ||
$owner = $event->getShare()->getNode->getOwner(); | ||
Check failure on line 53 in lib/private/Encryption/EncryptionEventListener.php GitHub Actions / static-code-analysisNoInterfaceProperties
|
||
$this->getUpdate($owner)->postUnshared($event->getShare()->getNodeType(), $event->getShare()->getNodeId()); | ||
} elseif ($event instanceof NodeRestoredEvent) { | ||
$this->getUpdate()->postRestore($event->getTarget()->getNode() instanceof Folder, $event->getTarget()->getPath()); | ||
Check failure on line 56 in lib/private/Encryption/EncryptionEventListener.php GitHub Actions / static-code-analysisUndefinedInterfaceMethod
|
||
} | ||
} | ||
|
||
private function getUpdate(?IUser $owner = null): Update { | ||
if (is_null($this->updater)) { | ||
$user = $this->userSession->getUser(); | ||
if (!$user && ($owner !== null)) { | ||
$user = $owner; | ||
} | ||
if (!$user) { | ||
throw new \Exception('Inconsistent data, File unshared, but owner not found. Should not happen'); | ||
} | ||
|
||
$uid = $user->getUID(); | ||
|
||
if (!$this->setupManager->isSetupComplete($user)) { | ||
$this->setupManager->setupForUser($user); | ||
} | ||
|
||
$this->updater = new Update( | ||
new Util( | ||
new View(), | ||
\OC::$server->getUserManager(), | ||
\OC::$server->getGroupManager(), | ||
\OC::$server->getConfig()), | ||
Filesystem::getMountManager(), | ||
\OC::$server->getEncryptionManager(), | ||
\OC::$server->get(IFile::class), | ||
\OC::$server->get(LoggerInterface::class), | ||
$uid | ||
); | ||
} | ||
|
||
return $this->updater; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.