-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send notification when passwords are about to expire
- Loading branch information
1 parent
ef0ce49
commit d7885c8
Showing
10 changed files
with
488 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?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\Jobs; | ||
|
||
use OC\BackgroundJob\TimedJob; | ||
use OCP\Notification\IManager; | ||
use OCP\IUserManager; | ||
use OCP\IConfig; | ||
use OCP\IURLGenerator; | ||
use OCP\ILogger; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCA\PasswordPolicy\Db\OldPasswordMapper; | ||
use OCA\PasswordPolicy\Db\OldPassword; | ||
use OCA\PasswordPolicy\UserNotificationConfigHandler; | ||
|
||
class PasswordExpirationNotifierJob extends TimedJob { | ||
/** @var OldPasswordMapper */ | ||
private $mapper; | ||
|
||
/** @var $manager */ | ||
private $manager; | ||
|
||
/** @var IUserManager */ | ||
private $userManager; | ||
|
||
/** @var UserNotificationConfigHandler */ | ||
private $unConfigHandler; | ||
|
||
/** @var ITimeFactory */ | ||
private $timeFactory; | ||
|
||
/** @var IURLGenerator */ | ||
private $urlGenerator; | ||
|
||
/** @var ILogger */ | ||
private $logger; | ||
|
||
public function __construct( | ||
OldPasswordMapper $mapper, | ||
IManager $manager, | ||
IUserManager $userManager, | ||
UserNotificationConfigHandler $unConfigHandler, | ||
ITimeFactory $timeFactory, | ||
IURLGenerator $urlGenerator, | ||
ILogger $logger | ||
) { | ||
$this->mapper = $mapper; | ||
$this->manager = $manager; | ||
$this->userManager = $userManager; | ||
$this->unConfigHandler = $unConfigHandler; | ||
$this->timeFactory = $timeFactory; | ||
$this->urlGenerator = $urlGenerator; | ||
$this->logger = $logger; | ||
|
||
$this->setInterval(12 * 60 * 60); | ||
} | ||
|
||
protected function run($arguments) { | ||
$expirationTime = $this->unConfigHandler->getExpirationTime(); | ||
if ($expirationTime === null) { | ||
return; // expiration not configured | ||
} | ||
|
||
$expirationTime = $expirationTime * 24 * 60 * 60; // convert days to seconds | ||
$expirationTimeNotification = $this->unConfigHandler->getExpirationTimeForNormalNotification(); | ||
if ($expirationTimeNotification === null) { | ||
$expirationTimeNotification = 0; | ||
} | ||
|
||
// ensure ranges are correct | ||
if ($expirationTime <= $expirationTimeNotification) { | ||
$message = "wrong expiration range: normal ($expirationTimeNotification) < expired ($expirationTime)"; | ||
$this->logger->warning($message, ['app' => 'password_policy']); | ||
return; | ||
} | ||
|
||
$notifyAfter = $expirationTime - $expirationTimeNotification; | ||
|
||
$currentTime = $this->timeFactory->getTime(); | ||
|
||
$maxTimestamp = $currentTime - $notifyAfter; | ||
// passwords changed before $maxTimestamp are expired or about to be expired | ||
// according to the expiration time range | ||
|
||
$oldPasswordsAboutToExpire = $this->mapper->getPasswordsAboutToExpire($maxTimestamp); | ||
foreach ($oldPasswordsAboutToExpire as $passInfo) { | ||
$elapsedTime = $currentTime - $passInfo->getChangeTime(); | ||
if ($elapsedTime >= $expirationTime) { | ||
$this->logger->debug("password timestamp for {$passInfo->getUid()}: {$passInfo->getChangeTime()}; elapsed time: {$elapsedTime} -> EXPIRED", ['app' => 'password_policy']); | ||
$this->sendPassExpiredNotification($passInfo, $expirationTime); | ||
} elseif ($elapsedTime >= $notifyAfter) { | ||
$this->logger->debug("password timestamp for {$passInfo->getUid()}: {$passInfo->getChangeTime()}; elapsed time: {$elapsedTime} -> NOTIFY", ['app' => 'password_policy']); | ||
$this->sendAboutToExpireNotification($passInfo, $expirationTime); | ||
} | ||
} | ||
} | ||
|
||
private function sendAboutToExpireNotification(OldPassword $passInfo, $expirationTime) { | ||
if ($this->unConfigHandler->isSentAboutToExpireNotification($passInfo)) { | ||
return; // notification already sent | ||
} | ||
|
||
$notificationTimestamp = $this->timeFactory->getTime(); | ||
|
||
$notification = $this->manager->createNotification(); | ||
$notification->setApp('password_policy') | ||
->setUser($passInfo->getUid()) | ||
->setDateTime(new \DateTime("@{$notificationTimestamp}")) | ||
->setObject('about_to_expire', $passInfo->getId()) | ||
->setSubject('about_to_expire', [$passInfo->getChangeTime(), $expirationTime]) | ||
->setMessage('about_to_expire', [$passInfo->getChangeTime(), $expirationTime]) | ||
->setLink($this->getNotificationLink()); | ||
$this->manager->notify($notification); | ||
|
||
$this->unConfigHandler->markAboutToExpireNotificationSentFor($passInfo); | ||
} | ||
|
||
private function sendPassExpiredNotification(OldPassword $passInfo, $expirationTime) { | ||
if ($this->unConfigHandler->isSentExpiredNotification($passInfo)) { | ||
return; // notification already sent | ||
} | ||
|
||
$notificationTimestamp = $this->timeFactory->getTime(); | ||
|
||
$notification = $this->manager->createNotification(); | ||
$notification->setApp('password_policy') | ||
->setUser($passInfo->getUid()) | ||
->setDateTime(new \DateTime("@{$notificationTimestamp}")) | ||
->setObject('expired', $passInfo->getId()) | ||
->setSubject('expired', [$passInfo->getChangeTime(), $expirationTime]) | ||
->setMessage('expired', [$passInfo->getChangeTime(), $expirationTime]) | ||
->setLink($this->getNotificationLink()); | ||
$this->manager->notify($notification); | ||
|
||
$this->unConfigHandler->markExpiredNotificationSentFor($passInfo); | ||
} | ||
|
||
private function getNotificationLink() { | ||
$link = $this->urlGenerator->linkToRouteAbsolute( | ||
'password_policy.password.show', | ||
['redirect_url' => ''] | ||
); | ||
return $link; | ||
} | ||
} |
Oops, something went wrong.