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)
191 lines
5.5 KiB
PHP
191 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Blog;
|
|
|
|
use App\Http\Controllers\Concerns\ValidatesListQuery;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Blog\StoreCategoryRequest;
|
|
use App\Http\Requests\Blog\UpdateCategoryRequest;
|
|
use App\Http\Resources\CategoryResource;
|
|
use App\Http\Resources\PostResource;
|
|
use App\Models\Blog\Category;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* @OA\Tag(name="Categories", description="Blog categories")
|
|
*/
|
|
class CategoryController extends Controller
|
|
{
|
|
use ValidatesListQuery;
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/blog/categories",
|
|
* tags={"Categories"},
|
|
* summary="Paginated list of categories",
|
|
*
|
|
* @OA\Parameter(name="per_page", in="query", @OA\Schema(type="integer")),
|
|
* @OA\Parameter(name="filter", in="query", @OA\Schema(type="string")),
|
|
*
|
|
* @OA\Response(response=200, description="OK")
|
|
* )
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$params = $this->listQuery(
|
|
$request,
|
|
['name', 'slug', 'created_at', 'updated_at']
|
|
);
|
|
|
|
$query = Category::with('posts');
|
|
|
|
if ($request->filled('filter')) {
|
|
$query->where('name', 'like', '%'.$request->string('filter').'%');
|
|
}
|
|
|
|
$query->orderBy($params['sort'], $params['direction']);
|
|
|
|
$categories = $query->paginate($params['per_page']);
|
|
|
|
return CategoryResource::collection($categories);
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/blog/categories/{id}",
|
|
* tags={"Categories"},
|
|
* summary="Get a category by ID",
|
|
*
|
|
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
|
|
*
|
|
* @OA\Response(response=200, description="OK")
|
|
* )
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$category = Category::with('posts')->findOrFail($id);
|
|
|
|
return new CategoryResource($category);
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/blog/categories/{slug}/posts",
|
|
* tags={"Categories"},
|
|
* summary="Get posts by category slug",
|
|
*
|
|
* @OA\Parameter(name="slug", in="path", required=true, @OA\Schema(type="string")),
|
|
* @OA\Parameter(name="per_page", in="query", @OA\Schema(type="integer")),
|
|
*
|
|
* @OA\Response(response=200, description="OK"),
|
|
* @OA\Response(response=404, description="Category not found")
|
|
* )
|
|
*/
|
|
public function getCategoryWithPosts(Request $request, $slug)
|
|
{
|
|
$category = Category::where('slug', $slug)->first();
|
|
|
|
if (! $category) {
|
|
return response()->json([
|
|
'message' => 'Category not found.',
|
|
], 404);
|
|
}
|
|
|
|
$perPage = $request->input('per_page', 10);
|
|
$filter = $request->input('filter');
|
|
$sortBy = $request->input('sort_by', 'published_at');
|
|
$sortDirection = $request->input('sort_direction', 'desc');
|
|
|
|
$postsQuery = $category->posts()->with(['category', 'tags', 'author']);
|
|
|
|
if (! empty($filter)) {
|
|
$postsQuery->where('title', 'like', '%'.$filter.'%');
|
|
}
|
|
|
|
$postsQuery->orderBy($sortBy, $sortDirection);
|
|
|
|
$posts = $postsQuery->paginate($perPage);
|
|
|
|
$meta = [
|
|
'total' => $posts->total(),
|
|
'per_page' => $posts->perPage(),
|
|
'current_page' => $posts->currentPage(),
|
|
'last_page' => $posts->lastPage(),
|
|
'from' => $posts->firstItem(),
|
|
'to' => $posts->lastItem(),
|
|
];
|
|
|
|
$links = [
|
|
'first' => $posts->url(1),
|
|
'last' => $posts->url($posts->lastPage()),
|
|
'prev' => $posts->previousPageUrl(),
|
|
'next' => $posts->nextPageUrl(),
|
|
];
|
|
|
|
return response()->json([
|
|
'category' => new CategoryResource($category),
|
|
'posts' => PostResource::collection($posts),
|
|
'meta' => $meta,
|
|
'links' => $links,
|
|
], 200);
|
|
}
|
|
|
|
/**
|
|
* @OA\Post(
|
|
* path="/blog/categories",
|
|
* tags={"Categories"},
|
|
* summary="Create a category",
|
|
*
|
|
* @OA\RequestBody(@OA\MediaType(mediaType="application/json")),
|
|
*
|
|
* @OA\Response(response=201, description="Created")
|
|
* )
|
|
*/
|
|
public function store(StoreCategoryRequest $request)
|
|
{
|
|
$category = Category::create($request->validated());
|
|
|
|
return new CategoryResource($category);
|
|
}
|
|
|
|
/**
|
|
* @OA\Put(
|
|
* path="/blog/categories/{id}",
|
|
* tags={"Categories"},
|
|
* summary="Update a category",
|
|
*
|
|
* @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")
|
|
* )
|
|
*/
|
|
public function update(UpdateCategoryRequest $request, $id)
|
|
{
|
|
$category = Category::findOrFail($id);
|
|
$category->update($request->validated());
|
|
|
|
return new CategoryResource($category);
|
|
}
|
|
|
|
/**
|
|
* @OA\Delete(
|
|
* path="/blog/categories/{id}",
|
|
* tags={"Categories"},
|
|
* summary="Delete a category",
|
|
*
|
|
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
|
|
*
|
|
* @OA\Response(response=200, description="OK")
|
|
* )
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$category = Category::findOrFail($id);
|
|
$category->delete();
|
|
|
|
return response()->json(['message' => 'Category deleted successfully'], 200);
|
|
}
|
|
}
|