Skip to content
Open
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
22 changes: 22 additions & 0 deletions database/factories/SiteFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Siteman\Cms\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Siteman\Cms\Models\Site;

/** @extends Factory<Site> */
class SiteFactory extends Factory
{
protected $model = Site::class;

/** @return array<string, mixed> */
public function definition(): array
{
return [
'name' => fake()->word(),
'slug' => fake()->word(),
'domain' => fake()->domainName(),
];
}
}
1 change: 1 addition & 0 deletions database/migrations/create_menus_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return new class extends Migration
Schema::create('menus', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('site_id')->constrained()->cascadeOnDelete();
$table->json('meta')->nullable();
$table->boolean('is_visible')->default(true);
$table->timestamps();
Expand Down
7 changes: 4 additions & 3 deletions database/migrations/create_pages_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ return new class extends Migration
{
Schema::create('pages', function (Blueprint $table) {
$table->id();
$table->foreignId('site_id')->constrained()->cascadeOnDelete();
$table->foreignId('parent_id')->nullable()->references('id')->on($table->getTable())->nullOnDelete();
$table->string('type')->default('page');
$table->string('title');
$table->string('slug');
$table->string('computed_slug')->unique();
$table->string('computed_slug');
$table->json('blocks')->nullable();
$table->string('layout')->nullable();
$table->timestamp('published_at')->nullable();
Expand All @@ -24,11 +25,11 @@ return new class extends Migration
$table->timestamps();
$table->softDeletes();

// Indexes for tree queries and ordering
$table->index('parent_id');
$table->index('order');
$table->index(['parent_id', 'order']); // Composite index for efficient sibling ordering
$table->index('type'); // For filtering by page type
$table->index('type');
$table->unique(['site_id', 'computed_slug']);
});
}

Expand Down
34 changes: 34 additions & 0 deletions database/migrations/create_sites_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

return new class extends Migration
{
public function up(): void
{
Schema::create('sites', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();
$table->string('domain')->nullable()->unique();
$table->json('meta')->nullable();
$table->softDeletes();
$table->timestamps();
});
Schema::create('site_user', function (Blueprint $table) {
$table->id();
$table->foreignId('site_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
$table->unique(['site_id', 'user_id']);
});
}

public function down(): void
{
Schema::dropIfExists('site_user');
Schema::dropIfExists('sites');
}
};
24 changes: 24 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,30 @@ parameters:
count: 1
path: database/factories/PageFactory.php

-
message: '#^Return type \(array\<string, mixed\>\) of method Siteman\\Cms\\Database\\Factories\\SiteFactory\:\:definition\(\) should be compatible with return type \(array\<model property of Siteman\\Cms\\Models\\Site, mixed\>\) of method Illuminate\\Database\\Eloquent\\Factories\\Factory\<Siteman\\Cms\\Models\\Site\>\:\:definition\(\)$#'
identifier: method.childReturnType
count: 1
path: database/factories/SiteFactory.php

-
message: '#^PHPDoc tag @var with type Filament\\Forms\\Components\\Field is not subtype of native type \$this\(Siteman\\Cms\\CmsServiceProvider\)\.$#'
identifier: varTag.nativeType
count: 1
path: src/CmsServiceProvider.php

-
message: '#^Access to an undefined property Illuminate\\Database\\Eloquent\\Model\:\:\$site_id\.$#'
identifier: property.notFound
count: 1
path: src/Models/Menu.php

-
message: '#^Access to an undefined property Illuminate\\Database\\Eloquent\\Model\:\:\$site_id\.$#'
identifier: property.notFound
count: 1
path: src/Models/Page.php

-
message: '#^Method Siteman\\Cms\\Models\\Page\:\:getChildren\(\) should return Illuminate\\Support\\Collection\<int, Siteman\\Cms\\Contracts\\MenuItemInterface\> but returns Staudenmeir\\LaravelAdjacencyList\\Eloquent\\Collection\<int, static\(Siteman\\Cms\\Models\\Page\)\>\.$#'
identifier: return.type
Expand All @@ -84,6 +102,12 @@ parameters:
count: 1
path: src/Models/Page.php

-
message: '#^Access to an undefined property Illuminate\\Database\\Eloquent\\Model\:\:\$site_id\.$#'
identifier: property.notFound
count: 1
path: src/Models/Role.php

-
message: '#^Call to an undefined method Filament\\Forms\\Components\\Textarea\:\:asPageMetaField\(\)\.$#'
identifier: method.notFound
Expand Down
6 changes: 6 additions & 0 deletions src/CmsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Siteman\Cms\Commands\InstallCommand;
use Siteman\Cms\Commands\MakeBlockCommand;
use Siteman\Cms\Commands\MakeSettingsCommand;
use Siteman\Cms\Commands\MakeSiteCommand;
use Siteman\Cms\Commands\MakeThemeCommand;
use Siteman\Cms\Commands\PublishCommand;
use Siteman\Cms\Commands\UpdateCommand;
Expand Down Expand Up @@ -66,6 +67,7 @@ public function configurePackage(Package $package): void
->hasViews('siteman')
->hasRoute('web')
->hasMigrations([
'create_sites_table',
'create_menus_table',
'create_pages_table',
'../settings/create_general_settings',
Expand All @@ -78,6 +80,7 @@ public function configurePackage(Package $package): void
MakeThemeCommand::class,
MakeBlockCommand::class,
MakeSettingsCommand::class,
MakeSiteCommand::class,
CreateAdminCommand::class,
]);

Expand Down Expand Up @@ -110,6 +113,9 @@ public function bootingPackage(): void
));

