Harden legacy user profile lookups.
Keep the old user controller aligned with current profile fields and safer query handling.
This commit is contained in:
@@ -3,12 +3,12 @@
|
|||||||
namespace App\Http\Controllers\Legacy;
|
namespace App\Http\Controllers\Legacy;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
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 App\Services\AvatarService;
|
||||||
use Carbon\Carbon;
|
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
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
@@ -37,7 +37,7 @@ class UserController extends Controller
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Map legacy form fields into the modern schema.
|
// 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`
|
// Core user column: `name`
|
||||||
if (isset($data['name'])) {
|
if (isset($data['name'])) {
|
||||||
@@ -46,24 +46,43 @@ class UserController extends Controller
|
|||||||
|
|
||||||
// Collect other profile updates to persist into `user_profiles` when available
|
// Collect other profile updates to persist into `user_profiles` when available
|
||||||
$profileUpdates = [];
|
$profileUpdates = [];
|
||||||
if (!empty($data['web'])) $profileUpdates['website'] = $data['web'];
|
if (! empty($data['web'])) {
|
||||||
if (!empty($data['signature'])) $profileUpdates['signature'] = $data['signature'];
|
$profileUpdates['website'] = $data['web'];
|
||||||
if (!empty($data['description'])) $profileUpdates['description'] = $data['description'];
|
}
|
||||||
if (!empty($data['about_me'])) $profileUpdates['about'] = $data['about_me'];
|
if (! empty($data['signature'])) {
|
||||||
if (!empty($data['country_code'])) $profileUpdates['country_code'] = $data['country_code'];
|
$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');
|
$d1 = $request->input('date1');
|
||||||
$d2 = $request->input('date2');
|
$d2 = $request->input('date2');
|
||||||
$d3 = $request->input('date3');
|
$d3 = $request->input('date3');
|
||||||
if ($d1 && $d2 && $d3) {
|
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);
|
$userGender = $request->input('gender', $user->gender);
|
||||||
if (!empty($userGender)) {
|
if ($userGender !== null && $userGender !== '') {
|
||||||
$g = strtolower($userGender);
|
$g = strtolower(trim((string) $userGender));
|
||||||
$map = ['m' => 'M', 'f' => 'F', 'n' => 'X', 'x' => 'X'];
|
$map = [
|
||||||
$profileUpdates['gender'] = $map[$g] ?? strtoupper($userGender);
|
'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;
|
$profileUpdates['mlist'] = $request->has('newsletter') ? 1 : 0;
|
||||||
@@ -85,7 +104,7 @@ class UserController extends Controller
|
|||||||
$f = $request->file('personal_picture');
|
$f = $request->file('personal_picture');
|
||||||
if (in_array($f->getMimeType(), $allowedLegacyMimes, true)) {
|
if (in_array($f->getMimeType(), $allowedLegacyMimes, true)) {
|
||||||
$ext = $f->guessExtension() ?: 'jpg';
|
$ext = $f->guessExtension() ?: 'jpg';
|
||||||
$name = $user->id . '.' . $ext;
|
$name = $user->id.'.'.$ext;
|
||||||
$f->move(public_path('user-picture'), $name);
|
$f->move(public_path('user-picture'), $name);
|
||||||
$profileUpdates['cover_image'] = $name;
|
$profileUpdates['cover_image'] = $name;
|
||||||
$user->picture = $name;
|
$user->picture = $name;
|
||||||
@@ -96,7 +115,7 @@ class UserController extends Controller
|
|||||||
$f = $request->file('emotion_icon');
|
$f = $request->file('emotion_icon');
|
||||||
if (in_array($f->getMimeType(), $allowedLegacyMimes, true)) {
|
if (in_array($f->getMimeType(), $allowedLegacyMimes, true)) {
|
||||||
$ext = $f->guessExtension() ?: 'jpg';
|
$ext = $f->guessExtension() ?: 'jpg';
|
||||||
$name = $user->id . '.' . $ext;
|
$name = $user->id.'.'.$ext;
|
||||||
$f->move(public_path('emotion'), $name);
|
$f->move(public_path('emotion'), $name);
|
||||||
$user->eicon = $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
|
// Persist profile updates into `user_profiles` when available, otherwise fallback to `users` table
|
||||||
try {
|
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()]);
|
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);
|
DB::table('users')->where('id', $user->id)->update($profileUpdates);
|
||||||
}
|
}
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
@@ -143,15 +162,33 @@ class UserController extends Controller
|
|||||||
$profile = DB::table('user_profiles')->where('user_id', $user->id)->first();
|
$profile = DB::table('user_profiles')->where('user_id', $user->id)->first();
|
||||||
if ($profile) {
|
if ($profile) {
|
||||||
// map modern profile fields onto the legacy user properties/helpers used by the view
|
// 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->website)) {
|
||||||
if (isset($profile->about)) $user->about_me = $profile->about;
|
$user->homepage = $profile->website;
|
||||||
if (isset($profile->birthdate)) $user->birth = $profile->birthdate;
|
}
|
||||||
if (isset($profile->gender)) $user->gender = $profile->gender;
|
if (isset($profile->about)) {
|
||||||
if (isset($profile->country_code)) $user->country_code = $profile->country_code;
|
$user->about_me = $profile->about;
|
||||||
if (isset($profile->avatar_hash)) $user->icon = $profile->avatar_hash;
|
}
|
||||||
if (isset($profile->cover_image)) $user->picture = $profile->cover_image;
|
if (isset($profile->birthdate)) {
|
||||||
if (isset($profile->signature)) $user->signature = $profile->signature;
|
$user->birth = $profile->birthdate;
|
||||||
if (isset($profile->description)) $user->description = $profile->description;
|
}
|
||||||
|
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) {
|
} catch (\Throwable $e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user