Skip to content

Commit b84cbc2

Browse files
author
thapacodes4u
committed
feat: implement pages and sections with tenant scoping & navigation
1 parent e1d0861 commit b84cbc2

3 files changed

Lines changed: 57 additions & 21 deletions

File tree

src/CmsPlugin.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,43 @@
22

33
namespace Eclipse\Cms;
44

5+
use Eclipse\Cms\Admin\Filament\Resources\SectionResource;
6+
use Eclipse\Cms\Models\Section;
57
use Eclipse\Common\Foundation\Plugins\Plugin;
8+
use Exception;
9+
use Filament\Facades\Filament;
10+
use Filament\Navigation\NavigationItem;
11+
use Filament\Panel;
612

713
class CmsPlugin extends Plugin
814
{
9-
//
15+
public function register(Panel $panel): void
16+
{
17+
parent::register($panel);
18+
19+
$panel->navigationItems($this->getSectionNavigationItems());
20+
}
21+
22+
public function getSectionNavigationItems(): array
23+
{
24+
try {
25+
return Section::query()
26+
->select('name', 'id', config('eclipse-cms.tenancy.foreign_key'))
27+
->get()
28+
->map(fn (Section $section): NavigationItem => NavigationItem::make($section->getTranslation('name', app()->getLocale()))
29+
->url(
30+
fn (): string => SectionResource::getUrl('edit', [
31+
'record' => $section->id,
32+
])
33+
)
34+
->icon('heroicon-o-arrow-turn-down-right')
35+
->group('CMS')
36+
->sort(10)
37+
->visible(fn (): bool => $section->{config('eclipse-cms.tenancy.foreign_key')} === Filament::getTenant()?->id)
38+
)
39+
->toArray();
40+
} catch (Exception) {
41+
return [];
42+
}
43+
}
1044
}

src/Models/Section.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Eclipse\Cms\Enums\SectionType;
66
use Eclipse\Cms\Factories\SectionFactory;
7+
use Filament\Facades\Filament;
78
use Illuminate\Database\Eloquent\Factories\HasFactory;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -48,9 +49,25 @@ protected static function newFactory(): SectionFactory
4849
return SectionFactory::new();
4950
}
5051

51-
/** @return BelongsTo<\Eclipse\Core\Models\Site, self> */
5252
public function site(): BelongsTo
5353
{
54-
return $this->belongsTo(\Eclipse\Core\Models\Site::class);
54+
return $this->belongsTo(config('eclipse-cms.tenancy.model'));
55+
}
56+
57+
protected static function booted(): void
58+
{
59+
if (config('eclipse-cms.tenancy.enabled')) {
60+
static::addGlobalScope('tenant', function ($query): void {
61+
if ($tenant = Filament::getTenant()) {
62+
$query->where(config('eclipse-cms.tenancy.foreign_key'), $tenant->id);
63+
}
64+
});
65+
66+
static::creating(function ($model): void {
67+
if ($tenant = Filament::getTenant()) {
68+
$model->{config('eclipse-cms.tenancy.foreign_key')} = $tenant->id;
69+
}
70+
});
71+
}
5572
}
5673
}

tests/TestCase.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ protected function setUp(): void
2626
config(['eclipse-cms.tenancy.enabled' => false]);
2727
config(['eclipse-cms.tenancy.model' => 'Workbench\\App\\Models\\Site']);
2828
config(['eclipse-cms.tenancy.foreign_key' => 'site_id']);
29-
config(['app.key' => 'base64:'.base64_encode('12345678901234567890123456789012')]);
3029

31-
// Disable Scout during tests
3230
config(['scout.driver' => null]);
3331
}
3432

@@ -42,26 +40,14 @@ protected function migrate(): self
4240
protected function setUpSuperAdmin(): self
4341
{
4442
$this->migrate();
45-
$this->superAdmin = User::factory()->create();
46-
$role = Role::firstOrCreate(['name' => 'super_admin', 'guard_name' => 'web']);
47-
48-
$permissions = [
49-
'view_any_page', 'view_page', 'create_page', 'update_page', 'delete_page', 'restore_page', 'force_delete_page',
50-
'view_any_section', 'view_section', 'create_section', 'update_section', 'delete_section', 'restore_section', 'force_delete_section',
51-
];
52-
53-
foreach ($permissions as $permission) {
54-
Permission::firstOrCreate(['name' => $permission, 'guard_name' => 'web']);
55-
}
56-
57-
$role->syncPermissions($permissions);
58-
$this->superAdmin->assignRole($role);
43+
$this->superAdmin = User::factory()->make();
44+
$this->superAdmin->assignRole('super_admin')->save();
5945
$this->actingAs($this->superAdmin);
6046

6147
return $this;
6248
}
6349

64-
protected function setUpCommonUserAndTenant(): self
50+
protected function setUpCommonUser(): self
6551
{
6652
$this->migrate();
6753
$this->user = User::factory()->create();
@@ -72,7 +58,6 @@ protected function setUpCommonUserAndTenant(): self
7258

7359
protected function setUpUserWithoutPermissions(): self
7460
{
75-
$this->migrate();
7661
$this->user = User::factory()->create();
7762
$this->actingAs($this->user);
7863

0 commit comments

Comments
 (0)