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

Change the notification's action to redirect to the settings page #78

Closed
wants to merge 2 commits into from
Closed
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
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',
],
]
];
109 changes: 109 additions & 0 deletions lib/Controller/NotificationRedirectorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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
*
* @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]);
}
}
18 changes: 16 additions & 2 deletions lib/Jobs/PasswordExpirationNotifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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']
);
}
}