Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(encryption): Migrate from hooks to events #48332

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions apps/encryption/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
'OCA\\Encryption\\Exceptions\\MultiKeyEncryptException' => $baseDir . '/../lib/Exceptions/MultiKeyEncryptException.php',
'OCA\\Encryption\\Exceptions\\PrivateKeyMissingException' => $baseDir . '/../lib/Exceptions/PrivateKeyMissingException.php',
'OCA\\Encryption\\Exceptions\\PublicKeyMissingException' => $baseDir . '/../lib/Exceptions/PublicKeyMissingException.php',
'OCA\\Encryption\\HookManager' => $baseDir . '/../lib/HookManager.php',
'OCA\\Encryption\\Hooks\\Contracts\\IHook' => $baseDir . '/../lib/Hooks/Contracts/IHook.php',
'OCA\\Encryption\\Hooks\\UserHooks' => $baseDir . '/../lib/Hooks/UserHooks.php',
'OCA\\Encryption\\KeyManager' => $baseDir . '/../lib/KeyManager.php',
'OCA\\Encryption\\Listeners\\UserEventsListener' => $baseDir . '/../lib/Listeners/UserEventsListener.php',
'OCA\\Encryption\\Migration\\SetMasterKeyStatus' => $baseDir . '/../lib/Migration/SetMasterKeyStatus.php',
'OCA\\Encryption\\Recovery' => $baseDir . '/../lib/Recovery.php',
'OCA\\Encryption\\Services\\PassphraseService' => $baseDir . '/../lib/Services/PassphraseService.php',
'OCA\\Encryption\\Session' => $baseDir . '/../lib/Session.php',
'OCA\\Encryption\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',
'OCA\\Encryption\\Settings\\Personal' => $baseDir . '/../lib/Settings/Personal.php',
Expand Down
5 changes: 2 additions & 3 deletions apps/encryption/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ class ComposerStaticInitEncryption
'OCA\\Encryption\\Exceptions\\MultiKeyEncryptException' => __DIR__ . '/..' . '/../lib/Exceptions/MultiKeyEncryptException.php',
'OCA\\Encryption\\Exceptions\\PrivateKeyMissingException' => __DIR__ . '/..' . '/../lib/Exceptions/PrivateKeyMissingException.php',
'OCA\\Encryption\\Exceptions\\PublicKeyMissingException' => __DIR__ . '/..' . '/../lib/Exceptions/PublicKeyMissingException.php',
'OCA\\Encryption\\HookManager' => __DIR__ . '/..' . '/../lib/HookManager.php',
'OCA\\Encryption\\Hooks\\Contracts\\IHook' => __DIR__ . '/..' . '/../lib/Hooks/Contracts/IHook.php',
'OCA\\Encryption\\Hooks\\UserHooks' => __DIR__ . '/..' . '/../lib/Hooks/UserHooks.php',
'OCA\\Encryption\\KeyManager' => __DIR__ . '/..' . '/../lib/KeyManager.php',
'OCA\\Encryption\\Listeners\\UserEventsListener' => __DIR__ . '/..' . '/../lib/Listeners/UserEventsListener.php',
'OCA\\Encryption\\Migration\\SetMasterKeyStatus' => __DIR__ . '/..' . '/../lib/Migration/SetMasterKeyStatus.php',
'OCA\\Encryption\\Recovery' => __DIR__ . '/..' . '/../lib/Recovery.php',
'OCA\\Encryption\\Services\\PassphraseService' => __DIR__ . '/..' . '/../lib/Services/PassphraseService.php',
'OCA\\Encryption\\Session' => __DIR__ . '/..' . '/../lib/Session.php',
'OCA\\Encryption\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',
'OCA\\Encryption\\Settings\\Personal' => __DIR__ . '/..' . '/../lib/Settings/Personal.php',
Expand Down
72 changes: 35 additions & 37 deletions apps/encryption/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
*/
namespace OCA\Encryption\AppInfo;

