diff options
author | Georg Ehrke <georgehrke@users.noreply.github.com> | 2019-07-02 08:58:21 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-02 08:58:21 +0000 |
commit | 9f7fca49bbeb13104457d9f74645d3715f98789f (patch) | |
tree | 55687534362b63a1b318b2a36de00b1e463caf78 /lib/private | |
parent | 9beda013c159e857113de1d9ab3314ed9c85d5f9 (diff) | |
parent | aa6622ccef610b4f3bfb57facb80e3e6613fd22e (diff) | |
download | nextcloud-server-9f7fca49bbeb13104457d9f74645d3715f98789f.tar.gz nextcloud-server-9f7fca49bbeb13104457d9f74645d3715f98789f.zip |
Merge pull request #15775 from nextcloud/refactor/decouple-remote-wipe-notifications
Decouple remote wipe notifcation channels with events
Diffstat (limited to 'lib/private')
6 files changed, 269 insertions, 47 deletions
diff --git a/lib/private/Authentication/Events/ARemoteWipeEvent.php b/lib/private/Authentication/Events/ARemoteWipeEvent.php new file mode 100644 index 00000000000..f6c08882143 --- /dev/null +++ b/lib/private/Authentication/Events/ARemoteWipeEvent.php @@ -0,0 +1,46 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * 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 <http://www.gnu.org/licenses/>. + */ + +namespace OC\Authentication\Events; + +use OC\Authentication\Token\IToken; +use OCP\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\GenericEvent; + +abstract class ARemoteWipeEvent extends Event { + + /** @var IToken */ + private $token; + + public function __construct(IToken $token) { + parent::__construct(); + $this->token = $token; + } + + public function getToken(): IToken { + return $this->token; + } + +} diff --git a/lib/private/Authentication/Events/RemoteWipeFinished.php b/lib/private/Authentication/Events/RemoteWipeFinished.php new file mode 100644 index 00000000000..79f6dcbffbe --- /dev/null +++ b/lib/private/Authentication/Events/RemoteWipeFinished.php @@ -0,0 +1,30 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * 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 <http://www.gnu.org/licenses/>. + */ + +namespace OC\Authentication\Events; + +class RemoteWipeFinished extends ARemoteWipeEvent { + +} diff --git a/lib/private/Authentication/Events/RemoteWipeStarted.php b/lib/private/Authentication/Events/RemoteWipeStarted.php new file mode 100644 index 00000000000..f3c0b628795 --- /dev/null +++ b/lib/private/Authentication/Events/RemoteWipeStarted.php @@ -0,0 +1,30 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * 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 <http://www.gnu.org/licenses/>. + */ + +namespace OC\Authentication\Events; + +class RemoteWipeStarted extends ARemoteWipeEvent { + +} diff --git a/lib/private/Authentication/Listeners/RemoteWipeActivityListener.php b/lib/private/Authentication/Listeners/RemoteWipeActivityListener.php new file mode 100644 index 00000000000..c8cd92d2102 --- /dev/null +++ b/lib/private/Authentication/Listeners/RemoteWipeActivityListener.php @@ -0,0 +1,80 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * 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 <http://www.gnu.org/licenses/>. + */ + +namespace OC\Authentication\Listeners; + +use BadMethodCallException; +use OC\Authentication\Events\ARemoteWipeEvent; +use OC\Authentication\Events\RemoteWipeFinished; +use OC\Authentication\Events\RemoteWipeStarted; +use OC\Authentication\Token\IToken; +use OCP\Activity\IManager as IActvityManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\ILogger; + +class RemoteWipeActivityListener implements IEventListener { + + /** @var IActvityManager */ + private $activityManager; + + /** @var ILogger */ + private $logger; + + public function __construct(IActvityManager $activityManager, + ILogger $logger) { + $this->activityManager = $activityManager; + $this->logger = $logger; + } + + public function handle(Event $event): void { + if ($event instanceof RemoteWipeStarted) { + $this->publishActivity('remote_wipe_start', $event->getToken()); + } else if ($event instanceof RemoteWipeFinished) { + $this->publishActivity('remote_wipe_finish', $event->getToken()); + } + } + + private function publishActivity(string $event, IToken $token): void { + $activity = $this->activityManager->generateEvent(); + $activity->setApp('core') + ->setType('security') + ->setAuthor($token->getUID()) + ->setAffectedUser($token->getUID()) + ->setSubject($event, [ + 'name' => $token->getName(), + ]); + try { + $this->activityManager->publish($activity); + } catch (BadMethodCallException $e) { + $this->logger->logException($e, [ + 'app' => 'core', + 'level' => ILogger::WARN, + 'message' => 'could not publish activity', + ]); + } + } + +} diff --git a/lib/private/Authentication/Listeners/RemoteWipeNotificationsListener.php b/lib/private/Authentication/Listeners/RemoteWipeNotificationsListener.php new file mode 100644 index 00000000000..ffddddff1d8 --- /dev/null +++ b/lib/private/Authentication/Listeners/RemoteWipeNotificationsListener.php @@ -0,0 +1,71 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * 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 <http://www.gnu.org/licenses/>. + */ + +namespace OC\Authentication\Listeners; + +use OC\Authentication\Events\ARemoteWipeEvent; +use OC\Authentication\Events\RemoteWipeFinished; +use OC\Authentication\Events\RemoteWipeStarted; +use OC\Authentication\Token\IToken; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Notification\IManager as INotificationManager; + +class RemoteWipeNotificationsListener implements IEventListener { + + /** @var INotificationManager */ + private $notificationManager; + + /** @var ITimeFactory */ + private $timeFactory; + + public function __construct(INotificationManager $notificationManager, + ITimeFactory $timeFactory) { + $this->notificationManager = $notificationManager; + $this->timeFactory = $timeFactory; + } + + public function handle(Event $event): void { + if ($event instanceof RemoteWipeStarted) { + $this->sendNotification('remote_wipe_start', $event->getToken()); + } else if ($event instanceof RemoteWipeFinished) { + $this->sendNotification('remote_wipe_finish', $event->getToken()); + } + } + + private function sendNotification(string $event, IToken $token): void { + $notification = $this->notificationManager->createNotification(); + $notification->setApp('auth') + ->setUser($token->getUID()) + ->setDateTime($this->timeFactory->getDateTime()) + ->setObject('token', $token->getId()) + ->setSubject($event, [ + 'name' => $token->getName(), + ]); + $this->notificationManager->notify($notification); + } + +} diff --git a/lib/private/Authentication/Token/RemoteWipe.php b/lib/private/Authentication/Token/RemoteWipe.php index 6091d30fc25..5534ff1cba1 100644 --- a/lib/private/Authentication/Token/RemoteWipe.php +++ b/lib/private/Authentication/Token/RemoteWipe.php @@ -26,39 +26,34 @@ declare(strict_types=1); namespace OC\Authentication\Token; use BadMethodCallException; +use OC\Authentication\Events\RemoteWipeFinished; +use OC\Authentication\Events\RemoteWipeStarted; use OC\Authentication\Exceptions\InvalidTokenException; use OC\Authentication\Exceptions\WipeTokenException; +use OCP\Activity\IEvent; use OCP\Activity\IManager as IActivityManager; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\EventDispatcher\IEventDispatcher; use OCP\ILogger; use OCP\Notification\IManager as INotificationManager; +use Symfony\Component\EventDispatcher\EventDispatcher; class RemoteWipe { /** @var IProvider */ private $tokenProvider; - /** @var IActivityManager */ - private $activityManager; - - /** @var INotificationManager */ - private $notificationManager; - - /** @var ITimeFactory */ - private $timeFactory; + /** @var IEventDispatcher */ + private $eventDispatcher; /** @var ILogger */ private $logger; public function __construct(IProvider $tokenProvider, - IActivityManager $activityManager, - INotificationManager $notificationManager, - ITimeFactory $timeFactory, + IEventDispatcher $eventDispatcher, ILogger $logger) { $this->tokenProvider = $tokenProvider; - $this->activityManager = $activityManager; - $this->notificationManager = $notificationManager; - $this->timeFactory = $timeFactory; + $this->eventDispatcher = $eventDispatcher; $this->logger = $logger; } @@ -83,8 +78,8 @@ class RemoteWipe { $dbToken = $e->getToken(); $this->logger->info("user " . $dbToken->getUID() . " started a remote wipe"); - $this->sendNotification('remote_wipe_start', $e->getToken()); - $this->publishActivity('remote_wipe_start', $e->getToken()); + + $this->eventDispatcher->dispatch(RemoteWipeStarted::class, new RemoteWipeStarted($dbToken)); return true; } @@ -111,39 +106,9 @@ class RemoteWipe { $this->tokenProvider->invalidateToken($token); $this->logger->info("user " . $dbToken->getUID() . " finished a remote wipe"); - $this->sendNotification('remote_wipe_finish', $e->getToken()); - $this->publishActivity('remote_wipe_finish', $e->getToken()); + $this->eventDispatcher->dispatch(RemoteWipeFinished::class, new RemoteWipeFinished($dbToken)); return true; } - private function publishActivity(string $event, IToken $token): void { - $activity = $this->activityManager->generateEvent(); - $activity->setApp('core') - ->setType('security') - ->setAuthor($token->getUID()) - ->setAffectedUser($token->getUID()) - ->setSubject($event, [ - 'name' => $token->getName(), - ]); - try { - $this->activityManager->publish($activity); - } catch (BadMethodCallException $e) { - $this->logger->warning('could not publish activity', ['app' => 'core']); - $this->logger->logException($e, ['app' => 'core']); - } - } - - private function sendNotification(string $event, IToken $token): void { - $notification = $this->notificationManager->createNotification(); - $notification->setApp('auth') - ->setUser($token->getUID()) - ->setDateTime($this->timeFactory->getDateTime()) - ->setObject('token', $token->getId()) - ->setSubject($event, [ - 'name' => $token->getName(), - ]); - $this->notificationManager->notify($notification); - } - } |