Tambah fungsi update profil dan ganti password

Menambahkan fungsi update profil dan ganti password di UsersController. Menyesuaikan rute dan formulir di tampilan profil untuk mendukung fitur ini.
This commit is contained in:
Daeng Deni Mardaeni
2024-11-17 12:48:52 +07:00
parent a2bff61998
commit 5678255090
3 changed files with 226 additions and 68 deletions

View File

@@ -6,6 +6,8 @@
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Lpj\Models\Branch;
use Modules\Usermanagement\Exports\UsersExport;
@@ -77,8 +79,7 @@
$search = $request->get('search');
$query->where(function ($q) use ($search) {
$q
->where('name', 'LIKE', "%$search%")
->orWhere('email', 'LIKE', "%$search%");
->where('name', 'LIKE', "%$search%")->orWhere('email', 'LIKE', "%$search%");
});
}
@@ -145,51 +146,6 @@
return view('usermanagement::users.create', compact('user', 'roles', 'branches'));
}
/**
* Update the specified resource in storage.
*
* @param \Modules\Usermanagement\Http\Requests\User $request
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(UserRequest $request, $id)
{
if (is_null($this->user) || !$this->user->can('users.update')) {
//abort(403, 'Sorry! You are not allowed to update users.');
}
$validated = $request->validated();
if($validated) {
try{
$user = User::find($id);
if ($request->hasFile('sign')) {
$sign = $request->file('sign');
$signName = time() . '.' . $sign->getClientOriginalExtension();
$sign->storeAs(
'public/signatures/' . $user->id . '/',
$signName,
);
$validated['sign'] = $signName;
}
$user->update($validated);
if ($request->roles) {
$user->roles()->detach();
$user->assignRole($request->roles);
}
} catch (Exception $e) {
return redirect()->back()->withErrors(['error' => 'Failed to update user. Please try again.']);
}
}
return redirect()->route('users.index')->with('success', 'User updated successfully.');
}
/**
* Remove the specified resource from storage.
*
@@ -287,4 +243,106 @@
return view('usermanagement::users.profile', compact('user'));
}
public function updateProfile(Request $request)
{
$user = Auth::user();
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,' . $user->id,
'nik' => 'required|string|max:255|unique:users,nik,' . $user->id,
'sign' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
$user->name = $validatedData['name'];
$user->email = $validatedData['email'];
$user->nik = $validatedData['nik'];
if ($request->hasFile('sign')) {
// Delete old e-sign if exists
if ($user->sign) {
Storage::delete('public/signatures/' . $user->id . '/' . $user->sign);
}
$sign = $request->file('sign');
$signName = time() . '.' . $sign->getClientOriginalExtension();
$sign->storeAs('public/signatures/' . $user->id, $signName);
$user->sign = $signName;
}
$user->save();
return redirect()->route('users.profile')->with('success', 'Profile updated successfully.');
}
public function changePassword(Request $request)
{
$validator = Validator::make($request->all(), [
'current_password' => 'required',
'password' => 'required|string|min:8|confirmed',
], [
'password_confirmation' => 'The new password confirmation does not match.',
]);
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
$user = Auth::user();
if (!Hash::check($request->current_password, $user->password)) {
return back()->withErrors(['current_password' => 'The current password is incorrect.']);
}
$user->password = Hash::make($request->password);
$user->save();
return redirect()->route('users.profile')->with('success', 'Password changed successfully.');
}
/**
* Update the specified resource in storage.
*
* @param \Modules\Usermanagement\Http\Requests\User $request
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(UserRequest $request, $id)
{
if (is_null($this->user) || !$this->user->can('users.update')) {
//abort(403, 'Sorry! You are not allowed to update users.');
}
$validated = $request->validated();
if ($validated) {
try {
$user = User::find($id);
if ($request->hasFile('sign')) {
$sign = $request->file('sign');
$signName = time() . '.' . $sign->getClientOriginalExtension();
$sign->storeAs(
'public/signatures/' . $user->id . '/',
$signName,
);
$validated['sign'] = $signName;
}
$user->update($validated);
if ($request->roles) {
$user->roles()->detach();
$user->assignRole($request->roles);
}
} catch (Exception $e) {
return redirect()->back()->withErrors(['error' => 'Failed to update user. Please try again.']);
}
}
return redirect()->route('users.index')->with('success', 'User updated successfully.');
}
}

View File

@@ -59,6 +59,101 @@
</div>
<!-- End of Container -->
</div>
<div class="container-fluid mt-8 w-full">
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<!-- Edit Profile Form -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Edit Profile</h3>
</div>
<div class="card-body">
<form action="{{ route('users.update-profile') }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="mb-4">
<label for="name" class="form-label">Name</label>
<input type="text" class="input @error('name') border-danger @enderror" id="name" name="name" value="{{ Auth::user()->name }}">
@error('name')
<div class="text-danger mt-2">{{ $message }}</div>
@enderror
</div>
<div class="mb-4">
<label for="email" class="form-label">Email</label>
<input type="email" class="input @error('email') border-danger @enderror" id="email" name="email" value="{{ Auth::user()->email }}">
@error('email')
<div class="text-danger mt-2">{{ $message }}</div>
@enderror
</div>
<div class="mb-4">
<label for="nik" class="form-label">NIK</label>
<input type="text" class="input @error('nik') border-danger @enderror" id="nik" name="nik" value="{{ Auth::user()->nik }}">
@error('nik')
<div class="text-danger mt-2">{{ $message }}</div>
@enderror
</div>
<div class="mb-4">
<label for="sign" class="form-label">E-Sign</label>
<input type="file" class="file-input @error('sign') border-danger @enderror" id="sign" name="sign" accept="image/*">
@if(Auth::user()->sign)
<div class="mt-2">
<p>Current E-Sign:</p>
<img src="{{ asset('storage/signatures/' . Auth::user()->id . '/' . Auth::user()->sign) }}"
alt="E-Sign"
class="mt-2 max-w-xs border border-gray-200 rounded">
</div>
@endif
@error('sign')
<div class="text-danger mt-2">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-primary">Update Profile</button>
</form>
</div>
</div>
<!-- Change Password Form -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Change Password</h3>
</div>
<div class="card-body">
<form action="{{ route('users.change-password') }}" method="POST">
@csrf
@method('PUT')
<div class="mb-4">
<label for="current_password" class="form-label">Current Password</label>
<input type="password" class="input @error('current_password') border-danger @enderror" id="current_password" name="current_password">
@error('current_password')
<div class="text-danger mt-2">{{ $message }}</div>
@enderror
</div>
<div class="mb-4">
<label for="password" class="form-label">New Password</label>
<input type="password" class="input @error('password') border-danger @enderror" id="password" name="password">
@error('password')
<div class="text-danger mt-2">{{ $message }}</div>
@enderror
</div>
<div class="mb-4">
<label for="password_confirmation" class="form-label">Confirm New Password</label>
<input type="password" class="input @error('password_confirmation') border-danger @enderror" id="password_confirmation" name="password_confirmation">
@error('password_confirmation')
<div class="text-danger mt-2">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-primary">Change Password</button>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection

View File

@@ -21,6 +21,11 @@ Route::middleware(['auth'])->group(function () {
Route::get('datatables', [UsersController::class, 'dataForDatatables'])->name('datatables');
Route::get('export', [UsersController::class, 'export'])->name('export');
Route::get('profile', [UsersController::class, 'profile'])->name('profile');
Route::put('/profile/update', [UsersController::class, 'updateProfile'])->name('update-profile');
Route::put('/profile/change-password', [UsersController::class, 'changePassword'])->name(
'change-password',
);
});
Route::resource('users', UsersController::class);