Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Update API to reflect changes to CLI interaction #213

Merged
merged 1 commit into from
Sep 26, 2024
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
2 changes: 1 addition & 1 deletion src/Extensions/QueuedJobDescriptorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class QueuedJobDescriptorExtension extends Extension
{
/**
* Called on dev/build by DatabaseAdmin
* Called by DbBuild
*/
protected function onAfterBuild(): void
{
Expand Down
25 changes: 8 additions & 17 deletions src/Tasks/GarbageCollectionTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,22 @@
namespace SilverStripe\Tasks;

use SilverStripe\Dev\BuildTask;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\SessionManager\Services\GarbageCollectionService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

class GarbageCollectionTask extends BuildTask
{
/**
* @var string
*/
private static $segment = 'LoginSessionGarbageCollectionTask';
protected static string $commandName = 'LoginSessionGarbageCollectionTask';

/**
* @var string
*/
protected $title = 'Login Session Garbage Collection Task';
protected string $title = 'Login Session Garbage Collection Task';

/**
* @var string
*/
protected $description = 'Removes expired login sessions and “remember me” hashes from the database';
protected static string $description = 'Removes expired login sessions and "remember me" hashes from the database';

/**
* @param HTTPRequest $request
*/
public function run($request)
protected function execute(InputInterface $input, PolyOutput $output): int
{
GarbageCollectionService::singleton()->collect();
echo "Garbage collection completed successfully\n";
return Command::SUCCESS;
}
}
24 changes: 9 additions & 15 deletions src/Tasks/InvalidateAllSessionsTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@

namespace SilverStripe\SessionManager\Tasks;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\BuildTask;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\Security\RememberLoginHash;
use SilverStripe\SessionManager\Models\LoginSession;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

class InvalidateAllSessionsTask extends BuildTask
{
private static string $segment = 'InvalidateAllSessions';
protected static string $commandName = 'InvalidateAllSessions';

/**
* @var string
*/
protected $title = 'Invalidate All Login Sessions Task';
protected string $title = 'Invalidate All Login Sessions Task';

/**
* @var string
*/
protected $description = 'Removes all login sessions and "remember me" hashes (including yours) from the database';
protected static string $description = 'Removes all login sessions and "remember me" hashes'
. ' (including yours) from the database';

/**
* @param HTTPRequest $request
*/
public function run($request)
protected function execute(InputInterface $input, PolyOutput $output): int
{
LoginSession::get()->removeAll();
RememberLoginHash::get()->removeAll();
echo "Session removal completed successfully\n";
return Command::SUCCESS;
}
}
9 changes: 8 additions & 1 deletion tests/php/Tasks/InvalidateAllSessionsTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\Control\Tests\HttpRequestMockBuilder;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Security\AuthenticationHandler;
use SilverStripe\Security\Member;
Expand All @@ -15,6 +16,8 @@
use SilverStripe\SessionManager\Middleware\LoginSessionMiddleware;
use SilverStripe\SessionManager\Models\LoginSession;
use SilverStripe\SessionManager\Tasks\InvalidateAllSessionsTask;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

class InvalidateAllSessionsTaskTest extends SapphireTest
{
Expand Down Expand Up @@ -46,7 +49,11 @@ public function testRemoveAllSessions()

// Completely invalidate all current sessions and login hashes
$task = new InvalidateAllSessionsTask();
$task->run($request);
$buffer = new BufferedOutput();
$output = new PolyOutput(PolyOutput::FORMAT_ANSI, wrappedOutput: $buffer);
$input = new ArrayInput([]);
$input->setInteractive(false);
$task->run($input, $output);

// Assert all sessions are removed from the database
$this->assertSame(0, LoginSession::get()->count(), 'There are no LoginSessions');
Expand Down
Loading