diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 277036d79f41b6af0ea4927c17d09de91d8125f0..68898786f4921f6c6351eb3b0f17406deaa4e626 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -29,6 +29,7 @@ namespace OCA\EmailRecovery\AppInfo; use OCA\EmailRecovery\Listeners\BeforeTemplateRenderedListener; use OCA\EmailRecovery\Listeners\UserConfigChangedListener; use OCA\EmailRecovery\Listeners\UserChangedListener; +use OCA\EmailRecovery\Listeners\BeforeUserDeletedListener; use OCA\EmailRecovery\Listeners\BeforeUserRegisteredListener; use OCA\EmailRecovery\Notification\Notifier; use OCP\AppFramework\App; @@ -39,6 +40,7 @@ use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\Notification\IManager as INotificationManager; use OCP\User\Events\UserConfigChangedEvent; use OCP\User\Events\UserChangedEvent; +use OCP\User\Events\BeforeUserDeletedEvent; use OCA\EcloudAccounts\Event\BeforeUserRegisteredEvent; class Application extends App implements IBootstrap { @@ -53,6 +55,7 @@ class Application extends App implements IBootstrap { $context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class); $context->registerEventListener(UserConfigChangedEvent::class, UserConfigChangedListener::class); $context->registerEventListener(UserChangedEvent::class, UserChangedListener::class); + $context->registerEventListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class); } public function boot(IBootContext $context): void { $context->injectFn([$this, 'registerNotifier']); diff --git a/lib/Controller/EmailRecoveryApiController.php b/lib/Controller/EmailRecoveryApiController.php index f60a18f7895e71cee4672398496746952f552a7f..ccda36000fd239382377f8d1b3f18f8f3da4cf6d 100644 --- a/lib/Controller/EmailRecoveryApiController.php +++ b/lib/Controller/EmailRecoveryApiController.php @@ -77,7 +77,7 @@ class EmailRecoveryApiController extends ApiController { } try { - if ($this->recoveryEmailService->validateRecoveryEmail($id, $email)) { + if ($this->recoveryEmailService->validateRecoveryEmail($email, $id)) { $this->recoveryEmailService->updateRecoveryEmail($id, $email); return $response; } diff --git a/lib/Controller/EmailRecoveryController.php b/lib/Controller/EmailRecoveryController.php index d9230c65595f0a82aeded45ab35b6145bb2e4d9e..e7fc331a172979bba1475106ed67edbef73aeaa1 100644 --- a/lib/Controller/EmailRecoveryController.php +++ b/lib/Controller/EmailRecoveryController.php @@ -175,7 +175,7 @@ class EmailRecoveryController extends Controller { private function handleRecoveryEmailLogic(string $recoveryEmail, string $userId, JSONResponse $response) { try { - if ($this->recoveryEmailService->validateRecoveryEmail($userId, $recoveryEmail)) { + if ($this->recoveryEmailService->validateRecoveryEmail($recoveryEmail, $userId)) { $this->recoveryEmailService->updateRecoveryEmail($userId, $recoveryEmail); $response->setStatus(200); } diff --git a/lib/Listeners/BeforeUserDeletedListener.php b/lib/Listeners/BeforeUserDeletedListener.php new file mode 100644 index 0000000000000000000000000000000000000000..ae28c0ce46ffbbe5a6f293ddaf026784454ee6a0 --- /dev/null +++ b/lib/Listeners/BeforeUserDeletedListener.php @@ -0,0 +1,40 @@ +logger = $logger; + $this->recoveryEmailService = $recoveryEmailService; + $this->userManager = $userManager; + } + + public function handle(Event $event): void { + if (!($event instanceof BeforeUserDeletedEvent)) { + return; + } + $user = $event->getUser()->getUID(); + try { + $userData = $this->userManager->get($user); + $userEmailAddress = $userData->getEMailAddress(); + if (!empty($userEmailAddress)) { + $this->recoveryEmailService->unrestrictEmail($userEmailAddress); + } + } catch (\Throwable $e) { + $this->logger->error('Error in removing email from restricted list: '.$e->getMessage()); + } + } +} diff --git a/lib/Listeners/BeforeUserRegisteredListener.php b/lib/Listeners/BeforeUserRegisteredListener.php index 5e598bfcef53ac4fc5a82092d42e3ca1bf1e16ca..5d258a39231cfce8fa039b9816777bfd2f8f646b 100644 --- a/lib/Listeners/BeforeUserRegisteredListener.php +++ b/lib/Listeners/BeforeUserRegisteredListener.php @@ -36,7 +36,7 @@ class BeforeUserRegisteredListener implements IEventListener { */ protected function validateRecoveryEmail(string $recoveryEmail, string $language): void { try { - $this->recoveryEmailService->validateRecoveryEmail('', $recoveryEmail, $language); + $this->recoveryEmailService->validateRecoveryEmail($recoveryEmail, '', $language); } catch (\Exception $e) { throw new RecoveryEmailValidationException($e->getMessage()); } diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index d6892c81cd2efd836d37d460b400a1abcb2579e9..e71d198c148ce3acf2897a6e6fad812b8d45c2ff 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -109,7 +109,7 @@ class RecoveryEmailService { return true; } - public function validateRecoveryEmail(string $username = '', string $recoveryEmail, string $language = 'en') : bool { + public function validateRecoveryEmail(string $recoveryEmail, string $username = '', string $language = 'en') : bool { $email = ''; if ($username != '') { $user = $this->userManager->get($username);