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

[Fix] Let user specify (tenant aware) routes #9

Merged
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ Optionally, you can publish the views using
php artisan vendor:publish --tag="filament-mails-views"
```

Add the routes to your `web.php` file:

```php
use Vormkracht10\FilamentMails\Facades\FilamentMails;

FilamentMails::routes();
```

Then add the plugin to your `PanelProvider`

```php
Expand All @@ -73,6 +81,22 @@ public function panel(Panel $panel): Panel
}
```

### Tenant middleware / route protection

If you want to protect the mail routes with your (tenant) middleware, you can do so by adding the routes to the `tenantRoutes`:

```php
use Vormkracht10\FilamentMails\FilamentMailsPlugin;
use Vormkracht10\FilamentMails\Facades\FilamentMails;

public function panel(Panel $panel): Panel
{
return $panel
->plugin(FilamentMailsPlugin::make())
->tenantRoutes(fn() => FilamentMails::routes());
}
```

> [!IMPORTANT]
> For setting up the webhooks to register mail events, please look into the README of [Laravel Mails](https://github.com/vormkracht10/laravel-mails), the underlying package that powers this package.

Expand Down
8 changes: 7 additions & 1 deletion resources/views/mails/download.blade.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<a type="button" href="{{ route('mail.download', $getState()->id) }}"
<a type="button"
href="{{ route('filament.' . Filament\Facades\Filament::getCurrentPanel()->getId() . '.mails.attachment.download', [
'tenant' => Filament\Facades\Filament::getTenant(),
'mail' => $getState()->mail_id,
'attachment' => $getState()->id,
'filename' => $getState()->filename,
]) }}"
class="rounded-md bg-white px-3.5 py-2.5 text-sm font-semibold cursor-pointer text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50">Download</a>
5 changes: 3 additions & 2 deletions resources/views/mails/preview.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="w-full h-screen">
<iframe src="{{ route('mail.preview', ['mail' => $mail->id]) }}" class="w-full h-full max-w-full"
style="width: 100vw; height: 100vh; border: none;">
<iframe
src="{{ route('filament.' . Filament\Facades\Filament::getCurrentPanel()->getId() . '.mails.preview', ['tenant' => Filament\Facades\Filament::getTenant(), 'mail' => $mail->id]) }}"
class="w-full h-full max-w-full" style="width: 100vw; height: 100vh; border: none;">
</iframe>
</div>
8 changes: 0 additions & 8 deletions routes/web.php

This file was deleted.

15 changes: 10 additions & 5 deletions src/Controllers/MailDownloadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

namespace Vormkracht10\FilamentMails\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;
use Vormkracht10\Mails\Models\MailAttachment;

class MailDownloadController extends Controller
{
public function __invoke(Request $request)
public function __invoke(string $tenant, string $mail, string $attachment, string $filename)
{
/** @var MailAttachment $attachment */
$attachment = MailAttachment::find($request->attachment);
$attachment = MailAttachment::find($attachment);

$file = Storage::disk($attachment->disk)->get($attachment->uuid);
$file = Storage::disk($attachment->disk)->path($attachment->uuid);

return response()->download($file);
return response()->download(
file: $file,
name: $filename,
headers: [
'Content-Type' => $attachment->mime,
]
);
}
}
13 changes: 12 additions & 1 deletion src/FilamentMails.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@

namespace Vormkracht10\FilamentMails;

class FilamentMails {}
use Illuminate\Support\Facades\Route;
use Vormkracht10\FilamentMails\Controllers\MailDownloadController;
use Vormkracht10\FilamentMails\Controllers\MailPreviewController;

class FilamentMails
{
public static function routes()
{
Route::get('mails/{mail}/preview', MailPreviewController::class)->name('mails.preview');
Route::get('mails/{mail}/attachment/{attachment}/{filename}', MailDownloadController::class)->name('mails.attachment.download');
}
}
5 changes: 1 addition & 4 deletions src/FilamentMailsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public function configurePackage(Package $package): void
if (file_exists($package->basePath('/../resources/views'))) {
$package->hasViews(static::$viewNamespace);
}

$package->hasRoute('web');
}

public function packageRegistered(): void {}
Expand Down Expand Up @@ -113,8 +111,7 @@ protected function getAssets(): array
*/
protected function getCommands(): array
{
return [
];
return [];
}

/**
Expand Down
Loading