From 5bf3cb1b6347fa3fd2e4d5aba77dbec571a1d5eb Mon Sep 17 00:00:00 2001 From: Eelco Date: Tue, 12 Mar 2024 16:21:59 +0100 Subject: [PATCH 1/8] allow for setting middlewares via config and via Filament Resources --- config/api-service.php | 2 ++ routes/api.php | 7 +++++-- src/ApiService.php | 12 ++++++++---- src/ApiServicePlugin.php | 2 +- src/Concerns/HasMiddlewares.php | 16 ++++++++++++++++ 5 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 src/Concerns/HasMiddlewares.php diff --git a/config/api-service.php b/config/api-service.php index 9e446d6..2bb8243 100644 --- a/config/api-service.php +++ b/config/api-service.php @@ -21,6 +21,8 @@ ], 'route' => [ 'panel_prefix' => true, + 'use_resource_middlewares' => false, + 'middlewares' => [], ], 'tenancy' => [ 'enabled' => false, diff --git a/routes/api.php b/routes/api.php index 57b4c28..f45efc1 100644 --- a/routes/api.php +++ b/routes/api.php @@ -22,7 +22,10 @@ $tenantSlugAttribute = $panel->getTenantSlugAttribute(); $panelPrefix = ApiService::isRoutePrefixedByPanel() ? $panelPath ?? $panelId : ''; - $routeGroup = Route::name($panelPrefix . '.'); + $handlerMiddlewares = ApiService::getHandlerMiddlewares(); + + $routeGroup = Route::name($panelPrefix . '.') + ->middleware($handlerMiddlewares); if ( $hasTenancy && @@ -52,7 +55,7 @@ }); } } - if (! ApiService::tenancyAwareness()) { + if (!ApiService::tenancyAwareness()) { $routeGroup ->prefix($panelPrefix . '/') ->group(function () use ($panel, $apiServicePlugin) { diff --git a/src/ApiService.php b/src/ApiService.php index c9fdc08..8c70303 100644 --- a/src/ApiService.php +++ b/src/ApiService.php @@ -2,13 +2,16 @@ namespace Rupadana\ApiService; +use Filament\Panel; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Route; +use Rupadana\ApiService\Concerns\HasMiddlewares; use Rupadana\ApiService\Concerns\HasTenancy; class ApiService { use HasTenancy; + use HasMiddlewares; /** * Filament Resource @@ -32,7 +35,7 @@ public static function getResource() return static::$resource; } - public static function registerRoutes() + public static function registerRoutes(Panel $panel) { $slug = static::getResource()::getSlug(); @@ -41,9 +44,10 @@ public static function registerRoutes() ->replace('/', '.') ->append('.'); - Route::name( - $name - ) + $resourceRouteMiddlewares = ApiService::useResourceMiddlewares() ? static::getResource()::getRouteMiddleware($panel) : []; + + Route::name($name) + ->middleware($resourceRouteMiddlewares) ->prefix(static::$groupRouteName ?? $slug) ->group(function (Router $route) { static::handlers(); diff --git a/src/ApiServicePlugin.php b/src/ApiServicePlugin.php index 102098d..8158578 100644 --- a/src/ApiServicePlugin.php +++ b/src/ApiServicePlugin.php @@ -65,7 +65,7 @@ public function route(Panel $panel): void $apiServiceClass = $resource . '\\Api\\' . $resourceName . 'ApiService'; - app($apiServiceClass)->registerRoutes(); + app($apiServiceClass)->registerRoutes($panel); } catch (Exception $e) { } } diff --git a/src/Concerns/HasMiddlewares.php b/src/Concerns/HasMiddlewares.php new file mode 100644 index 0000000..81f8eab --- /dev/null +++ b/src/Concerns/HasMiddlewares.php @@ -0,0 +1,16 @@ + Date: Tue, 12 Mar 2024 16:33:46 +0100 Subject: [PATCH 2/8] added explanation for middlewares --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 4187324..b35f1c1 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ return [ ], 'route' => [ 'panel_prefix' => true, + 'use_resource_middlewares' => false, + 'middlewares' => [], ], 'tenancy' => [ 'enabled' => false, @@ -191,6 +193,25 @@ You can edit prefix & group route name as you want, default this plugin use mode } ``` +### Middlewares + +You can add or override middlewares at two specific places. Via the config and/or via the Resources $routeMiddleware. + +If you set `route.use_resource_middlewares` to true, the package will register the middlewares for that specific resource as defined in: + +```php +class BlogResource extends Resource + { + ... + protected static string | array $routeMiddleware = []; <-- your specific resource middlewares + ... + } +``` + +Then your API resource endpoint will go through these middlewares first. + +Another method of adding/overriding middlewares is via the config: `route.middlewares` you can add your middlewares as an array. These middelwares will be globally added to the API routes. + ### Tenancy When you want to enable Tenancy on this package you can enable this by setting the config `tenancy.enabled` to `true`. This makes sure that your api responses only retreive the data which that user has access to. So if you have configured 5 tenants and an user has access to 2 tenants. Then, enabling this feature will return only the data of those 2 tenants. From 2dad86485bfc03cf6d8ff0c64f6a5592d72da8d7 Mon Sep 17 00:00:00 2001 From: Eelco Date: Tue, 12 Mar 2024 16:34:35 +0100 Subject: [PATCH 3/8] small readme change --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b35f1c1..41194f7 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,7 @@ If you set `route.use_resource_middlewares` to true, the package will register t class BlogResource extends Resource { ... - protected static string | array $routeMiddleware = []; <-- your specific resource middlewares + protected static string | array $routeMiddleware = []; // <-- your specific resource middlewares ... } ``` From ab72d7d183ff54539fcbb9985ae24d7596ea4464 Mon Sep 17 00:00:00 2001 From: Eelco Date: Tue, 12 Mar 2024 17:47:11 +0100 Subject: [PATCH 4/8] refactored to middleware() in plugin initialization --- README.md | 15 +++++++++++++-- config/api-service.php | 1 - routes/api.php | 4 ++-- src/ApiService.php | 9 ++++++--- src/ApiServicePlugin.php | 22 ++++++++++++++++++++++ src/Concerns/HasMiddlewares.php | 16 ---------------- 6 files changed, 43 insertions(+), 24 deletions(-) delete mode 100644 src/Concerns/HasMiddlewares.php diff --git a/README.md b/README.md index 41194f7..4b94b2e 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,7 @@ You can edit prefix & group route name as you want, default this plugin use mode ### Middlewares -You can add or override middlewares at two specific places. Via the config and/or via the Resources $routeMiddleware. +You can add or override middlewares at two specific places. Via the Filament Panel Provider and/or via the Resources $routeMiddleware. If you set `route.use_resource_middlewares` to true, the package will register the middlewares for that specific resource as defined in: @@ -210,7 +210,18 @@ class BlogResource extends Resource Then your API resource endpoint will go through these middlewares first. -Another method of adding/overriding middlewares is via the config: `route.middlewares` you can add your middlewares as an array. These middelwares will be globally added to the API routes. +Another method of adding/overriding middlewares is via the initialization of the plugin in your Panel Provider by adding the `middleware()` method like so: + +```php +use Rupadana\ApiService\ApiServicePlugin; + +$panel->plugins([ + ApiServicePlugin::make() + ->middleware([ + // ... add your middlewares + ]) +]) +``` ### Tenancy diff --git a/config/api-service.php b/config/api-service.php index 2bb8243..29bbd69 100644 --- a/config/api-service.php +++ b/config/api-service.php @@ -22,7 +22,6 @@ 'route' => [ 'panel_prefix' => true, 'use_resource_middlewares' => false, - 'middlewares' => [], ], 'tenancy' => [ 'enabled' => false, diff --git a/routes/api.php b/routes/api.php index f45efc1..532bdca 100644 --- a/routes/api.php +++ b/routes/api.php @@ -22,10 +22,10 @@ $tenantSlugAttribute = $panel->getTenantSlugAttribute(); $panelPrefix = ApiService::isRoutePrefixedByPanel() ? $panelPath ?? $panelId : ''; - $handlerMiddlewares = ApiService::getHandlerMiddlewares(); + $middlewares = $apiServicePlugin->getMiddlewares(); $routeGroup = Route::name($panelPrefix . '.') - ->middleware($handlerMiddlewares); + ->middleware($middlewares); if ( $hasTenancy && diff --git a/src/ApiService.php b/src/ApiService.php index 8c70303..acca0a8 100644 --- a/src/ApiService.php +++ b/src/ApiService.php @@ -5,13 +5,11 @@ use Filament\Panel; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Route; -use Rupadana\ApiService\Concerns\HasMiddlewares; use Rupadana\ApiService\Concerns\HasTenancy; class ApiService { use HasTenancy; - use HasMiddlewares; /** * Filament Resource @@ -44,7 +42,7 @@ public static function registerRoutes(Panel $panel) ->replace('/', '.') ->append('.'); - $resourceRouteMiddlewares = ApiService::useResourceMiddlewares() ? static::getResource()::getRouteMiddleware($panel) : []; + $resourceRouteMiddlewares = static::useResourceMiddlewares() ? static::getResource()::getRouteMiddleware($panel) : []; Route::name($name) ->middleware($resourceRouteMiddlewares) @@ -67,4 +65,9 @@ public static function isRoutePrefixedByPanel(): bool { return config('api-service.route.panel_prefix', true); } + + public static function useResourceMiddlewares(): bool + { + return config('api-service.route.use_resource_middlewares', false); + } } diff --git a/src/ApiServicePlugin.php b/src/ApiServicePlugin.php index 8158578..1310ed2 100644 --- a/src/ApiServicePlugin.php +++ b/src/ApiServicePlugin.php @@ -12,6 +12,11 @@ class ApiServicePlugin implements Plugin { use CanManipulateFiles; + /** + * @var array + */ + protected array $middleware = []; + public function getId(): string { return 'api-service'; @@ -83,4 +88,21 @@ public static function get(): static return $plugin; } + + /** + * @param array $middleware + */ + public function middleware(array $middleware): static + { + $this->middleware = [ + ...$this->middleware, + ...$middleware, + ]; + return $this; + } + + public function getMiddlewares(): array + { + return $this->middleware; + } } diff --git a/src/Concerns/HasMiddlewares.php b/src/Concerns/HasMiddlewares.php deleted file mode 100644 index 81f8eab..0000000 --- a/src/Concerns/HasMiddlewares.php +++ /dev/null @@ -1,16 +0,0 @@ - Date: Tue, 12 Mar 2024 17:49:58 +0100 Subject: [PATCH 5/8] removed middlewares array in example config --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 4b94b2e..32bd49b 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,6 @@ return [ 'route' => [ 'panel_prefix' => true, 'use_resource_middlewares' => false, - 'middlewares' => [], ], 'tenancy' => [ 'enabled' => false, From c8eebce3f9ba69eb0b91c5579ed48ecada28cb23 Mon Sep 17 00:00:00 2001 From: Eelco Date: Wed, 13 Mar 2024 16:25:10 +0100 Subject: [PATCH 6/8] duplicate trait use and allowedIncludes() in paginationHandler --- src/Http/Handlers.php | 1 - stubs/PaginationHandler.stub | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Http/Handlers.php b/src/Http/Handlers.php index 36f80b6..3cdd2f1 100644 --- a/src/Http/Handlers.php +++ b/src/Http/Handlers.php @@ -11,7 +11,6 @@ class Handlers { - use HasHandlerTenantScope; use HttpResponse; use HasHandlerTenantScope; diff --git a/stubs/PaginationHandler.stub b/stubs/PaginationHandler.stub index db9fa8b..835e5d9 100644 --- a/stubs/PaginationHandler.stub +++ b/stubs/PaginationHandler.stub @@ -19,9 +19,10 @@ class PaginationHandler extends Handlers { ->allowedFields($model::$allowedFields ?? []) ->allowedSorts($model::$allowedSorts ?? []) ->allowedFilters($model::$allowedFilters ?? []) + ->allowedIncludes($model::$allowedIncludes ?? null) ->paginate(request()->query('per_page')) ->appends(request()->query()); return static::getApiTransformer()::collection($query); } -} \ No newline at end of file +} From b14d4e162b2b1bfc30ea47d92aa4bb053c3e9ad2 Mon Sep 17 00:00:00 2001 From: I Wayan Rupadana Date: Sat, 16 Mar 2024 07:11:00 +0800 Subject: [PATCH 7/8] fix: missing line --- routes/api.php | 6 ++---- src/ApiServicePlugin.php | 3 ++- src/Http/Handlers.php | 1 - 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/routes/api.php b/routes/api.php index 9810ff8..7488aa4 100644 --- a/routes/api.php +++ b/routes/api.php @@ -20,7 +20,7 @@ $hasTenancy = $panel->hasTenancy(); $tenantRoutePrefix = $panel->getTenantRoutePrefix(); $tenantSlugAttribute = $panel->getTenantSlugAttribute(); - + $apiServicePlugin = $panel->getPlugin('api-service'); $middlewares = $apiServicePlugin->getMiddlewares(); $panelRoutePrefix = ApiService::isRoutePrefixedByPanel() ? '{panel}' : ''; $panelNamePrefix = $panelRoutePrefix ? $panel->getId() . '.' : ''; @@ -33,13 +33,11 @@ Route::prefix($panelRoutePrefix . '/' . (($tenantRoutePrefix) ? "{$tenantRoutePrefix}/" : '') . '{tenant' . (($tenantSlugAttribute) ? ":{$tenantSlugAttribute}" : '') . '}') ->name($panelNamePrefix) ->middleware($middlewares) - ->group(function () use ($panel) { - $apiServicePlugin = $panel->getPlugin('api-service'); + ->group(function () use ($panel, $apiServicePlugin) { $apiServicePlugin->route($panel); }); } - if (! ApiService::tenancyAwareness()) { Route::prefix($panelRoutePrefix) ->name($panelNamePrefix) diff --git a/src/ApiServicePlugin.php b/src/ApiServicePlugin.php index 1310ed2..b168998 100644 --- a/src/ApiServicePlugin.php +++ b/src/ApiServicePlugin.php @@ -90,7 +90,7 @@ public static function get(): static } /** - * @param array $middleware + * @param array $middleware */ public function middleware(array $middleware): static { @@ -98,6 +98,7 @@ public function middleware(array $middleware): static ...$this->middleware, ...$middleware, ]; + return $this; } diff --git a/src/Http/Handlers.php b/src/Http/Handlers.php index 869b200..d721b67 100644 --- a/src/Http/Handlers.php +++ b/src/Http/Handlers.php @@ -8,7 +8,6 @@ use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Routing\Router; use Rupadana\ApiService\ApiService; -use Rupadana\ApiService\Traits\HasHandlerTenantScope; use Rupadana\ApiService\Traits\HttpResponse; use Rupadana\ApiService\Transformers\DefaultTransformer; From 3c49f940c4a89209d02d25fcfe056f6ee48029e9 Mon Sep 17 00:00:00 2001 From: I Wayan Rupadana Date: Sat, 16 Mar 2024 09:17:36 +0800 Subject: [PATCH 8/8] add notification release --- .github/workflows/discord-release-notification.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/workflows/discord-release-notification.yml diff --git a/.github/workflows/discord-release-notification.yml b/.github/workflows/discord-release-notification.yml new file mode 100644 index 0000000..1c18b84 --- /dev/null +++ b/.github/workflows/discord-release-notification.yml @@ -0,0 +1,13 @@ +on: + release: + types: [released] + +jobs: + message: + runs-on: ubuntu-latest + steps: + - name: Discord Webhook Action + uses: tsickert/discord-webhook@v5.3.0 + with: + webhook-url: ${{ secrets.WEBHOOK_URL }} + content: "# Released Version ${{ github.event.release.name }} \n\n\n${{ github.event.release.body }}" \ No newline at end of file