| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Laravel\Sanctum\Console\Commands;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Console\Command;
|
|
|
6 |
use Laravel\Sanctum\Sanctum;
|
|
|
7 |
|
|
|
8 |
class PruneExpired extends Command
|
|
|
9 |
{
|
|
|
10 |
/**
|
|
|
11 |
* The name and signature of the console command.
|
|
|
12 |
*
|
|
|
13 |
* @var string
|
|
|
14 |
*/
|
|
|
15 |
protected $signature = 'sanctum:prune-expired {--hours=24 : The number of hours to retain expired Sanctum tokens}';
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* The console command description.
|
|
|
19 |
*
|
|
|
20 |
* @var string
|
|
|
21 |
*/
|
|
|
22 |
protected $description = 'Prune tokens expired for more than specified number of hours';
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Execute the console command.
|
|
|
26 |
*
|
|
|
27 |
* @return int
|
|
|
28 |
*/
|
|
|
29 |
public function handle()
|
|
|
30 |
{
|
|
|
31 |
$model = Sanctum::$personalAccessTokenModel;
|
|
|
32 |
|
|
|
33 |
$hours = $this->option('hours');
|
|
|
34 |
|
|
|
35 |
$this->components->task(
|
|
|
36 |
'Pruning tokens with expired expires_at timestamps',
|
|
|
37 |
fn () => $model::where('expires_at', '<', now()->subHours($hours))->delete()
|
|
|
38 |
);
|
|
|
39 |
|
|
|
40 |
if ($expiration = config('sanctum.expiration')) {
|
|
|
41 |
$this->components->task(
|
|
|
42 |
'Pruning tokens with expired expiration value based on configuration file',
|
|
|
43 |
fn () => $model::where('created_at', '<', now()->subMinutes($expiration + ($hours * 60)))->delete()
|
|
|
44 |
);
|
|
|
45 |
} else {
|
|
|
46 |
$this->components->warn('Expiration value not specified in configuration file.');
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$this->components->info("Tokens expired for more than [$hours hours] pruned successfully.");
|
|
|
50 |
|
|
|
51 |
return 0;
|
|
|
52 |
}
|
|
|
53 |
}
|