Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Commit

Permalink
Allow consumer of library to overwrite default validty mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Duijnhoven committed May 3, 2019
1 parent 654cd5f commit 3148f2a
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@
]
];
```

## Configuration

You can tweak the setting found described in `vendor/jouwweb/jw-persistent-user/config/jwpersistentuser.local.php.dist`.
8 changes: 7 additions & 1 deletion config/jwpersistentuser.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
'jwpersistentuser' => [
'serieTokenEntityClass' => 'User\Model\SerieToken',
'tokenRefreshedAfterLogin' => true,
]
],
'service_manager' => [
'alias' => [
// Configure this library to use another strategy for writing serie validity
'JwPersistentUser\UserValidity' => 'Some\Custom\User\Validity\Writer',
],
],
];
4 changes: 4 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
'JwPersistentUser\ModuleOptions' => 'JwPersistentUser\Model\ModuleOptionsFactory',
'JwPersistentUser\Service\RememberMe' => 'JwPersistentUser\Service\RememberMeServiceFactory',
'JwPersistentUser\Service\Cookie' => 'JwPersistentUser\Service\CookieServiceFactory',
'JwPersistentUser\Service\UserAlwaysValid' => \Zend\ServiceManager\Factory\InvokableFactory::class,
],
'alias' => [
'JwPersistentUser\UserValidity' => 'JwPersistentUser\Service\UserAlwaysValid',
],
],
];
24 changes: 24 additions & 0 deletions src/JwPersistentUser/Service/RememberMeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class RememberMeService
*/
protected $moduleOptions;

/**
* @var UserValidityInterface
*/
protected $userValidityInterface;

/**
* @param int $userId
* @return SerieTokenInterface
Expand All @@ -34,6 +39,7 @@ public function createNew($userId)
$serieToken->setSerie($this->generateRandom());
$serieToken->setToken($this->generateRandom());
$serieToken->setExpiresAt($this->getNewExpireDate());
$serieToken->setValidUntil($this->getUserValidityInterface()->getValidUntilForUser($userId));

$this->getMapper()->persist($serieToken);

Expand Down Expand Up @@ -135,4 +141,22 @@ public function setModuleOptions($moduleOptions)
$this->moduleOptions = $moduleOptions;
return $this;
}

/**
* @return UserValidityInterface
*/
public function getUserValidityInterface()
{
return $this->userValidityInterface;
}

/**
* @param UserValidityInterface $userValidityInterface
* @return $this
*/
public function setUserValidityInterface($userValidityInterface)
{
$this->userValidityInterface = $userValidityInterface;
return $this;
}
}
1 change: 1 addition & 0 deletions src/JwPersistentUser/Service/RememberMeServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o

$service->setModuleOptions($container->get('JwPersistentUser\ModuleOptions'));
$service->setMapper($container->get('JwPersistentUser\Mapper\SerieToken'));
$service->setUserValidityInterface($container->get('JwPersistentUser\UserValidity'));

return $service;
}
Expand Down
11 changes: 11 additions & 0 deletions src/JwPersistentUser/Service/UserAlwaysValid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace JwPersistentUser\Service;

class UserAlwaysValid implements UserValidityInterface
{
public function getValidUntilForUser($userId)
{
return null;
}
}
8 changes: 8 additions & 0 deletions src/JwPersistentUser/Service/UserValidityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace JwPersistentUser\Service;

interface UserValidityInterface
{
public function getValidUntilForUser($userId);
}
42 changes: 40 additions & 2 deletions tests/src/JwPersistentUserTest/Service/RememberMeServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use JwPersistentUser\Model\ModuleOptions;
use JwPersistentUser\Service\RememberMeService;
use JwPersistentUser\Mapper\SerieTokenMapperInterface;
use JwPersistentUser\Service\UserValidityInterface;

class RememberMeServiceTest extends TestCase
{
Expand All @@ -16,15 +17,20 @@ class RememberMeServiceTest extends TestCase
protected $service;

/**
* @var SerieTokenMapperInterface
* @var SerieTokenMapperInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $mapper;

/**
* @var ModuleOptions
* @var ModuleOptions|\PHPUnit_Framework_MockObject_MockObject
*/
protected $options;

/**
* @var UserValidityInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $userValidityInterface;

public function setUp()
{
parent::setUp();
Expand All @@ -34,6 +40,9 @@ public function setUp()
$this->mapper = $this->getMock('JwPersistentUser\Mapper\SerieTokenMapperInterface');
$this->service->setMapper($this->mapper);

$this->userValidityInterface = $this->getMock(UserValidityInterface::class);
$this->service->setUserValidityInterface($this->userValidityInterface);

$this->service->setModuleOptions($this->options = new ModuleOptions);
$this->options->setSerieTokenEntityClass('JwPersistentUser\Model\SerieToken');
}
Expand All @@ -43,11 +52,40 @@ public function testCreateNew()
$this->mapper->expects($this->once())
->method('persist');

$this
->userValidityInterface
->expects($this->any())
->method('getValidUntilForUser')
->with(3)
->willReturn(null);

$response = $this->service->createNew(3);

$this->assertEquals(3, $response->getUserId());
$this->assertEquals(10, strlen($response->getSerie()));
$this->assertEquals(10, strlen($response->getToken()));
$this->assertNull($response->getValidUntil());
}

public function testCreateNewWithValidity()
{
$this->mapper->expects($this->once())
->method('persist');

$validUntil = new \DateTime('tomorrow');
$this
->userValidityInterface
->expects($this->any())
->method('getValidUntilForUser')
->with(3)
->willReturn($validUntil);

$response = $this->service->createNew(3);

$this->assertEquals(3, $response->getUserId());
$this->assertEquals(10, strlen($response->getSerie()));
$this->assertEquals(10, strlen($response->getToken()));
$this->assertEquals($validUntil, $response->getValidUntil());
}

public function testRemoveSerie()
Expand Down

0 comments on commit 3148f2a

Please sign in to comment.