Skip to content

Commit

Permalink
Remove the notification when the password change
Browse files Browse the repository at this point in the history
  • Loading branch information
jvillafanez committed Jul 11, 2018
1 parent dbf7af4 commit 82ff3cc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
33 changes: 31 additions & 2 deletions lib/HooksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -61,6 +62,9 @@ class HooksHandler {
/** @var ISession */
private $session;

/** @var IManager */
private $notificationManager;

/** @var UserNotificationConfigHandler */
private $userNotificationConfigHandler;

Expand All @@ -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;
Expand All @@ -83,6 +88,7 @@ public function __construct(
$this->passwordExpiredRule = $passwordExpiredRule;
$this->oldPasswordMapper = $oldPasswordMapper;
$this->session = $session;
$this->notificationManager = $notificationManager;
$this->userNotificationConfigHandler = $userNotificationConfigHandler;
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/Jobs/PasswordExpirationNotifierJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand Down
24 changes: 22 additions & 2 deletions lib/UserNotificationConfigHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -101,14 +102,33 @@ 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
*/
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
Expand Down
1 change: 0 additions & 1 deletion tests/Db/OldPasswordMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
5 changes: 5 additions & 0 deletions tests/HooksHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCP\ISession;
use OCP\IUser;
use OCP\Security\IHasher;
use OCP\Notification\IManager;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;

Expand All @@ -60,6 +61,8 @@ class HooksHandlerTest extends TestCase {
protected $handler;
/** @var UserNotificationConfigHandler | \PHPUnit_Framework_MockObject_MockObject */
protected $unConfigHandler;
/** @var IManager | \PHPUnit_Framework_MockObject_MockObject */
protected $manager;

protected function setUp() {
parent::setUp();
Expand All @@ -77,6 +80,7 @@ protected function setUp() {
$this->passwordExpiredRule = $this->createMock(PasswordExpired::class);
$this->oldPasswordMapper = $this->createMock(OldPasswordMapper::class);
$this->session = $this->createMock(ISession::class);
$this->manager = $this->createMock(IManager::class);
$this->unConfigHandler = $this->createMock(UserNotificationConfigHandler::class);

$this->handler = new HooksHandler(
Expand All @@ -88,6 +92,7 @@ protected function setUp() {
$this->passwordExpiredRule,
$this->oldPasswordMapper,
$this->session,
$this->manager,
$this->unConfigHandler
);
}
Expand Down

0 comments on commit 82ff3cc

Please sign in to comment.