Close anonymous write access to the public API and repair the broken test suite
Deploy Backend / deploy (push) Has been cancelled

Every mutating endpoint was reachable without authentication, so any visitor
could create, edit or delete posts, categories, tags, projects and about-me
records on the live site.

Security
- Split routes into an anonymous read-only group and a write group gated by
  `auth:sanctum` + a new `admin` middleware.
- Add `is_admin` to users; Sanctum token support on the User model.
- Add AuthController (login / me / logout). The Sanctum token is delivered in
  an httpOnly cookie and never returned in the JSON body, because the admin
  panel reaches the API through a Next.js server-side proxy that breaks
  Sanctum's first-party domain detection.
- AuthenticateAdminPanelSession promotes that cookie to a bearer header so
  Sanctum's normal validation, expiry and revocation still apply.
- Remove the unauthenticated `/user/{id}` route, which exposed user emails.
- Non-admin accounts are refused at login and the failure shape is identical
  to a bad password, so the endpoint cannot enumerate accounts.
- Fix `Route [login] not defined`: a non-JSON unauthenticated request resolved
  a route that does not exist in this API-only app and returned 500.
- Reject invalid list query parameters with 422 instead of 500, and cap
  per_page at 50 (`?per_page=100000` previously dumped whole tables).

Correctness
- Make the translation migration portable. It used MySQL-only JSON_OBJECTAGG,
  which aborted all 29 tests during migration; the data collapse now runs in
  PHP so it works on MySQL, MariaDB and SQLite.
- Add HasJsonTranslations trait providing the missing setTranslation(), which
  made project create/update return 500, and replace five copy-pasted
  getTranslation()/hasTranslation() bodies.
- Make posts.excerpt nullable to match StorePostRequest; omitting an excerpt
  was a guaranteed SQL integrity error.
- Route the AboutMeController CRUD methods that existed but were never
  registered.
- CategoryTest omitted a required description; fixed the test, the existing
  schema and validation already agreed.

Tests: 83 passing / 126 assertions (was 29 failing / 0 assertions)
This commit is contained in:
root authored and DevOps committed 2026-10-01 18:47:34 +00:00
1 parent 35686d82c3
commit d2f6bd4d7d
29 files changed
+1002 -188

No files matched your search

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api\Blog;
use App\Http\Controllers\Concerns\ValidatesListQuery;
use App\Http\Controllers\Controller;
use App\Http\Requests\Blog\StorePostRequest;
use App\Http\Requests\Blog\UpdatePostRequest;
@@ -14,17 +15,21 @@ use Illuminate\Http\Request;
*/
class PostController extends Controller
{
use ValidatesListQuery;
/**
* @OA\Get(
* path="/blog/posts",
* tags={"Posts"},
* summary="Paginated list of posts",
*
* @OA\Parameter(name="per_page", in="query", @OA\Schema(type="integer")),
* @OA\Parameter(name="category", in="query", @OA\Schema(type="string")),
* @OA\Parameter(name="tag", in="query", @OA\Schema(type="string")),
* @OA\Parameter(name="search", in="query", @OA\Schema(type="string")),
* @OA\Parameter(name="sort_field", in="query", @OA\Schema(type="string")),
* @OA\Parameter(name="sort_direction", in="query", @OA\Schema(type="string", enum={"asc","desc"})),
*
* @OA\Response(response=200, description="OK")
* )
*/
@@ -32,16 +37,19 @@ class PostController extends Controller
{
$filters = $request->only(['category', 'tag', 'search', 'author', 'published']);
$sortField = $request->get('sort_field', 'published_at');
$sortDirection = $request->get('sort_direction', 'desc');
$perPage = $request->get('per_page', 10);
$params = $this->listQuery(
$request,
['published_at', 'views', 'likes', 'title'],
'sort_field',
'published_at',
'desc'
);
$query = Post::with(['category', 'tags', 'author'])
->filter($filters)
->sort($sortField, $sortDirection);
->sort($params['sort'], $params['direction']);
$posts = $query->paginate($perPage);
$posts = $query->paginate($params['per_page']);
return PostResource::collection($posts);
}
@@ -51,7 +59,9 @@ class PostController extends Controller
* path="/blog/posts/{slug}",
* tags={"Posts"},
* summary="Get a single post by slug with related posts",
*
* @OA\Parameter(name="slug", in="path", required=true, @OA\Schema(type="string")),
*
* @OA\Response(response=200, description="OK")
* )
*/
@@ -72,7 +82,9 @@ class PostController extends Controller
* path="/blog/posts",
* tags={"Posts"},
* summary="Create a post",
*
* @OA\RequestBody(@OA\MediaType(mediaType="application/json")),
*
* @OA\Response(response=201, description="Created")
* )
*/
@@ -92,8 +104,11 @@ class PostController extends Controller
* path="/blog/posts/{id}",
* tags={"Posts"},
* summary="Update a post",
*
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
*
* @OA\RequestBody(@OA\MediaType(mediaType="application/json")),
*
* @OA\Response(response=200, description="OK")
* )
*/
@@ -114,7 +129,9 @@ class PostController extends Controller
* path="/blog/posts/{id}",
* tags={"Posts"},
* summary="Delete a post",
*
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
*
* @OA\Response(response=200, description="OK")
* )
*/