Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Auth/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,16 @@ public function id()
*/
public function once(array $credentials = [])
{
$this->useSession = false;
$previousUseSession = $this->useSession;

$user = $this->authenticate($credentials);
$this->useSession = false;

$this->useSession = true;
try {
$user = $this->authenticate($credentials);
}
finally {
$this->useSession = $previousUseSession;
}

return !!$user;
}
Expand Down
62 changes: 62 additions & 0 deletions tests/Auth/ManagerOnceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Winter\Storm\Tests\Auth;

use ReflectionProperty;
use RuntimeException;
use Winter\Storm\Auth\Manager as AuthManager;

class ManagerOnceTest extends \Winter\Storm\Tests\TestCase
{
public function tearDown(): void
{
ManagerOnceTestThrowingManager::forgetInstance();

parent::tearDown();
}

public function testOnceRestoresSessionUseWhenAuthenticationThrows()
{
$manager = ManagerOnceTestThrowingManager::instance();

$this->assertTrue($this->getUseSession($manager));

try {
$manager->once(['login' => 'someone', 'password' => 'secret']);
$this->fail('The exception from authenticate() should propagate.');
}
catch (RuntimeException $ex) {
// Expected.
}

$this->assertTrue($this->getUseSession($manager));
}

public function testOnceRestoresThePreviousValueRatherThanTrue()
{
$manager = ManagerOnceTestThrowingManager::instance();
(new ReflectionProperty($manager, 'useSession'))->setValue($manager, false);

try {
$manager->once(['login' => 'someone', 'password' => 'secret']);
}
catch (RuntimeException $ex) {
// Expected.
}

$this->assertFalse($this->getUseSession($manager));
}

protected function getUseSession(AuthManager $manager): bool
{
return (new ReflectionProperty($manager, 'useSession'))->getValue($manager);
}
}

class ManagerOnceTestThrowingManager extends AuthManager
{
public function authenticate(array $credentials, $remember = true)
{
throw new RuntimeException('authentication failed');
}
}
Loading