diff --git a/CHANGELOG.md b/CHANGELOG.md index 1317111..f3b3ec0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Chg #58: Raise the minimum version of PHP to 8.0 and did refactoring using the features of it (@xepozz, @rustamwin) - Chg #58: Raise version of `yiisoft/access` to `^2.0` (@rustamwin) - Enh #83: Allow to create a session cookie via `CookieLogin` (@rustamwin) +- New #86: Add optional parameter `$duration` to `CookieLogin::addCookie()` (@vjik) ## 2.0.0 February 15, 2023 diff --git a/README.md b/README.md index 4c8528e..71a04bb 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ The package handles user-related functionality: The package could be installed with composer: ```shell -composer require yiisoft/user --prefer-dist +composer require yiisoft/user ``` ## General usage diff --git a/src/Login/Cookie/CookieLogin.php b/src/Login/Cookie/CookieLogin.php index 830e43f..3dd1563 100644 --- a/src/Login/Cookie/CookieLogin.php +++ b/src/Login/Cookie/CookieLogin.php @@ -48,17 +48,25 @@ public function withCookieName(string $name): self * * @param CookieLoginIdentityInterface $identity The cookie login identity instance. * @param ResponseInterface $response Response for adding auto-login cookie. + * @param DateInterval|false|null $duration Interval until the auto-login cookie expires. If it is null it means + * the auto-login cookie is session cookie that expires when browser is closed. If it is false (by default) will be + * used default value of duration. * * @throws JsonException If an error occurs during JSON encoding of the cookie value. * * @return ResponseInterface Response with added auto-login cookie. */ - public function addCookie(CookieLoginIdentityInterface $identity, ResponseInterface $response): ResponseInterface - { + public function addCookie( + CookieLoginIdentityInterface $identity, + ResponseInterface $response, + DateInterval|null|false $duration = false, + ): ResponseInterface { + $duration = $duration === false ? $this->duration : $duration; + $data = [$identity->getId(), $identity->getCookieLoginKey()]; - if ($this->duration !== null) { - $expires = (new DateTimeImmutable())->add($this->duration); + if ($duration !== null) { + $expires = (new DateTimeImmutable())->add($duration); $data[] = $expires->getTimestamp(); } else { $expires = null; diff --git a/tests/Login/Cookie/CookieLoginTest.php b/tests/Login/Cookie/CookieLoginTest.php index 2c09e60..dec8e8e 100644 --- a/tests/Login/Cookie/CookieLoginTest.php +++ b/tests/Login/Cookie/CookieLoginTest.php @@ -87,4 +87,40 @@ public function testRemoveCookieWithCustomName(): void $response->getHeaderLine('Set-Cookie'), ); } + + public function dataAddCookieWithCustomDuration(): array + { + return [ + 'false' => [ + '#testName=%5B%2242%22%2C%22auto-login-key-correct%22%2C[0-9]{10}%5D; Expires=.*?; Max-Age=604800; Path=/; Secure; HttpOnly; SameSite=Lax#', + false, + ], + 'null' => [ + '#testName=%5B%2242%22%2C%22auto-login-key-correct%22%2C0%5D; Path=/; Secure; HttpOnly; SameSite=Lax#', + null, + ], + 'p1d' => [ + '#testName=%5B%2242%22%2C%22auto-login-key-correct%22%2C[0-9]{10}%5D; Expires=.*?; Max-Age=86400; Path=/; Secure; HttpOnly; SameSite=Lax#', + new DateInterval('P1D'), + ], + ]; + } + + /** + * @dataProvider dataAddCookieWithCustomDuration + */ + public function testAddCookieWithCustomDuration(string $expectedRegExp, DateInterval|null|false $duration): void + { + $cookieLogin = (new CookieLogin(new DateInterval('P1W')))->withCookieName('testName'); + + $identity = new CookieLoginIdentity(); + + $response = new Response(); + $response = $cookieLogin->addCookie($identity, $response, $duration); + + $this->assertMatchesRegularExpression( + $expectedRegExp, + $response->getHeaderLine('Set-Cookie'), + ); + } }