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

@@ -0,0 +1,61 @@
<?php
namespace App\Models\Concerns;
/**
* Locale-aware accessors for models that store translations in a single
* `translations` JSON column, shaped as:
*
* ['title' => ['fa' => '...', 'en' => '...']]
*
* The website models each had their own copy-pasted getTranslation()/hasTranslation()
* and no setTranslation() at all, which is why the project write endpoints
* returned HTTP 500 in production. This trait replaces the duplicated bodies
* and supplies the missing writer.
*/
trait HasJsonTranslations
{
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true): mixed
{
$translations = $this->translations;
if (! is_array($translations) || ! isset($translations[$key][$locale])) {
if (! $useFallbackLocale) {
return null;
}
$fallback = $this->getFallbackLocale();
return $translations[$key][$fallback] ?? null;
}
return $translations[$key][$locale];
}
/**
* Set a single locale's value. Call save() to persist.
*/
public function setTranslation(string $key, string $locale, mixed $value): static
{
$translations = $this->translations;
$translations = is_array($translations) ? $translations : [];
$translations[$key][$locale] = $value;
$this->translations = $translations;
return $this;
}
public function hasTranslation(string $key, string $locale): bool
{
$translations = $this->translations;
return is_array($translations) && isset($translations[$key][$locale]);
}
public function getFallbackLocale(): string
{
return (string) config('app.fallback_locale', 'en');
}
}
+12 -1
View File
@@ -8,10 +8,11 @@ use App\Models\Blog\Post;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
@@ -22,6 +23,7 @@ class User extends Authenticatable
'name',
'email',
'password',
'is_admin',
];
/**
@@ -44,6 +46,7 @@ class User extends Authenticatable
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'is_admin' => 'boolean',
];
}
@@ -51,4 +54,12 @@ class User extends Authenticatable
{
return $this->hasMany(Post::class, 'author_id');
}
/**
* Whether this user may modify site content.
*/
public function isAdmin(): bool
{
return (bool) $this->is_admin;
}
}
+5 -24
View File
@@ -2,19 +2,19 @@
namespace App\Models\Website;
use App\Models\Concerns\HasJsonTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class AboutMe extends Model
{
use HasFactory;
use HasJsonTranslations;
protected $table = 'website_about_me';
protected $fillable = ['image',
'translations',];
protected $fillable = ['image',
'translations', ];
public function skills()
{
@@ -25,24 +25,6 @@ class AboutMe extends Model
'translations' => 'array',
];
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true): mixed
{
$translations = $this->translations;
if (isset($translations[$key][$locale])) {
return $translations[$key][$locale];
}
if ($useFallbackLocale && isset($translations[$key]['en'])) {
return $translations[$key]['en'];
}
return null;
}
public function hasTranslation(string $key, string $locale): bool
{
$translations = $this->translations;
return isset($translations[$key][$locale]);
}
public function getTitleAttribute()
{
return $this->translations['title'] ?? null;
@@ -52,5 +34,4 @@ class AboutMe extends Model
{
return $this->translations['description'] ?? null;
}
}
}
+4 -17
View File
@@ -2,19 +2,19 @@
namespace App\Models\Website;
use App\Models\Concerns\HasJsonTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
use HasJsonTranslations;
protected $table = 'website_projects';
protected $fillable = ['category_id', 'image1', 'image2',
'translations',];
protected $fillable = ['category_id', 'image1', 'image2',
'translations', ];
public function category()
{
@@ -25,18 +25,6 @@ class Project extends Model
'translations' => 'array',
];
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true): mixed
{
$translations = $this->translations;
if (isset($translations[$key][$locale])) {
return $translations[$key][$locale];
}
if ($useFallbackLocale && isset($translations[$key]['en'])) {
return $translations[$key]['en'];
}
return null;
}
public function getTitleAttribute()
{
return $this->translations['title'] ?? null;
@@ -46,5 +34,4 @@ class Project extends Model
{
return $this->translations['description'] ?? null;
}
}
+4 -17
View File
@@ -2,36 +2,24 @@
namespace App\Models\Website;
use App\Models\Concerns\HasJsonTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProjectCategory extends Model
{
use HasFactory;
use HasJsonTranslations;
protected $table = 'website_project_categories';
protected $fillable = ['image',
'translations',];
protected $fillable = ['image',
'translations', ];
protected $casts = [
'translations' => 'array',
];
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true): mixed
{
$translations = $this->translations;
if (isset($translations[$key][$locale])) {
return $translations[$key][$locale];
}
if ($useFallbackLocale && isset($translations[$key]['en'])) {
return $translations[$key]['en'];
}
return null;
}
public function getTitleAttribute()
{
return $this->translations['title'] ?? null;
@@ -41,5 +29,4 @@ class ProjectCategory extends Model
{
return $this->translations['description'] ?? null;
}
}
+4 -13
View File
@@ -2,14 +2,17 @@
namespace App\Models\Website;
use App\Models\Concerns\HasJsonTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Service extends Model
{
use HasFactory;
use HasJsonTranslations;
protected $table = 'website_services';
protected $fillable = ['imageSrc',
'translations'];
@@ -17,18 +20,6 @@ class Service extends Model
'translations' => 'array',
];
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true): mixed
{
$translations = $this->translations;
if (isset($translations[$key][$locale])) {
return $translations[$key][$locale];
}
if ($useFallbackLocale && isset($translations[$key]['en'])) {
return $translations[$key]['en'];
}
return null;
}
public function getTitleAttribute()
{
return $this->translations['title'] ?? null;
@@ -38,4 +29,4 @@ class Service extends Model
{
return $this->translations['description'] ?? null;
}
}
}
+4 -17
View File
@@ -2,19 +2,19 @@
namespace App\Models\Website;
use App\Models\Concerns\HasJsonTranslations;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Skill extends Model
{
use HasFactory;
use HasJsonTranslations;
protected $table = 'website_skills';
protected $fillable = ['image', 'link', 'about_me_id', 'percentage',
'translations',];
protected $fillable = ['image', 'link', 'about_me_id', 'percentage',
'translations', ];
public function aboutMe()
{
@@ -25,18 +25,6 @@ class Skill extends Model
'translations' => 'array',
];
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true): mixed
{
$translations = $this->translations;
if (isset($translations[$key][$locale])) {
return $translations[$key][$locale];
}
if ($useFallbackLocale && isset($translations[$key]['en'])) {
return $translations[$key]['en'];
}
return null;
}
public function getTitleAttribute()
{
return $this->translations['title'] ?? null;
@@ -46,5 +34,4 @@ class Skill extends Model
{
return $this->translations['description'] ?? null;
}
}