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

add blade directive hasanypermission #2610

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 16 additions & 14 deletions src/PermissionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function boot()
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../config/permission.php',
__DIR__ . '/../config/permission.php',
'permission'
);

Expand All @@ -52,21 +52,21 @@ public function register()

protected function offerPublishing(): void
{
if (! $this->app->runningInConsole()) {
if (!$this->app->runningInConsole()) {
return;
}

if (! function_exists('config_path')) {
if (!function_exists('config_path')) {
// function not available and 'publish' not relevant in Lumen
return;
}

$this->publishes([
__DIR__.'/../config/permission.php' => config_path('permission.php'),
__DIR__ . '/../config/permission.php' => config_path('permission.php'),
], 'permission-config');

$this->publishes([
__DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->getMigrationFileName('create_permission_tables.php'),
__DIR__ . '/../database/migrations/create_permission_tables.php.stub' => $this->getMigrationFileName('create_permission_tables.php'),
], 'permission-migrations');
}

Expand All @@ -76,7 +76,7 @@ protected function registerCommands(): void
Commands\CacheReset::class,
]);

if (! $this->app->runningInConsole()) {
if (!$this->app->runningInConsole()) {
return;
}

Expand All @@ -90,7 +90,7 @@ protected function registerCommands(): void

protected function registerOctaneListener(): void
{
if ($this->app->runningInConsole() || ! $this->app['config']->get('octane.listeners')) {
if ($this->app->runningInConsole() || !$this->app['config']->get('octane.listeners')) {
return;
}

Expand All @@ -101,7 +101,7 @@ protected function registerOctaneListener(): void
$event->sandbox->make(PermissionRegistrar::class)->setPermissionsTeamId(null);
});

if (! $this->app['config']->get('permission.register_octane_reset_listener')) {
if (!$this->app['config']->get('permission.register_octane_reset_listener')) {
return;
}
// @phpstan-ignore-next-line
Expand Down Expand Up @@ -133,6 +133,8 @@ protected function registerBladeExtensions($bladeCompiler): void
$bladeCompiler->directive('haspermission', fn ($args) => "<?php if({$bladeMethodWrapper}('checkPermissionTo', {$args})): ?>");
$bladeCompiler->directive('elsehaspermission', fn ($args) => "<?php elseif({$bladeMethodWrapper}('checkPermissionTo', {$args})): ?>");
$bladeCompiler->directive('endhaspermission', fn () => '<?php endif; ?>');
$bladeCompiler->directive('hasanypermission', fn ($args) => "<?php if({$bladeMethodWrapper}('hasAnyPermission', {$args})): ?>");
$bladeCompiler->directive('endhasanypermission', fn () => '<?php endif; ?>');

$bladeCompiler->directive('hasrole', fn ($args) => "<?php if({$bladeMethodWrapper}('hasRole', {$args})): ?>");
$bladeCompiler->directive('endhasrole', fn () => '<?php endif; ?>');
Expand All @@ -152,18 +154,18 @@ protected function registerBladeExtensions($bladeCompiler): void

protected function registerMacroHelpers(): void
{
if (! method_exists(Route::class, 'macro')) { // Lumen
if (!method_exists(Route::class, 'macro')) { // Lumen
return;
}

Route::macro('role', function ($roles = []) {
/** @var Route $this */
return $this->middleware('role:'.implode('|', Arr::wrap($roles)));
return $this->middleware('role:' . implode('|', Arr::wrap($roles)));
});

Route::macro('permission', function ($permissions = []) {
/** @var Route $this */
return $this->middleware('permission:'.implode('|', Arr::wrap($permissions)));
return $this->middleware('permission:' . implode('|', Arr::wrap($permissions)));
});
}

Expand All @@ -176,9 +178,9 @@ protected function getMigrationFileName(string $migrationFileName): string

$filesystem = $this->app->make(Filesystem::class);

return Collection::make([$this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR])
->flatMap(fn ($path) => $filesystem->glob($path.'*_'.$migrationFileName))
->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}")
return Collection::make([$this->app->databasePath() . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR])
->flatMap(fn ($path) => $filesystem->glob($path . '*_' . $migrationFileName))
->push($this->app->databasePath() . "/migrations/{$timestamp}_{$migrationFileName}")
->first();
}
}