-
Notifications
You must be signed in to change notification settings - Fork 6
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
send notification when passwords are about to expire #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
}); | ||
}); | ||
})(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
use OCP\IRequest; | ||
use OCP\Settings\ISettings; | ||
use OCP\Template; | ||
use OCA\PasswordPolicy\UserNotificationConfigHandler; | ||
|
||
class SettingsController extends Controller implements ISettings { | ||
|
||
|
@@ -49,13 +50,26 @@ class SettingsController extends Controller implements ISettings { | |
'spv_password_history_value' => 3, | ||
'spv_user_password_expiration_checked' => false, | ||
'spv_user_password_expiration_value' => 90, | ||
'spv_user_password_expiration_notification_checked' => false, | ||
'spv_user_password_expiration_notification_value' => UserNotificationConfigHandler::DEFAULT_EXPIRATION_FOR_NORMAL_NOTIFICATION, | ||
'spv_user_password_force_change_on_first_login_checked' => false, | ||
'spv_expiration_password_checked' => false, | ||
'spv_expiration_password_value' => 7, | ||
'spv_expiration_nopassword_checked' => false, | ||
'spv_expiration_nopassword_value' => 7, | ||
]; | ||
|
||
/** | ||
* functions to convert values between what is shown and what is stored | ||
* these functions must be defined in this class, they're per config key | ||
*/ | ||
const CONVERSIONS = [ | ||
'spv_user_password_expiration_notification_value' => [ | ||
'in' => 'daysToSeconds', | ||
'out' => 'secondsToDays', | ||
], | ||
]; | ||
|
||
public function __construct($appName, | ||
IRequest $request, | ||
IConfig $config) { | ||
|
@@ -71,6 +85,10 @@ public function updatePolicy() { | |
if ($this->request->getParam($key) !== null) { | ||
if ($key !== 'spv_def_special_chars_value' && \substr($key, -6) === '_value') { | ||
$value = \min(\max(0, (int)$this->request->getParam($key)), 255); | ||
if (isset(self::CONVERSIONS[$key]['in'])) { | ||
$convertFuncName = self::CONVERSIONS[$key]['in']; | ||
$value = $this->$convertFuncName($value); | ||
} | ||
$this->config->setAppValue('password_policy', $key, $value); | ||
} else { | ||
$this->config->setAppValue('password_policy', $key, \strip_tags($this->request->getParam($key))); | ||
|
@@ -92,9 +110,33 @@ public function getPriority() { | |
public function getPanel() { | ||
$template = new Template('password_policy', 'admin'); | ||
foreach(self::DEFAULTS as $key => $default) { | ||
$template->assign($key, $this->config->getAppValue('password_policy', $key, $default)); | ||
$value = $this->config->getAppValue('password_policy', $key, $default); | ||
if (isset(self::CONVERSIONS[$key]['out'])) { | ||
$convertFuncName = self::CONVERSIONS[$key]['out']; | ||
$value = $this->$convertFuncName($value); | ||
} | ||
$template->assign($key, $value); | ||
} | ||
return $template; | ||
} | ||
|
||
/** | ||
* Convert the days to seconds | ||
* @param int $days | ||
* @return int the number of seconds | ||
*/ | ||
private function daysToSeconds($days) { | ||
return $days * 24 * 60 * 60; | ||
} | ||
|
||
/** | ||
* Convert seconds to days. The value will always be rounded up, | ||
* so 1 second will be converted to 1 day | ||
* @param int $seconds the number of seconds to be converted | ||
* @return int the number of days in those seconds, rounded up | ||
*/ | ||
private function secondsToDays($seconds) { | ||
$floatDays = $seconds / (24 * 60 * 60); | ||
return \intval(\ceil($floatDays)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if using so this means if someone uses occ to set a seconds value in the DB, the UI would show it as the higher value for days. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The UI is only displaying integer number of days. So we have to do something here. When testing, I will understand that it will show 1 day, but underneath I actually set something like 300 seconds only. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure either. Maybe we should take the restrictive approach and show 0 days. This will send notifications before the expected time. The good thing is that it's just a visualization problem in the UI if you set the value via occ. The notifications will be sent accordingly to the value stored in the DB (in seconds) |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the reason for this
if
condition obvious? I could use a comment, or some way to understand what is the script+path pattern aimed for and why.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy-pasted from notifications app: https://github.com/owncloud/notifications/blob/master/appinfo/app.php#L29
I'll add a comment