diff --git a/appinfo/routes.php b/appinfo/routes.php index 0976e997..d1e6d091 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -19,8 +19,22 @@ * */ -return ['routes' => [ - ['name' => 'Settings#updatePolicy', 'url' => '/update_policy', 'verb' => 'POST'], - ['name' => 'password#show', 'url' => '/update_password', 'verb' => 'GET'], - ['name' => 'password#update', 'url' => '/update_password', 'verb' => 'POST'], -]]; +return [ + 'routes' => [ + ['name' => 'Settings#updatePolicy', 'url' => '/update_policy', 'verb' => 'POST'], + ['name' => 'password#show', 'url' => '/update_password', 'verb' => 'GET'], + ['name' => 'password#update', 'url' => '/update_password', 'verb' => 'POST'], + ], + 'ocs' => [ + [ + 'name' => 'NotificationRedirector#markAndRedirectAboutToExpire', + 'url' => '/process_notification/about_to_expire/{id}', + 'verb' => 'POST', + ], + [ + 'name' => 'NotificationRedirector#markAndRedirectExpired', + 'url' => '/process_notification/expired/{id}', + 'verb' => 'POST', + ], + ] +]; diff --git a/lib/Controller/NotificationRedirectorController.php b/lib/Controller/NotificationRedirectorController.php new file mode 100644 index 00000000..3edc9825 --- /dev/null +++ b/lib/Controller/NotificationRedirectorController.php @@ -0,0 +1,109 @@ +<?php +/** + * @author Juan Pablo Villafáñez <jvillafanez@solidgear.es> + * @copyright Copyright (c) 2018, ownCloud GmbH + * @license GPL-2.0 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\PasswordPolicy\Controller; + +use OC\OCS\Result; +use OCP\AppFramework\OCSController; +use OCP\Notification\IManager; +use OCP\IUserSession; +use OCP\IURLGenerator; +use OCP\IRequest; + +class NotificationRedirectorController extends OCSController { + /** @var IManager */ + private $notificationManager; + + /** @var IUserSession */ + private $session; + + /** @var IURLGenerator */ + private $urlGenerator; + + public function __construct( + $appName, + IRequest $request, + IManager $notificationManager, + IUserSession $session, + IURLGenerator $urlGenerator + ) { + parent::__construct($appName, $request); + $this->notificationManager = $notificationManager; + $this->session = $session; + $this->urlGenerator = $urlGenerator; + } + + /** + * @NoAdminRequired + * + * @param int $id + * @return Result + */ + public function markAndRedirectAboutToExpire($id) { + $currentUser = $this->session->getUser(); + if ($currentUser === null) { + return new Result(null, Http::STATUS_NO_CONTENT); + } + $userid = $currentUser->getUID(); + + $notification = $this->notificationManager->createNotification(); + $notification->setApp('password_policy') + ->setUser($userid) + ->setObject('about_to_expire', $id); + + $this->notificationManager->markProcessed($notification); + + $targetRedirection = $this->urlGenerator->linkToRouteAbsolute( + 'settings.SettingsPage.getPersonal', + ['sectionid' => 'general'] + ); + + return new Result(['redirectTo' => $targetRedirection]); + } + + /** + * @NoAdminRequired + * + * @param int $id + * @return Result + */ + public function markAndRedirectExpired($id) { + $currentUser = $this->session->getUser(); + if ($currentUser === null) { + return new Result(null, Http::STATUS_NO_CONTENT); + } + $userid = $currentUser->getUID(); + + $notification = $this->notificationManager->createNotification(); + $notification->setApp('password_policy') + ->setUser($userid) + ->setObject('expired', $id); + + $this->notificationManager->markProcessed($notification); + + $targetRedirection = $this->urlGenerator->linkToRouteAbsolute( + 'settings.SettingsPage.getPersonal', + ['sectionid' => 'general'] + ); + + return new Result(['redirectTo' => $targetRedirection]); + } +} diff --git a/lib/Jobs/PasswordExpirationNotifierJob.php b/lib/Jobs/PasswordExpirationNotifierJob.php index 5d7ed729..fe1eb6f2 100644 --- a/lib/Jobs/PasswordExpirationNotifierJob.php +++ b/lib/Jobs/PasswordExpirationNotifierJob.php @@ -147,7 +147,7 @@ private function sendAboutToExpireNotification(OldPassword $passInfo, $expiratio $linkAction = $notification->createAction(); $linkAction->setLabel('Change password') - ->setLink($this->getNotificationLink(), 'GET'); + ->setLink($this->getActionLinkAboutToExpire($passInfo->getId()), 'POST'); $notification->addAction($linkAction); $this->manager->notify($notification); @@ -184,7 +184,7 @@ private function sendPassExpiredNotification(OldPassword $passInfo, $expirationT $linkAction = $notification->createAction(); $linkAction->setLabel('Change password') - ->setLink($this->getNotificationLink(), 'GET'); + ->setLink($this->getActionLinkExpired($passInfo->getId()), 'POST'); $notification->addAction($linkAction); $this->manager->notify($notification); @@ -198,4 +198,18 @@ private function getNotificationLink() { ['sectionid' => 'general'] ); } + + private function getActionLinkAboutToExpire($id) { + return $this->urlGenerator->linkToRouteAbsolute( + 'ocs.password_policy.NotificationRedirector.markAndRedirectAboutToExpire', + ['id' => $id, 'format' => 'json'] + ); + } + + private function getActionLinkExpired($id) { + return $this->urlGenerator->linkToRouteAbsolute( + 'ocs.password_policy.NotificationRedirector.markAndRedirectExpired', + ['id' => $id, 'format' => 'json'] + ); + } }