mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 17:44:05 +00:00
Add and test command for deleteing expired impersonation tokens
This commit is contained in:
parent
2d5b6aa0c8
commit
f0b395ce23
3 changed files with 91 additions and 0 deletions
36
src/Commands/ClearExpiredImpersonationTokens.php
Normal file
36
src/Commands/ClearExpiredImpersonationTokens.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Stancl\Tenancy\Features\UserImpersonation;
|
||||
|
||||
/**
|
||||
* @see Stancl\Tenancy\Features\UserImpersonation
|
||||
*/
|
||||
class ClearExpiredImpersonationTokens extends Command
|
||||
{
|
||||
protected $signature = 'tenants:clear-expired-impersonation-tokens
|
||||
{--ttl= : TTL in seconds for impersonation tokens (default is UserImpersonation::$ttl)}';
|
||||
|
||||
protected $description = 'Remove expired impersonation tokens.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$this->components->info('Removing expired impersonation tokens.');
|
||||
|
||||
$ttl = (int) $this->option('ttl') ?: UserImpersonation::$ttl;
|
||||
$expirationDate = now()->subSeconds($ttl);
|
||||
|
||||
$impersonationTokenModel = UserImpersonation::modelClass();
|
||||
|
||||
$deletedTokenCount = $impersonationTokenModel::where('created_at', '<', $expirationDate)
|
||||
->delete();
|
||||
|
||||
$this->components->info($deletedTokenCount . ' expired impersonation ' . str('token')->plural($deletedTokenCount) . ' deleted.');
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -104,6 +104,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
Commands\ClearPendingTenants::class,
|
||||
Commands\CreatePendingTenants::class,
|
||||
Commands\CreateUserWithRLSPolicies::class,
|
||||
Commands\ClearExpiredImpersonationTokens::class,
|
||||
]);
|
||||
|
||||
if (static::$migrateFreshOverride) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue