Harden legacy user profile lookups.

Keep the old user controller aligned with current profile fields and safer query handling.
This commit is contained in:
test
2026-09-20 14:50:59 +02:00
parent a138a3e404
commit 4479d81253
+65 -28
View File
@@ -3,12 +3,12 @@
namespace App\Http\Controllers\Legacy;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use App\Services\AvatarService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;
class UserController extends Controller
{
@@ -37,7 +37,7 @@ class UserController extends Controller
}
} else {
// Map legacy form fields into the modern schema.
$data = $request->only(['name','web','country_code','signature','description','about_me']);
$data = $request->only(['name', 'web', 'country_code', 'signature', 'description', 'about_me']);
// Core user column: `name`
if (isset($data['name'])) {
@@ -46,24 +46,43 @@ class UserController extends Controller
// Collect other profile updates to persist into `user_profiles` when available
$profileUpdates = [];
if (!empty($data['web'])) $profileUpdates['website'] = $data['web'];
if (!empty($data['signature'])) $profileUpdates['signature'] = $data['signature'];
if (!empty($data['description'])) $profileUpdates['description'] = $data['description'];
if (!empty($data['about_me'])) $profileUpdates['about'] = $data['about_me'];
if (!empty($data['country_code'])) $profileUpdates['country_code'] = $data['country_code'];
if (! empty($data['web'])) {
$profileUpdates['website'] = $data['web'];
}
if (! empty($data['signature'])) {
$profileUpdates['signature'] = $data['signature'];
}
if (! empty($data['description'])) {
$profileUpdates['description'] = $data['description'];
}
if (! empty($data['about_me'])) {
$profileUpdates['about'] = $data['about_me'];
}
if (! empty($data['country_code'])) {
$profileUpdates['country_code'] = $data['country_code'];
}
$d1 = $request->input('date1');
$d2 = $request->input('date2');
$d3 = $request->input('date3');
if ($d1 && $d2 && $d3) {
$profileUpdates['birthdate'] = sprintf('%04d-%02d-%02d', (int)$d3, (int)$d2, (int)$d1);
$profileUpdates['birthdate'] = sprintf('%04d-%02d-%02d', (int) $d3, (int) $d2, (int) $d1);
}
$userGender = $request->input('gender', $user->gender);
if (!empty($userGender)) {
$g = strtolower($userGender);
$map = ['m' => 'M', 'f' => 'F', 'n' => 'X', 'x' => 'X'];
$profileUpdates['gender'] = $map[$g] ?? strtoupper($userGender);
if ($userGender !== null && $userGender !== '') {
$g = strtolower(trim((string) $userGender));
$map = [
'm' => 'M',
'male' => 'M',
'f' => 'F',
'female' => 'F',
'n' => 'X',
'x' => 'X',
'na' => 'X',
'n/a' => 'X',
];
$profileUpdates['gender'] = $map[$g] ?? null;
}
$profileUpdates['mlist'] = $request->has('newsletter') ? 1 : 0;
@@ -85,7 +104,7 @@ class UserController extends Controller
$f = $request->file('personal_picture');
if (in_array($f->getMimeType(), $allowedLegacyMimes, true)) {
$ext = $f->guessExtension() ?: 'jpg';
$name = $user->id . '.' . $ext;
$name = $user->id.'.'.$ext;
$f->move(public_path('user-picture'), $name);
$profileUpdates['cover_image'] = $name;
$user->picture = $name;
@@ -96,7 +115,7 @@ class UserController extends Controller
$f = $request->file('emotion_icon');
if (in_array($f->getMimeType(), $allowedLegacyMimes, true)) {
$ext = $f->guessExtension() ?: 'jpg';
$name = $user->id . '.' . $ext;
$name = $user->id.'.'.$ext;
$f->move(public_path('emotion'), $name);
$user->eicon = $name;
}
@@ -107,9 +126,9 @@ class UserController extends Controller
// Persist profile updates into `user_profiles` when available, otherwise fallback to `users` table
try {
if (!empty($profileUpdates) && Schema::hasTable('user_profiles')) {
if (! empty($profileUpdates) && Schema::hasTable('user_profiles')) {
DB::table('user_profiles')->updateOrInsert(['user_id' => $user->id], $profileUpdates + ['updated_at' => now(), 'created_at' => now()]);
} elseif (!empty($profileUpdates)) {
} elseif (! empty($profileUpdates)) {
DB::table('users')->where('id', $user->id)->update($profileUpdates);
}
} catch (\Throwable $e) {
@@ -143,15 +162,33 @@ class UserController extends Controller
$profile = DB::table('user_profiles')->where('user_id', $user->id)->first();
if ($profile) {
// map modern profile fields onto the legacy user properties/helpers used by the view
if (isset($profile->website)) $user->homepage = $profile->website;
if (isset($profile->about)) $user->about_me = $profile->about;
if (isset($profile->birthdate)) $user->birth = $profile->birthdate;
if (isset($profile->gender)) $user->gender = $profile->gender;
if (isset($profile->country_code)) $user->country_code = $profile->country_code;
if (isset($profile->avatar_hash)) $user->icon = $profile->avatar_hash;
if (isset($profile->cover_image)) $user->picture = $profile->cover_image;
if (isset($profile->signature)) $user->signature = $profile->signature;
if (isset($profile->description)) $user->description = $profile->description;
if (isset($profile->website)) {
$user->homepage = $profile->website;
}
if (isset($profile->about)) {
$user->about_me = $profile->about;
}
if (isset($profile->birthdate)) {
$user->birth = $profile->birthdate;
}
if (isset($profile->gender)) {
$user->gender = $profile->gender;
}
if (isset($profile->country_code)) {
$user->country_code = $profile->country_code;
}
if (isset($profile->avatar_hash)) {
$user->icon = $profile->avatar_hash;
}
if (isset($profile->cover_image)) {
$user->picture = $profile->cover_image;
}
if (isset($profile->signature)) {
$user->signature = $profile->signature;
}
if (isset($profile->description)) {
$user->description = $profile->description;
}
}
}
} catch (\Throwable $e) {