From 55a3959d51d538c004c313a6d65268d4dc73d815 Mon Sep 17 00:00:00 2001 From: Sylvester Damgaard Date: Tue, 2 Jun 2026 22:53:54 +0200 Subject: [PATCH] Fix Eloquent user IDs for MCP dashboard --- README.md | 2 +- UPGRADE.md | 2 +- composer.json | 2 +- docs/getting-started/installation.md | 2 +- docs/introduction.md | 2 +- src/Console/InstallCommand.php | 4 +- .../CP/Concerns/ResolvesUserId.php | 6 +- .../Controllers/OAuth/AuthorizeController.php | 4 +- src/Support/UserIds.php | 13 ++++ .../Controllers/CP/ResolvesUserIdTest.php | 72 +++++++++++++++++++ tests/Unit/Support/UserIdsTest.php | 21 ++++++ 11 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 src/Support/UserIds.php create mode 100644 tests/Unit/Http/Controllers/CP/ResolvesUserIdTest.php create mode 100644 tests/Unit/Support/UserIdsTest.php diff --git a/README.md b/README.md index 74def7e..7b2ecac 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A comprehensive MCP (Model Context Protocol) server for Statamic CMS v6 that pro - PHP 8.3+ - Laravel 12+ - Statamic 6.6+ -- Laravel MCP ^0.6 +- Laravel MCP ^0.6 || ^0.7 ## Installation diff --git a/UPGRADE.md b/UPGRADE.md index 83f5b27..a4c21c4 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -7,7 +7,7 @@ | PHP | ^8.3 | ^8.3 | | Statamic | ^5.65 \| ^6.0 | ^6.6 | | Laravel | ^11.0 \| ^12.0 | ^12.0 \| ^13.0 (via Statamic v6) | -| Laravel MCP | ^0.4.1 \| ^0.5 | ^0.6 | +| Laravel MCP | ^0.4.1 \| ^0.5 | ^0.6 \|\| ^0.7 | | Symfony YAML | ^7.3 | ^7.0 \| ^8.0 | **Statamic v5 support has been removed.** If you are on Statamic v5, stay on v1.x. diff --git a/composer.json b/composer.json index e5d0cdf..262d672 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require": { "php": "^8.3", "statamic/cms": "^6.6", - "laravel/mcp": "^0.6", + "laravel/mcp": "^0.6 || ^0.7", "symfony/yaml": "^7.0 || ^8.0" }, "require-dev": { diff --git a/docs/getting-started/installation.md b/docs/getting-started/installation.md index 62916a3..5ee03bf 100644 --- a/docs/getting-started/installation.md +++ b/docs/getting-started/installation.md @@ -11,7 +11,7 @@ weight: 1 - **PHP** 8.3 or higher - **Statamic CMS** v6.6+ - **Laravel** 12.0+ -- **laravel/mcp** ^0.6 (installed automatically as a dependency) +- **laravel/mcp** ^0.6 || ^0.7 (installed automatically as a dependency) ## Install via Composer diff --git a/docs/introduction.md b/docs/introduction.md index 818a1e2..62efef0 100644 --- a/docs/introduction.md +++ b/docs/introduction.md @@ -74,7 +74,7 @@ The server registers a set of domain routers — each handling a specific Statam - **PHP** 8.3+ - **Statamic** v6.6+ - **Laravel** 12.0+ -- **laravel/mcp** ^0.6 +- **laravel/mcp** ^0.6 || ^0.7 ## Quick Links diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index 7541e54..93d3e89 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -666,7 +666,7 @@ protected function getCursorRulesContent(string $projectPath): string # Statamic MCP Server v2.0 Integration This project uses Statamic MCP Server v2.0 for enhanced AI-assisted development. -Requires Statamic v6+ and laravel/mcp v0.6+. +Requires Statamic v6+ and laravel/mcp ^0.6 or ^0.7. ## MCP Server Configuration - Command: php artisan mcp:start statamic @@ -801,7 +801,7 @@ protected function getStatamicMcpGuidelines(): string # Statamic MCP Guidelines (v2.0) This file provides AI assistants with comprehensive understanding of the Statamic MCP Server v2.0 capabilities. -Requires Statamic v6+ and laravel/mcp v0.6+. +Requires Statamic v6+ and laravel/mcp ^0.6 or ^0.7. ## MCP Server Overview diff --git a/src/Http/Controllers/CP/Concerns/ResolvesUserId.php b/src/Http/Controllers/CP/Concerns/ResolvesUserId.php index 26752a5..0b5ae86 100644 --- a/src/Http/Controllers/CP/Concerns/ResolvesUserId.php +++ b/src/Http/Controllers/CP/Concerns/ResolvesUserId.php @@ -4,6 +4,7 @@ namespace Cboxdk\StatamicMcp\Http\Controllers\CP\Concerns; +use Cboxdk\StatamicMcp\Support\UserIds; use Statamic\Facades\User; trait ResolvesUserId @@ -15,9 +16,6 @@ private function resolveUserId(): string { $user = User::current(); - /** @var string $userId */ - $userId = $user ? $user->id() : ''; - - return $userId; + return $user ? UserIds::normalize($user->id()) : ''; } } diff --git a/src/Http/Controllers/OAuth/AuthorizeController.php b/src/Http/Controllers/OAuth/AuthorizeController.php index 6fae83f..b242383 100644 --- a/src/Http/Controllers/OAuth/AuthorizeController.php +++ b/src/Http/Controllers/OAuth/AuthorizeController.php @@ -12,6 +12,7 @@ use Cboxdk\StatamicMcp\OAuth\Contracts\OAuthDriver; use Cboxdk\StatamicMcp\OAuth\Exceptions\OAuthException; use Cboxdk\StatamicMcp\OAuth\OAuthClient; +use Cboxdk\StatamicMcp\Support\UserIds; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; @@ -277,8 +278,7 @@ public function approve(Request $request): RedirectResponse ]))); } - /** @var string $userId */ - $userId = $user->id(); + $userId = UserIds::normalize($user->id()); // Get originally requested scopes from the hidden form field (set by show()) /** @var string $originalScope */ diff --git a/src/Support/UserIds.php b/src/Support/UserIds.php new file mode 100644 index 0000000..51f3900 --- /dev/null +++ b/src/Support/UserIds.php @@ -0,0 +1,13 @@ +once() + ->andReturn($this->makeEloquentUserWithId(1)); + + $this->assertSame('1', $this->resolveCurrentUserId()); + } + + public function test_preserves_string_user_ids(): void + { + User::shouldReceive('current') + ->once() + ->andReturn($this->makeEloquentUserWithId('flat-file-user')); + + $this->assertSame('flat-file-user', $this->resolveCurrentUserId()); + } + + public function test_resolves_missing_user_as_empty_string(): void + { + User::shouldReceive('current') + ->once() + ->andReturn(null); + + $this->assertSame('', $this->resolveCurrentUserId()); + } + + private function resolveCurrentUserId(): string + { + $controller = new class + { + use ResolvesUserId; + }; + + $method = new ReflectionMethod($controller, 'resolveUserId'); + $method->setAccessible(true); + + return $method->invoke($controller); + } + + private function makeEloquentUserWithId(int|string $id): EloquentUser + { + $model = new class extends Model + { + public $timestamps = false; + + public $incrementing = false; + + protected $keyType = 'string'; + + protected $guarded = []; + }; + $model->setAttribute('id', $id); + + return (new EloquentUser)->model($model); + } +} diff --git a/tests/Unit/Support/UserIdsTest.php b/tests/Unit/Support/UserIdsTest.php new file mode 100644 index 0000000..3e79210 --- /dev/null +++ b/tests/Unit/Support/UserIdsTest.php @@ -0,0 +1,21 @@ +assertSame('1', UserIds::normalize(1)); + } + + public function test_preserves_string_user_ids_from_flat_file_driver(): void + { + $this->assertSame('flat-file-user', UserIds::normalize('flat-file-user')); + } +}