diff --git a/lib/HooksHandler.php b/lib/HooksHandler.php index f0ee79c4..4f2dd413 100644 --- a/lib/HooksHandler.php +++ b/lib/HooksHandler.php @@ -33,6 +33,7 @@ use OCP\ISession; use OCP\IUser; use OCP\Security\IHasher; +use OCP\Notification\IManager; use Symfony\Component\EventDispatcher\GenericEvent; class HooksHandler { @@ -61,6 +62,9 @@ class HooksHandler { /** @var ISession */ private $session; + /** @var IManager */ + private $notificationManager; + /** @var UserNotificationConfigHandler */ private $userNotificationConfigHandler; @@ -73,6 +77,7 @@ public function __construct( PasswordExpired $passwordExpiredRule = null, OldPasswordMapper $oldPasswordMapper = null, ISession $session = null, + IManager $notificationManager = null, UserNotificationConfigHandler $userNotificationConfigHandler = null ) { $this->config = $config; @@ -83,6 +88,7 @@ public function __construct( $this->passwordExpiredRule = $passwordExpiredRule; $this->oldPasswordMapper = $oldPasswordMapper; $this->session = $session; + $this->notificationManager = $notificationManager; $this->userNotificationConfigHandler = $userNotificationConfigHandler; } @@ -109,6 +115,7 @@ private function fixDI() { $this->hasher ); $this->session = \OC::$server->getSession(); + $this->notificationManager = \OC::$server->getNotificationManager(); $this->userNotificationConfigHandler = new UserNotificationConfigHandler($this->config); } } @@ -187,12 +194,34 @@ public function saveOldPassword(GenericEvent $event) { $user = $this->getUser($event); $password = $event->getArgument('password'); + $userId = $user->getUID(); + $oldPassword = new OldPassword(); - $oldPassword->setUid($user->getUID()); + $oldPassword->setUid($userId); $oldPassword->setPassword($this->hasher->hash($password)); $oldPassword->setChangeTime($this->timeFactory->getTime()); $this->oldPasswordMapper->insert($oldPassword); - $this->userNotificationConfigHandler->resetExpirationMarks($user->getUID()); + + // get previous marks + $aboutToExpireMark = $this->userNotificationConfigHandler->getMarkAboutToExpireNotificationSentFor($userId); + $expiredMark = $this->userNotificationConfigHandler->getMarkExpiredNotificationSentFor($userId); + + $this->userNotificationConfigHandler->resetExpirationMarks($userId); + + if ($aboutToExpireMark !== null) { + $notification = $this->notificationManager->createNotification(); + $notification->setApp('password_policy') + ->setUser($userId) + ->setObject('about_to_expire', $aboutToExpireMark); + $this->notificationManager->markProcessed($notification); + } + if ($expiredMark !== null) { + $notification = $this->notificationManager->createNotification(); + $notification->setApp('password_policy') + ->setUser($userId) + ->setObject('expired', $expiredMark); + $this->notificationManager->markProcessed($notification); + } } /** diff --git a/lib/Jobs/PasswordExpirationNotifierJob.php b/lib/Jobs/PasswordExpirationNotifierJob.php index 4a1f5225..43db021c 100644 --- a/lib/Jobs/PasswordExpirationNotifierJob.php +++ b/lib/Jobs/PasswordExpirationNotifierJob.php @@ -114,6 +114,7 @@ private function sendAboutToExpireNotification(OldPassword $passInfo, $expiratio $notificationTimestamp = $this->timeFactory->getTime(); + // we'll use the id of the passInfo as object id and marker $notification = $this->manager->createNotification(); $notification->setApp('password_policy') ->setUser($passInfo->getUid()) @@ -140,6 +141,7 @@ private function sendPassExpiredNotification(OldPassword $passInfo, $expirationT $notificationTimestamp = $this->timeFactory->getTime(); + // we'll use the id of the passInfo as object id and marker $notification = $this->manager->createNotification(); $notification->setApp('password_policy') ->setUser($passInfo->getUid()) diff --git a/lib/UserNotificationConfigHandler.php b/lib/UserNotificationConfigHandler.php index d17c0a93..5cab1281 100644 --- a/lib/UserNotificationConfigHandler.php +++ b/lib/UserNotificationConfigHandler.php @@ -92,7 +92,8 @@ public function getExpirationTimeForNormalNotification() { } /** - * Mark that a "password about to expire" notification has been sent + * Mark that a "password about to expire" notification has been sent. + * Note that we're using the id of the passInfo as marker, but this might change * @param OldPassword $passInfo the information about the password. It has * to include the userid owning the password and an id for the password */ @@ -101,7 +102,8 @@ public function markAboutToExpireNotificationSentFor(OldPassword $passInfo) { } /** - * Mark that a "password expired" notification has been sent + * Mark that a "password expired" notification has been sent. + * Note that we're using the id of the passInfo as marker, but this might change * @param OldPassword $passInfo the information about the password. It has * to include the userid owning the password and an id for the password */ @@ -109,6 +111,24 @@ public function markExpiredNotificationSentFor(OldPassword $passInfo) { $this->config->setUserValue($passInfo->getUid(), 'password_policy', 'expiredSent', $passInfo->getId()); } + /** + * Get the mark set with markAboutToExpireNotificationSentFor for the specified user + * @param string $userid the user id to get the mark from + * @return string|null the mark or null if there is no mark + */ + public function getMarkAboutToExpireNotificationSentFor($userid) { + return $this->config->getUserValue($userid, 'password_policy', 'aboutToExpireSent', null); + } + + /** + * Get the mark set with markExpiredNotificationSentFor for the specified user + * @param string $userid the user id to get the mark from + * @return string|null the mark or null if there is no mark + */ + public function getMarkExpiredNotificationSentFor($userid) { + return $this->config->getUserValue($userid, 'password_policy', 'expiredSent', null); + } + /** * Check if a "password about to expire" notification has been sent for that * password diff --git a/tests/Db/OldPasswordMapperTest.php b/tests/Db/OldPasswordMapperTest.php index 272f3bd5..58de6c7e 100644 --- a/tests/Db/OldPasswordMapperTest.php +++ b/tests/Db/OldPasswordMapperTest.php @@ -44,7 +44,6 @@ class OldPasswordMapperTest extends TestCase { private function resetDB() { $qb = $this->db->getQueryBuilder(); $qb->delete($this->mapper->getTableName()) - //->where($qb->expr()->eq('uid', $qb->createNamedParameter($this->testUID))); ->where($qb->expr()->in('uid', $qb->createNamedParameter($this->testUIDs, IQueryBuilder::PARAM_STR_ARRAY))); $qb->execute(); }