188 lines
7.0 KiB
PHP
188 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\ProfileUpdateRequest;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Validation\Rules\Password as PasswordRule;
|
|
|
|
class ProfileController extends Controller
|
|
{
|
|
/**
|
|
* Display the user's profile form.
|
|
*/
|
|
public function edit(Request $request): View
|
|
{
|
|
return view('profile.edit', [
|
|
'user' => $request->user(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the user's profile information.
|
|
*/
|
|
public function update(ProfileUpdateRequest $request, \App\Services\AvatarService $avatarService): RedirectResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
// Core fields
|
|
$validated = $request->validated();
|
|
|
|
logger()->debug('Profile update validated data', $validated);
|
|
|
|
// Username is read-only and must not be changed here.
|
|
// Use `name` for the real/display name field.
|
|
if (isset($validated['name'])) {
|
|
$user->name = $validated['name'];
|
|
}
|
|
|
|
// Only allow setting email when we don't have one yet (legacy users)
|
|
if (!empty($validated['email']) && empty($user->email)) {
|
|
$user->email = $validated['email'];
|
|
$user->email_verified_at = null;
|
|
}
|
|
|
|
$user->save();
|
|
|
|
// Profile fields - target columns in `user_profiles` per spec
|
|
$profileUpdates = [];
|
|
if (!empty($validated['about'])) $profileUpdates['about'] = $validated['about'];
|
|
|
|
// website / legacy homepage
|
|
if (!empty($validated['web'])) {
|
|
$profileUpdates['website'] = $validated['web'];
|
|
} elseif (!empty($validated['homepage'])) {
|
|
$profileUpdates['website'] = $validated['homepage'];
|
|
}
|
|
|
|
// Birthday -> store as birthdate
|
|
$day = $validated['day'] ?? null;
|
|
$month = $validated['month'] ?? null;
|
|
$year = $validated['year'] ?? null;
|
|
if ($year && $month && $day) {
|
|
$profileUpdates['birthdate'] = sprintf('%04d-%02d-%02d', (int)$year, (int)$month, (int)$day);
|
|
}
|
|
|
|
// Gender normalization -> store as provided normalized value
|
|
if (!empty($validated['gender'])) {
|
|
$g = strtolower($validated['gender']);
|
|
$map = ['m' => 'M', 'f' => 'F', 'n' => 'X', 'x' => 'X'];
|
|
$profileUpdates['gender'] = $map[$g] ?? strtoupper($validated['gender']);
|
|
}
|
|
|
|
if (!empty($validated['country'])) $profileUpdates['country_code'] = $validated['country'];
|
|
|
|
// Mailing and notify flags: normalize true/false when saving
|
|
if (array_key_exists('mailing', $validated)) {
|
|
$profileUpdates['mlist'] = filter_var($validated['mailing'], FILTER_VALIDATE_BOOLEAN) ? 1 : 0;
|
|
}
|
|
if (array_key_exists('notify', $validated)) {
|
|
$profileUpdates['friend_upload_notice'] = filter_var($validated['notify'], FILTER_VALIDATE_BOOLEAN) ? 1 : 0;
|
|
}
|
|
|
|
// signature/description should be stored in their own columns
|
|
if (isset($validated['signature'])) $profileUpdates['signature'] = $validated['signature'];
|
|
if (isset($validated['description'])) $profileUpdates['description'] = $validated['description'];
|
|
|
|
// 'about' direct field (ensure explicit about wins when provided)
|
|
if (isset($validated['about'])) $profileUpdates['about'] = $validated['about'];
|
|
|
|
// Files: avatar -> use AvatarService, emoticon and photo -> store to public disk
|
|
if ($request->hasFile('avatar')) {
|
|
try {
|
|
$hash = $avatarService->storeFromUploadedFile($user->id, $request->file('avatar'));
|
|
// store returned hash into profile avatar column
|
|
if (!empty($hash)) {
|
|
$profileUpdates['avatar'] = $hash;
|
|
}
|
|
} catch (\Exception $e) {
|
|
return Redirect::back()->with('error', 'Avatar processing failed: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
if ($request->hasFile('emoticon')) {
|
|
$file = $request->file('emoticon');
|
|
$fname = $file->getClientOriginalName();
|
|
$path = \Illuminate\Support\Facades\Storage::disk('public')->putFileAs('user-emoticons/'.$user->id, $file, $fname);
|
|
try {
|
|
\Illuminate\Support\Facades\DB::table('users')->where('id', $user->id)->update(['eicon' => $fname]);
|
|
} catch (\Exception $e) {}
|
|
}
|
|
|
|
if ($request->hasFile('photo')) {
|
|
$file = $request->file('photo');
|
|
$fname = $file->getClientOriginalName();
|
|
$path = \Illuminate\Support\Facades\Storage::disk('public')->putFileAs('user-picture/'.$user->id, $file, $fname);
|
|
// store cover image filename in user_profiles.cover_image (fallback to users.picture)
|
|
if (\Illuminate\Support\Facades\Schema::hasTable('user_profiles')) {
|
|
$profileUpdates['cover_image'] = $fname;
|
|
} else {
|
|
try {
|
|
\Illuminate\Support\Facades\DB::table('users')->where('id', $user->id)->update(['picture' => $fname]);
|
|
} catch (\Exception $e) {}
|
|
}
|
|
}
|
|
|
|
// Persist profile updates now that files (avatar/cover) have been handled
|
|
try {
|
|
if (\Illuminate\Support\Facades\Schema::hasTable('user_profiles')) {
|
|
if (!empty($profileUpdates)) {
|
|
\Illuminate\Support\Facades\DB::table('user_profiles')->updateOrInsert(['user_id' => $user->id], $profileUpdates);
|
|
}
|
|
} else {
|
|
if (!empty($profileUpdates)) {
|
|
\Illuminate\Support\Facades\DB::table('users')->where('id', $user->id)->update($profileUpdates);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
logger()->error('Profile update error: '.$e->getMessage());
|
|
}
|
|
|
|
return Redirect::to('/user')->with('status', 'profile-updated');
|
|
}
|
|
|
|
/**
|
|
* Delete the user's account.
|
|
*/
|
|
public function destroy(Request $request): RedirectResponse
|
|
{
|
|
$request->validateWithBag('userDeletion', [
|
|
'password' => ['required', 'current_password'],
|
|
]);
|
|
|
|
$user = $request->user();
|
|
|
|
Auth::logout();
|
|
|
|
// Soft-delete the user (preserve record) — align with soft-delete policy.
|
|
$user->delete();
|
|
|
|
$request->session()->invalidate();
|
|
$request->session()->regenerateToken();
|
|
|
|
return Redirect::to('/');
|
|
}
|
|
|
|
/**
|
|
* Update the user's password.
|
|
*/
|
|
public function password(Request $request): RedirectResponse
|
|
{
|
|
$request->validate([
|
|
'current_password' => ['required', 'current_password'],
|
|
'password' => ['required', 'confirmed', PasswordRule::min(8)],
|
|
]);
|
|
|
|
$user = $request->user();
|
|
$user->password = Hash::make($request->input('password'));
|
|
$user->save();
|
|
|
|
return Redirect::to('/user')->with('status', 'password-updated');
|
|
}
|
|
}
|