|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Facades\Artisan; |
| 7 | +use Illuminate\Support\Facades\DB; |
| 8 | +use Carbon\Carbon; |
| 9 | + |
| 10 | +class RevokeOauthAccessTokens extends Command |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The name and signature of the console command. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $signature = 'processmaker:revoke-oauth-access-tokens {--name=} {--after=} {--client_id=} {--no-interaction|n}'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Revoke oauth access tokens for a given token name, client_id, or after a given date'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Execute the console command. |
| 28 | + * |
| 29 | + * @return mixed |
| 30 | + */ |
| 31 | + public function handle() |
| 32 | + { |
| 33 | + $name = $this->option('name'); |
| 34 | + $after = $this->option('after'); |
| 35 | + $clientId = $this->option('client_id'); |
| 36 | + $noInteraction = $this->option('no-interaction'); |
| 37 | + |
| 38 | + if (!$name && !$after && !$clientId) { |
| 39 | + $this->error('At least one of --name, --after, or --client_id must be specified.'); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (!$noInteraction && !$this->confirm('Are you sure you want to revoke this certificate?')) { |
| 44 | + $this->info('Certificate revocation cancelled.'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $query = DB::table('oauth_access_tokens')->where('revoked', false); |
| 49 | + |
| 50 | + if ($name) { |
| 51 | + $names = explode(',', $name); |
| 52 | + $query->whereIn('name', $names); |
| 53 | + } |
| 54 | + |
| 55 | + if ($after) { |
| 56 | + $date = Carbon::createFromFormat('Y-m-d', $after); |
| 57 | + $query->where('created_at', '>', $date); |
| 58 | + } |
| 59 | + |
| 60 | + if ($clientId) { |
| 61 | + $clientIds = explode(',', $clientId); |
| 62 | + $query->whereIn('client_id', $clientIds); |
| 63 | + } |
| 64 | + |
| 65 | + $revokedCount = $query->update(['revoked' => true]); |
| 66 | + |
| 67 | + $this->info("Revoked $revokedCount tokens."); |
| 68 | + } |
| 69 | +} |
0 commit comments