Skip to content

Commit

Permalink
[12.x] Enhance console commands (#1724)
Browse files Browse the repository at this point in the history
* make migration and creating clients optional

* use components on cli

* fix tests

* Update InstallCommand.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
hafezdivandari and taylorotwell authored Mar 7, 2024
1 parent e063df3 commit b6133a7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/Console/ClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function createPersonalClient(ClientRepository $clients)
null, $name, 'http://localhost'
);

$this->info('Personal access client created successfully.');
$this->components->info('Personal access client created successfully.');

$this->outputClientDetails($client);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ protected function createPasswordClient(ClientRepository $clients)
null, $name, 'http://localhost', $provider
);

$this->info('Password grant client created successfully.');
$this->components->info('Password grant client created successfully.');

$this->outputClientDetails($client);
}
Expand All @@ -119,7 +119,7 @@ protected function createClientCredentialsClient(ClientRepository $clients)
null, $name, ''
);

$this->info('New client created successfully.');
$this->components->info('New client created successfully.');

$this->outputClientDetails($client);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ protected function createAuthCodeClient(ClientRepository $clients)
$userId, $name, $redirect, null, false, false, ! $this->option('public')
);

$this->info('New client created successfully.');
$this->components->info('New client created successfully.');

$this->outputClientDetails($client);
}
Expand All @@ -167,7 +167,7 @@ protected function outputClientDetails(Client $client)
$this->line('');
}

$this->line('<comment>Client ID:</comment> '.$client->getKey());
$this->line('<comment>Client secret:</comment> '.$client->plainSecret);
$this->components->twoColumnDetail('<comment>Client ID</comment>', $client->getKey());
$this->components->twoColumnDetail('<comment>Client secret</comment>', $client->plainSecret);
}
}
4 changes: 2 additions & 2 deletions src/Console/HashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class HashCommand extends Command
public function handle()
{
if (! Passport::$hashesClientSecrets) {
$this->warn('Please enable client hashing yet in your AppServiceProvider before continuing.');
$this->components->warn('Please enable client hashing yet in your AppServiceProvider before continuing.');

return;
}
Expand All @@ -49,7 +49,7 @@ public function handle()
])->save();
}

$this->info('All client secrets were successfully hashed.');
$this->components->info('All client secrets were successfully hashed.');
}
}
}
14 changes: 9 additions & 5 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class InstallCommand extends Command
*/
public function handle()
{
$provider = in_array('users', array_keys(config('auth.providers'))) ? 'users' : null;

$this->call('passport:keys', ['--force' => $this->option('force'), '--length' => $this->option('length')]);

$this->call('vendor:publish', ['--tag' => 'passport-migrations']);
Expand All @@ -41,10 +39,16 @@ public function handle()
$this->configureUuids();
}

$this->call('migrate');
if ($this->confirm('Would you like to run all pending database migrations?', true)) {
$this->call('migrate');

if ($this->confirm('Would you like to create the "personal access" and "password grant" clients?', true)) {
$provider = in_array('users', array_keys(config('auth.providers'))) ? 'users' : null;

$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client', '--provider' => $provider]);
$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client', '--provider' => $provider]);
}
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Console/KeysCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function handle()
];

if ((file_exists($publicKey) || file_exists($privateKey)) && ! $this->option('force')) {
$this->error('Encryption keys already exist. Use the --force option to overwrite them.');
$this->components->error('Encryption keys already exist. Use the --force option to overwrite them.');

return 1;
} else {
Expand All @@ -60,7 +60,7 @@ public function handle()
chmod($privateKey, 0600);
}

$this->info('Encryption keys generated successfully.');
$this->components->info('Encryption keys generated successfully.');
}

return 0;
Expand Down
10 changes: 5 additions & 5 deletions src/Console/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ public function handle()
Passport::refreshToken()->where('revoked', 1)->orWhereDate('expires_at', '<', $expired)->delete();

$this->option('hours')
? $this->info('Purged revoked items and items expired for more than '.$this->option('hours').' hours.')
: $this->info('Purged revoked items and items expired for more than seven days.');
? $this->components->info('Purged revoked items and items expired for more than '.$this->option('hours').' hours.')
: $this->components->info('Purged revoked items and items expired for more than seven days.');
} elseif ($this->option('revoked')) {
Passport::token()->where('revoked', 1)->delete();
Passport::authCode()->where('revoked', 1)->delete();
Passport::refreshToken()->where('revoked', 1)->delete();

$this->info('Purged revoked items.');
$this->components->info('Purged revoked items.');
} elseif ($this->option('expired')) {
Passport::token()->whereDate('expires_at', '<', $expired)->delete();
Passport::authCode()->whereDate('expires_at', '<', $expired)->delete();
Passport::refreshToken()->whereDate('expires_at', '<', $expired)->delete();

$this->option('hours')
? $this->info('Purged items expired for more than '.$this->option('hours').' hours.')
: $this->info('Purged items expired for more than seven days.');
? $this->components->info('Purged items expired for more than '.$this->option('hours').' hours.')
: $this->components->info('Purged items expired for more than seven days.');
}
}
}
2 changes: 1 addition & 1 deletion tests/Feature/KeysCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public function testPrivateAndPublicKeysShouldNotBeGeneratedTwice()
{
$this->artisan('passport:keys')
->assertFailed()
->expectsOutput('Encryption keys already exist. Use the --force option to overwrite them.');
->expectsOutputToContain('Encryption keys already exist. Use the --force option to overwrite them.');
}
}

0 comments on commit b6133a7

Please sign in to comment.