Skip to content

Commit

Permalink
Redirect "Change Password" action to settings page
Browse files Browse the repository at this point in the history
It works because the target URL will return a success code despite the
ajax call. Then the ajax call of notifications app triggers the JS
handler for "OCA.Notifications.Action" in which we can then actually
redirect to the target URL.
  • Loading branch information
PVince81 committed Jul 10, 2018
1 parent b86ed96 commit be90884
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
9 changes: 9 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,12 @@

$app = new \OCA\PasswordPolicy\AppInfo\Application();
$app->registerNotifier();

$request = \OC::$server->getRequest();
if (\OC::$server->getUserSession() !== null && \OC::$server->getUserSession()->getUser() !== null
&& substr($request->getScriptName(), 0 - strlen('/index.php')) === '/index.php'
&& substr($request->getPathInfo(), 0, strlen('/s/')) !== '/s/'
&& substr($request->getPathInfo(), 0, strlen('/login')) !== '/login') {

\OCP\Util::addScript('password_policy', 'notification');
}
25 changes: 25 additions & 0 deletions js/notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2018 Vincent Petry <[email protected]>
*
* This file is licensed under the Affero General Public License version 3
* or later.
*
* See the COPYING-README file.
*
*/
(function () {

$(document).ready(function() {
// convert action URL to redirect
$('body').on('OCA.Notification.Action', function(e) {
if (e.notification.app === 'password_policy'
&& (e.notification.object_type === 'about_to_expire' || e.notification.object_type === 'expired')
&& e.action.type === 'GET'
) {
OC.redirect(e.notification.link);
return false;
}
});
});
})();

7 changes: 3 additions & 4 deletions lib/Jobs/PasswordExpirationNotifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,9 @@ private function sendPassExpiredNotification(OldPassword $passInfo, $expirationT
}

private function getNotificationLink() {
$link = $this->urlGenerator->linkToRouteAbsolute(
'password_policy.password.show',
['redirect_url' => '']
return $this->urlGenerator->linkToRouteAbsolute(
'settings.SettingsPage.getPersonal',
['sectionid' => 'general']
);
return $link;
}
}
26 changes: 25 additions & 1 deletion lib/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ private function formatAboutToExpire(INotification $notification, L10N $l) {
);
}

foreach ($notification->getActions() as $action) {
switch ($action->getLabel()) {
case 'Change password':
$action->setParsedLabel(
(string) $l->t('Change Password')
);
break;
}

$notification->addParsedAction($action);
}

return $notification;
}

Expand All @@ -103,6 +115,18 @@ private function formatExpired(INotification $notification, L10N $l) {
(string) $l->t('You have to change your password before you can access again', $messageParams)
);

foreach ($notification->getActions() as $action) {
switch ($action->getLabel()) {
case 'Change password':
$action->setParsedLabel(
(string) $l->t('Change Password')
);
break;
}

$notification->addParsedAction($action);
}

return $notification;
}
}
}

0 comments on commit be90884

Please sign in to comment.