use OC\Core\Events\BeforePasswordResetEvent;
use OC\Core\Events\PasswordResetEvent;
use OCA\Encryption\Crypto\Crypt;
use OCA\Encryption\Crypto\DecryptAll;
use OCA\Encryption\Crypto\EncryptAll;
use OCA\Encryption\Crypto\Encryption;
use OCA\Encryption\HookManager;
use OCA\Encryption\Hooks\UserHooks;
use OCA\Encryption\KeyManager;
use OCA\Encryption\Recovery;
use OCA\Encryption\Listeners\UserEventsListener;
use OCA\Encryption\Session;
use OCA\Encryption\Users\Setup;
use OCA\Encryption\Util;
Expand All @@ -23,7 +23,14 @@
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Encryption\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserSession;
use OCP\User\Events\BeforePasswordUpdatedEvent;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserCreatedEvent;
use OCP\User\Events\UserDeletedEvent;
use Psr\Log\LoggerInterface;

class Application extends App implements IBootstrap {
Expand All @@ -49,46 +56,37 @@ public function boot(IBootContext $context): void {
}

$context->injectFn($this->registerEncryptionModule(...));
$context->injectFn($this->registerHooks(...));
$context->injectFn($this->registerEventListeners(...));
$context->injectFn($this->setUp(...));
});
}

public function setUp(IManager $encryptionManager) {
if ($encryptionManager->isEnabled()) {
/** @var Setup $setup */
$setup = $this->getContainer()->query(Setup::class);
$setup = $this->getContainer()->get(Setup::class);
$setup->setupSystem();
}
}

/**
* register hooks
*/
public function registerHooks(IConfig $config) {
if (!$config->getSystemValueBool('maintenance')) {
$container = $this->getContainer();
$server = $container->getServer();
// Register our hooks and fire them.
$hookManager = new HookManager();

$hookManager->registerHook([
new UserHooks($container->query(KeyManager::class),
$server->getUserManager(),
$server->get(LoggerInterface::class),
$container->query(Setup::class),
$server->getUserSession(),
$container->query(Util::class),
$container->query(Session::class),
$container->query(Crypt::class),
$container->query(Recovery::class))
]);
public function registerEventListeners(IConfig $config, IEventDispatcher $eventDispatcher, IManager $encryptionManager): void {
if (!$encryptionManager->isEnabled()) {
return;
}

$hookManager->fireHooks();
} else {
if ($config->getSystemValueBool('maintenance')) {
// Logout user if we are in maintenance to force re-login
$this->getContainer()->getServer()->getUserSession()->logout();
$this->getContainer()->get(IUserSession::class)->logout();
return;
}

// No maintenance so register all events
$eventDispatcher->addServiceListener(UserCreatedEvent::class, UserEventsListener::class);
$eventDispatcher->addServiceListener(UserDeletedEvent::class, UserEventsListener::class);
$eventDispatcher->addServiceListener(BeforePasswordUpdatedEvent::class, UserEventsListener::class);
$eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, UserEventsListener::class);
$eventDispatcher->addServiceListener(BeforePasswordResetEvent::class, UserEventsListener::class);
$eventDispatcher->addServiceListener(PasswordResetEvent::class, UserEventsListener::class);
}

public function registerEncryptionModule(IManager $encryptionManager) {
Expand All @@ -99,14 +97,14 @@ public function registerEncryptionModule(IManager $encryptionManager) {
Encryption::DISPLAY_NAME,
function () use ($container) {
return new Encryption(
$container->query(Crypt::class),
$container->query(KeyManager::class),
$container->query(Util::class),
$container->query(Session::class),
$container->query(EncryptAll::class),
$container->query(DecryptAll::class),
$container->getServer()->get(LoggerInterface::class),
$container->getServer()->getL10N($container->getAppName())
$container->get(Crypt::class),
$container->get(KeyManager::class),
$container->get(Util::class),
$container->get(Session::class),
$container->get(EncryptAll::class),
$container->get(DecryptAll::class),
$container->get(LoggerInterface::class),
$container->get(IL10N::class),
come-nc marked this conversation as resolved.
Show resolved Hide resolved
);
});
}
Expand Down
2 changes: 1 addition & 1 deletion apps/encryption/lib/Crypto/Crypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(
/**
* create new private/public key-pair for user
*
* @return array|bool
* @return array{publicKey: string, privateKey: string}|false
*/
public function createKeyPair() {
$res = $this->getOpenSSLPKey();
Expand Down
43 changes: 0 additions & 43 deletions apps/encryption/lib/HookManager.php

This file was deleted.

17 changes: 0 additions & 17 deletions apps/encryption/lib/Hooks/Contracts/IHook.php

This file was deleted.

Loading
Loading