|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Console\Commands\Auth; |
| 6 | + |
| 7 | +use App\Mail\AuthApiTokenExpirationReminderMail; |
| 8 | +use App\Mail\AuthApiTokenExpiredMail; |
| 9 | +use App\Models\Passport\Token; |
| 10 | +use App\Models\User; |
| 11 | +use Illuminate\Console\Command; |
| 12 | +use Illuminate\Database\Eloquent\Builder; |
| 13 | +use Illuminate\Database\Eloquent\Collection; |
| 14 | +use Illuminate\Support\Carbon; |
| 15 | +use Illuminate\Support\Facades\Mail; |
| 16 | + |
| 17 | +class AuthSendReminderForExpiringApiTokensCommand extends Command |
| 18 | +{ |
| 19 | + /** |
| 20 | + * The name and signature of the console command. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $signature = 'auth:send-mails-expiring-api-tokens '. |
| 25 | + ' { --dry-run : Do not actually send emails or save anything to the database, just output what would happen }'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The console command description. |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + protected $description = 'Sends emails about expiring API tokens, one week before and when they expired.'; |
| 33 | + |
| 34 | + /** |
| 35 | + * Execute the console command. |
| 36 | + */ |
| 37 | + public function handle(): int |
| 38 | + { |
| 39 | + $dryRun = (bool) $this->option('dry-run'); |
| 40 | + if ($dryRun) { |
| 41 | + $this->comment('Running in dry-run mode. No emails will be sent and nothing will be saved to the database.'); |
| 42 | + } |
| 43 | + |
| 44 | + $this->comment('Sending reminder emails about expiring API tokens...'); |
| 45 | + $sentMails = 0; |
| 46 | + Token::query() |
| 47 | + ->where('expires_at', '<=', Carbon::now()->addDays(7)) |
| 48 | + ->whereNull('reminder_sent_at') |
| 49 | + ->with([ |
| 50 | + 'client', |
| 51 | + 'user', |
| 52 | + ]) |
| 53 | + ->whereHas('user', function (Builder $query): void { |
| 54 | + /** @var Builder<User> $query */ |
| 55 | + $query->where('is_placeholder', '=', false); |
| 56 | + }) |
| 57 | + ->isApiToken(true) |
| 58 | + ->orderBy('created_at', 'asc') |
| 59 | + ->chunk(500, function (Collection $tokens) use ($dryRun, &$sentMails): void { |
| 60 | + /** @var Collection<int, Token> $tokens */ |
| 61 | + foreach ($tokens as $token) { |
| 62 | + $user = $token->user; |
| 63 | + $this->info('Start sending email to user "'.$user->email.'" ('.$user->getKey().') reminding about API token '.$token->getKey()); |
| 64 | + $sentMails++; |
| 65 | + if (! $dryRun) { |
| 66 | + Mail::to($user->email) |
| 67 | + ->queue(new AuthApiTokenExpirationReminderMail($token, $user)); |
| 68 | + $token->reminder_sent_at = Carbon::now(); |
| 69 | + $token->save(); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + $this->comment('Finished sending '.$sentMails.' expiring API token emails...'); |
| 74 | + |
| 75 | + $this->comment('Sent emails about expired API tokens'); |
| 76 | + $sentMails = 0; |
| 77 | + Token::query() |
| 78 | + ->where('expires_at', '<=', Carbon::now()) |
| 79 | + ->whereNull('expired_info_sent_at') |
| 80 | + ->with([ |
| 81 | + 'client', |
| 82 | + 'user', |
| 83 | + ]) |
| 84 | + ->whereHas('user', function (Builder $query): void { |
| 85 | + /** @var Builder<User> $query */ |
| 86 | + $query->where('is_placeholder', '=', false); |
| 87 | + }) |
| 88 | + ->isApiToken(true) |
| 89 | + ->orderBy('created_at', 'asc') |
| 90 | + ->chunk(500, function (Collection $tokens) use ($dryRun, &$sentMails): void { |
| 91 | + /** @var Collection<int, Token> $tokens */ |
| 92 | + foreach ($tokens as $token) { |
| 93 | + $user = $token->user; |
| 94 | + $this->info('Start sending email to user "'.$user->email.'" ('.$user->getKey().') about expired API token '.$token->getKey()); |
| 95 | + $sentMails++; |
| 96 | + if (! $dryRun) { |
| 97 | + Mail::to($user->email) |
| 98 | + ->queue(new AuthApiTokenExpiredMail($token, $user)); |
| 99 | + $token->expired_info_sent_at = Carbon::now(); |
| 100 | + $token->save(); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + $this->comment('Finished sending '.$sentMails.' expired API token emails...'); |
| 105 | + |
| 106 | + return self::SUCCESS; |
| 107 | + } |
| 108 | +} |
0 commit comments