Skip to content

Commit

Permalink
Merge pull request #39 from rupadana/dev
Browse files Browse the repository at this point in the history
feat: Middleware API on ApiServicePlugin class
  • Loading branch information
rupadana authored Mar 16, 2024
2 parents 7ba4a19 + 3c49f94 commit fb4ab91
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 10 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/discord-release-notification.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
on:
release:
types: [released]

jobs:
message:
runs-on: ubuntu-latest
steps:
- name: Discord Webhook Action
uses: tsickert/[email protected]
with:
webhook-url: ${{ secrets.WEBHOOK_URL }}
content: "# Released Version ${{ github.event.release.name }} \n\n\n${{ github.event.release.body }}"
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ return [
],
'route' => [
'panel_prefix' => true,
'use_resource_middlewares' => false,
],
'tenancy' => [
'enabled' => false,
Expand Down Expand Up @@ -190,6 +191,36 @@ 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 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:

```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 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

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.
Expand Down
1 change: 1 addition & 0 deletions config/api-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
],
'route' => [
'panel_prefix' => true,
'use_resource_middlewares' => false,
],
'tenancy' => [
'enabled' => false,
Expand Down
7 changes: 5 additions & 2 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
$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() . '.' : '';

Expand All @@ -30,15 +32,16 @@
) {
Route::prefix($panelRoutePrefix . '/' . (($tenantRoutePrefix) ? "{$tenantRoutePrefix}/" : '') . '{tenant' . (($tenantSlugAttribute) ? ":{$tenantSlugAttribute}" : '') . '}')
->name($panelNamePrefix)
->group(function () use ($panel) {
$apiServicePlugin = $panel->getPlugin('api-service');
->middleware($middlewares)
->group(function () use ($panel, $apiServicePlugin) {
$apiServicePlugin->route($panel);
});
}

if (! ApiService::tenancyAwareness()) {
Route::prefix($panelRoutePrefix)
->name($panelNamePrefix)
->middleware($middlewares)
->group(function () use ($panel) {
$apiServicePlugin = $panel->getPlugin('api-service');
$apiServicePlugin->route($panel);
Expand Down
15 changes: 11 additions & 4 deletions src/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rupadana\ApiService;

use Filament\Panel;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
use Rupadana\ApiService\Concerns\HasTenancy;
Expand Down Expand Up @@ -32,7 +33,7 @@ public static function getResource()
return static::$resource;
}

public static function registerRoutes()
public static function registerRoutes(Panel $panel)
{

$slug = static::getResource()::getSlug();
Expand All @@ -41,9 +42,10 @@ public static function registerRoutes()
->replace('/', '.')
->append('.');

Route::name(
$name
)
$resourceRouteMiddlewares = static::useResourceMiddlewares() ? static::getResource()::getRouteMiddleware($panel) : [];

Route::name($name)
->middleware($resourceRouteMiddlewares)
->prefix(static::$groupRouteName ?? $slug)
->group(function (Router $route) {
static::handlers();
Expand All @@ -63,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);
}
}
25 changes: 24 additions & 1 deletion src/ApiServicePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class ApiServicePlugin implements Plugin
{
use CanManipulateFiles;

/**
* @var array<string>
*/
protected array $middleware = [];

public function getId(): string
{
return 'api-service';
Expand Down Expand Up @@ -65,7 +70,7 @@ public function route(Panel $panel): void

$apiServiceClass = $resource . '\\Api\\' . $resourceName . 'ApiService';

app($apiServiceClass)->registerRoutes();
app($apiServiceClass)->registerRoutes($panel);
} catch (Exception $e) {
}
}
Expand All @@ -83,4 +88,22 @@ public static function get(): static

return $plugin;
}

/**
* @param array<string> $middleware
*/
public function middleware(array $middleware): static
{
$this->middleware = [
...$this->middleware,
...$middleware,
];

return $this;
}

public function getMiddlewares(): array
{
return $this->middleware;
}
}
2 changes: 0 additions & 2 deletions src/Http/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
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;

class Handlers
{
use HasHandlerTenantScope;
use HttpResponse;
protected Panel $panel;
public static ?string $uri = '/';
Expand Down
3 changes: 2 additions & 1 deletion stubs/PaginationHandler.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit fb4ab91

Please sign in to comment.