$config->set('tags.tag_model', Tag::class);
$config->set('permission.teams', true);
$config->set('permission.models.role', Models\Role::class);
$config->set('permission.column_names.team_foreign_key', 'site_id');
$config->set('filament-shield.shield_resource.show_model_path', false);
$config->set('filament-shield.permission_prefixes.resource', [
'view_any',
Expand Down
48 changes: 45 additions & 3 deletions src/Commands/CreateAdminCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@

use Illuminate\Console\Command;
use Siteman\Cms\Facades\Siteman;
use Siteman\Cms\Models\Site;

use function Laravel\Prompts\password;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;

class CreateAdminCommand extends Command
{
public $signature = 'siteman:create-admin username {name?} {email?} {password?}';
public $signature = 'siteman:create-admin {name?} {email?} {password?} {--site= : Site ID or slug}';

public $description = 'This command creates a Siteman admin';
public $description = 'This command creates a Siteman admin for a site';

public function handle(): int
{
$site = $this->resolveSite();

if (!$site) {
$this->components->error('No site found. Please create a site first or specify a valid --site option.');

return self::FAILURE;
}

Siteman::setCurrentSite($site);

$username = $this->argument('name') ?? text('Enter the admin user name', default: 'admin');
$email = $this->argument('email') ?? text('Enter the admin email', default: 'admin@admin.com');
$password = $this->argument('password') ?? password('Enter the admin password');
Expand All @@ -26,10 +38,40 @@ public function handle(): int
'password' => bcrypt($password),
]);

$user->sites()->attach($site);
$user->assignRole(Siteman::createSuperAdminRole());

$this->components->info('User successfully created. You can now log in at '.Siteman::getLoginUrl());
$this->components->info("User successfully created for site '{$site->name}'. You can now log in at ".Siteman::getLoginUrl());

return self::SUCCESS;
}

protected function resolveSite(): ?Site
{
$siteOption = $this->option('site');

if ($siteOption) {
return Site::where('id', $siteOption)
->orWhere('slug', $siteOption)
->orWhere('domain', $siteOption)
->first();
}

$sites = Site::all();

if ($sites->isEmpty()) {
return null;
}

if ($sites->count() === 1) {
return $sites->first();
}

$siteId = select(
label: 'Select a site for the admin user',
options: $sites->pluck('name', 'id')->toArray(),
);

return Site::find($siteId);
}
}
34 changes: 32 additions & 2 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
<?php declare(strict_types=1);

