Skip to content

Commit

Permalink
Change the notification's action to redirect to the settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed Aug 1, 2018
1 parent 78dfd05 commit 5d4b2bc
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 7 deletions.
24 changes: 19 additions & 5 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
]
];
111 changes: 111 additions & 0 deletions lib/Controller/NotificationRedirectorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* @author Juan Pablo Villafáñez <[email protected]>
* @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
* @NoCSRFRequired
*
* @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
* @NoCSRFRequired
*
* @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]);
}
}
28 changes: 26 additions & 2 deletions lib/Jobs/PasswordExpirationNotifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ private function sendAboutToExpireNotification(OldPassword $passInfo, $expiratio

$linkAction = $notification->createAction();
$linkAction->setLabel('Change password')
->setLink($this->getNotificationLink(), 'GET');
->setLink($this->getActionLinkAboutToExpire($passInfo->getId()), 'POST');

if (\method_exists($linkAction, 'setRedirect')) {
$linkAction->setRedirect(true);
}

$notification->addAction($linkAction);

$this->manager->notify($notification);
Expand Down Expand Up @@ -184,7 +189,12 @@ private function sendPassExpiredNotification(OldPassword $passInfo, $expirationT

$linkAction = $notification->createAction();
$linkAction->setLabel('Change password')
->setLink($this->getNotificationLink(), 'GET');
->setLink($this->getActionLinkExpired($passInfo->getId()), 'POST');

if (\method_exists($linkAction, 'setRedirect')) {
$linkAction->setRedirect(true);
}

$notification->addAction($linkAction);

$this->manager->notify($notification);
Expand All @@ -198,4 +208,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']
);
}
}

0 comments on commit 5d4b2bc

Please sign in to comment.