Superadmin boleh padam pengguna
- Butang Padam dalam senarai pengguna (dengan pengesahan) - Tidak boleh padam akaun sendiri — dipapar '(akaun anda)' - Tidak boleh padam pengguna yang telah mencatat rekod kehadiran (disokong FK dicatat_oleh_user_id di peringkat DB) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\KehadiranKtm;
|
||||
use App\Models\Sppm\Dun;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -65,4 +66,21 @@ class PenggunaController extends Controller
|
||||
|
||||
return redirect()->route('pengguna.index')->with('status', 'Pengguna baharu berjaya didaftarkan.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, User $pengguna)
|
||||
{
|
||||
if ($pengguna->id === $request->user()->id) {
|
||||
return back()->withErrors(['pengguna' => 'Anda tidak boleh memadam akaun anda sendiri.']);
|
||||
}
|
||||
|
||||
if (KehadiranKtm::where('dicatat_oleh_user_id', $pengguna->id)->exists()) {
|
||||
return back()->withErrors([
|
||||
'pengguna' => "Pengguna {$pengguna->name} tidak boleh dipadam kerana telah mencatat rekod kehadiran.",
|
||||
]);
|
||||
}
|
||||
|
||||
$pengguna->delete();
|
||||
|
||||
return redirect()->route('pengguna.index')->with('status', "Pengguna {$pengguna->name} telah dipadam.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,10 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if ($errors->any())
|
||||
<div class="makluman makluman-ralat">{{ $errors->first() }}</div>
|
||||
@endif
|
||||
|
||||
@if ($pengguna->isEmpty())
|
||||
<div class="kad teks-tengah">
|
||||
<p>Tiada pengguna lagi.</p>
|
||||
@@ -23,6 +27,7 @@
|
||||
<th>E-mel</th>
|
||||
<th>Peranan</th>
|
||||
<th>DUN</th>
|
||||
<th>Tindakan</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -38,6 +43,18 @@
|
||||
} }}
|
||||
</td>
|
||||
<td>{{ $p->dun ? $p->dun->code . ' — ' . $p->dun->nama : '-' }}</td>
|
||||
<td>
|
||||
@if ($p->id === auth()->id())
|
||||
<span class="teks-kecil">(akaun anda)</span>
|
||||
@else
|
||||
<form method="POST" action="{{ route('pengguna.destroy', $p) }}" style="margin:0"
|
||||
onsubmit="return confirm('Padam pengguna {{ $p->name }}? Tindakan ini tidak boleh dikembalikan.');">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="btn btn-kecil btn-merah">Padam</button>
|
||||
</form>
|
||||
@endif
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
|
||||
@@ -44,5 +44,6 @@ Route::middleware(['auth'])->group(function () {
|
||||
Route::get('/pengguna', [PenggunaController::class, 'index'])->name('pengguna.index');
|
||||
Route::get('/pengguna/cipta', [PenggunaController::class, 'create'])->name('pengguna.create');
|
||||
Route::post('/pengguna', [PenggunaController::class, 'store'])->name('pengguna.store');
|
||||
Route::delete('/pengguna/{pengguna}', [PenggunaController::class, 'destroy'])->name('pengguna.destroy');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user