Skip to content

Commit

Permalink
Reviewed and enhanched multi-tenancy support from pull request #55 by @…
Browse files Browse the repository at this point in the history
…nilede-bharat. Minor fixes and code review. Updated README and screenshots.
  • Loading branch information
marcogermani87 committed May 4, 2024
1 parent f7d4fb3 commit 1db185c
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ Register the plugin through your panel service provider:
## Configuration

```php
<?php

use RickDBCN\FilamentEmail\Filament\Resources\EmailResource;
use RickDBCN\FilamentEmail\Models\Email;

Expand Down Expand Up @@ -79,8 +77,10 @@ return [
'can_access' => [
'role' => [],
],
];

//Use this option for customize tenant model class
//'tenant_model' => \App\Models\Team::class,
];
```

## Testing
Expand Down
4 changes: 3 additions & 1 deletion config/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@
'can_access' => [
'role' => [],
],
// 'tenant_model' => \App\Models\Team::class, 'pass model of relation to emails like team has many emails'

//Use this option for customize tenant model class
//'tenant_model' => \App\Models\Team::class,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::table('filament_email_log', function (Blueprint $table) {
$table->after('id', function (Blueprint $table) {
$table->foreignId('team_id')->nullable();
});
});
}

public function down()
{
Schema::table('filament_email_log', function (Blueprint $table) {
$table->dropColumn('team_id');
});
}
};
1 change: 0 additions & 1 deletion database/migrations/create_filament_email_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ return new class extends Migration
{
Schema::create('filament_email_log', function (Blueprint $table) {
$table->id();
$table->foreignId('tenant_id')->nullable();
$table->string('from')->nullable();
$table->string('to')->nullable();
$table->string('cc')->nullable();
Expand Down
Binary file modified screenshots/filters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/resend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/update-and-resend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/view.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Filament/Resources/EmailResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public static function table(Table $table): Table
->onColor('success')
->offColor('danger')
->inline(false)
->disabled(fn ($record): bool => empty($record->attachments))
->default(fn ($record): bool => ! empty($record->attachments))
->required(),
])
Expand Down
1 change: 1 addition & 0 deletions src/FilamentEmailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function configurePackage(Package $package): void
->hasMigrations([
'create_filament_email_table',
'add_attachments_field_to_filament_email_log_table',
'add_team_id_field_to_filament_email_log_table',
]);

$this->app->register(EmailMessageServiceProvider::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Listeners/FilamentEmailLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function handle(object $event): void
->put($savePathRaw, $rawMessage->getMessage()->toString());

$model::create([
'team_id' => Filament::getTenant()?->id ?? null,
'from' => $this->recipientsToString($email->getFrom()),
'to' => $this->recipientsToString($email->getTo()),
'cc' => $this->recipientsToString($email->getCc()),
Expand All @@ -59,7 +60,6 @@ public function handle(object $event): void
'raw_body' => $savePathRaw,
'sent_debug_info' => $rawMessage->getDebug(),
'attachments' => ! empty($attachments) ? $attachments : null,
'tenant_id' => Filament::getTenant()?->id ?? auth()->id(),
]);

}
Expand Down
19 changes: 17 additions & 2 deletions src/Models/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

namespace RickDBCN\FilamentEmail\Models;

use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Mpdf\Tag\B;

/**
* Email
Expand Down Expand Up @@ -41,9 +45,20 @@ class Email extends Model
'attachments' => 'json',
];

public function team()
public function team(): BelongsTo
{
return $this->hasOne(config('filament-email')['tenant_model'], 'id', 'tenant_id');
return $this->belongsTo(config('filament-email.tenant_model'), 'team_id', 'id');
}

protected static function booted(): void
{
static::addGlobalScope('teams', function (Builder $query) {
if (auth()->check() && Filament::getTenant()) {
$query->whereBelongsTo(auth()->user()?->teams);
} else {
$query->whereTeamId(null);
}
});
}

public static function boot()
Expand Down

0 comments on commit 1db185c

Please sign in to comment.