namespace Siteman\Cms\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Process;
use Laravel\Prompts\Prompt;
use Siteman\Cms\Models\Site;
use Symfony\Component\Process\PhpExecutableFinder;

use function Laravel\Prompts\confirm;
Expand Down Expand Up @@ -69,6 +70,8 @@ public function handle(Filesystem $filesystem): int
}
}

$site = $this->createDefaultSite();

if ($userModel::count() > 0
&& !confirm('There are already users in the database. Do you want to continue?')) {
$this->components->info('No user created. All done.');
Expand All @@ -80,7 +83,15 @@ public function handle(Filesystem $filesystem): int
$email = text('Enter the admin email', default: 'admin@admin.com');
$password = password('Enter the admin password');

Process::run([(new PhpExecutableFinder)->find(), 'artisan', 'siteman:create-admin', $name, $email, $password], function ($type, $line) {
Process::run([
(new PhpExecutableFinder)->find(),
'artisan',
'siteman:create-admin',
$name,
$email,
$password,
'--site='.$site->id,
], function ($type, $line) {
$this->output->write($line);
});

Expand All @@ -94,4 +105,23 @@ public function handle(Filesystem $filesystem): int

return self::SUCCESS;
}

protected function createDefaultSite(): Site
{
if ($site = Site::first()) {
$this->components->info("Using existing site: {$site->name}");

return $site;
}

$siteName = text('Enter the site name', default: config('app.name', 'My Site'));
$siteDomain = text('Enter the site domain (optional)', default: '', hint: 'e.g., example.com');

$this->call('siteman:make-site', [
'name' => $siteName,
'--domain' => $siteDomain ?: null,
]);

return Site::latest()->first();
}
}
50 changes: 50 additions & 0 deletions src/Commands/MakeSiteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

namespace Siteman\Cms\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Siteman\Cms\Models\Site;

use function Laravel\Prompts\text;

class MakeSiteCommand extends Command
{
public $signature = 'siteman:make-site {name?} {--slug=} {--domain=}';

public $description = 'Create a new site';

public function handle(): int
{
$name = $this->argument('name') ?? text(
label: 'Enter the site name',
default: config('app.name', 'My Site'),
required: true,
);

$slug = $this->option('slug') ?? Str::slug($name);
$domain = $this->option('domain') ?: null;

if (Site::where('slug', $slug)->exists()) {
$this->components->error("A site with slug '{$slug}' already exists.");

return self::FAILURE;
}

if ($domain && Site::where('domain', $domain)->exists()) {
$this->components->error("A site with domain '{$domain}' already exists.");

return self::FAILURE;
}

$site = Site::create([
'name' => $name,
'slug' => $slug,
'domain' => $domain,
]);

$this->components->info("Site '{$site->name}' created successfully with slug '{$site->slug}'.");

return self::SUCCESS;
}
}
18 changes: 18 additions & 0 deletions src/Http/Middleware/SitePermissionMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Siteman\Cms\Http\Middleware;

use Filament\Facades\Filament;
use Illuminate\Http\Request;

class SitePermissionMiddleware
{
public function handle(Request $request, \Closure $next)
{
if (!empty(auth()->user())) {
setPermissionsTeamId(Filament::getTenant()->getKey());
}

return $next($request);
}
}
8 changes: 7 additions & 1 deletion src/Http/SitemanController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Siteman\Cms\Http;

Expand All @@ -12,6 +12,12 @@ class SitemanController
{
public function __invoke(Request $request): mixed
{
$site = Siteman::getCurrentSite();

if (!$site) {
abort(404, 'Site not found');
}

$page = Page::published()->where('computed_slug', '/'.ltrim($request->path(), '/'))->first();
if (!$page) {
$rootPath = '/'.str($request->path())->before('/');
Expand Down
Loading