Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register about details #2584

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Guard
* Return a collection of guard names suitable for the $model,
* as indicated by the presence of a $guard_name property or a guardName() method on the model.
*
* @param string|Model $model model class object or name
* @param string|Model $model model class object or name
*/
public static function getNames($model): Collection
{
Expand Down Expand Up @@ -58,7 +58,7 @@ protected static function getConfigAuthGuards(string $class): Collection
/**
* Lookup a guard name relevant for the $class model and the current user.
*
* @param string|Model $class model class object or name
* @param string|Model $class model class object or name
* @return string guard name
*/
public static function getDefaultName($class): string
Expand Down
27 changes: 27 additions & 0 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Spatie\Permission;

use Composer\InstalledVersions;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Routing\Route;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -38,6 +40,8 @@ public function boot()
});

$this->app->singleton(PermissionRegistrar::class);

$this->registerAbout();
}

public function register()
Expand Down Expand Up @@ -181,4 +185,27 @@ protected function getMigrationFileName(string $migrationFileName): string
->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}")
->first();
}

protected function registerAbout(): void
{
if (! class_exists(InstalledVersions::class) || ! class_exists(AboutCommand::class)) {
return;
}

$features = [
'Teams' => 'teams',
'Wildcard-Permissions' => 'enable_wildcard_permission',
'Octane-Listener' => 'register_octane_reset_listener',
'Passport' => 'use_passport_client_credentials',
];

AboutCommand::add('Spatie Permissions', fn () => [
'Features Enabled' => collect($features)
->filter(fn (string $feature, string $name): bool => $this->app['config']->get("permission.{$feature}"))
->keys()
->whenEmpty(fn (Collection $collection) => $collection->push('Default'))
->join(', '),
'Version' => InstalledVersions::getPrettyVersion('spatie/laravel-permission'),
]);
}
}
18 changes: 18 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,22 @@ public function it_can_show_roles_by_teams()
$this->assertRegExp('/\|\s+\|\s+testRole\s+\|\s+testRole_2\s+\|\s+testRole_Team\s+\|\s+testRole_Team\s+\|/', $output);
}
}

/** @test */
public function it_can_respond_to_about_command()
{
config()->set('permission.teams', true);
app(\Spatie\Permission\PermissionRegistrar::class)->initializeCache();

Artisan::call('about');

$output = Artisan::output();

$pattern = '/Spatie Permissions[ .\n]*Features Enabled[ .]*Teams[ .\n]*Version/';
if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression($pattern, $output);
} else { // phpUnit 9/8
$this->assertRegExp($pattern, $output);
}
}
}