mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 12:30:49 +08:00
ptgen api point support parameter & default user
This commit is contained in:
63
app/Console/Commands/DeleteExpiredToken.php
Normal file
63
app/Console/Commands/DeleteExpiredToken.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Laravel\Sanctum\PersonalAccessToken;
|
||||
|
||||
class DeleteExpiredToken extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'user:delete_expired_token {--uid=} {--days=}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Delete user expired token, options: --uid, --days';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$uid = $this->option('uid');
|
||||
$days = $this->option('days');
|
||||
if (!is_numeric($days)) {
|
||||
$days = 60;
|
||||
}
|
||||
$query = PersonalAccessToken::query()->where('tokenable_type', User::class);
|
||||
if ($uid) {
|
||||
$query->where('tokenable_id', $uid);
|
||||
}
|
||||
$log = sprintf('uid: %s, days: %s', $uid, $days);
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
|
||||
$query->where('last_used_at', '<', Carbon::now()->subDays($days));
|
||||
$result = $query->delete();
|
||||
$log = sprintf('[%s], %s, result: %s, query: %s', REQUEST_ID, __METHOD__, var_export($result, true), last_query());
|
||||
$this->info($log);
|
||||
do_log($log);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user