feat(usermanagement): enhance user management features and implement automated tests

- Memperbarui UsersController:
  - Mengaktifkan middleware untuk menginisialisasi pengguna yang terautentikasi.
  - Mengubah nama izin dari pola 'users.*' menjadi 'usermanagement.*' untuk konsistensi.
  - Menggunakan Storage Facade untuk operasi penyimpanan file tanda tangan.
  - Menambahkan validasi untuk direktori sebelum menyimpan file baru.
  - Mengubah metode untuk memberikan respons JSON pada penghapusan pengguna.

- Memperbarui views/users/index.blade.php:
  - Menghapus dropdown filter yang tidak digunakan.
  - Menambahkan tombol Export to Excel dan Add User dengan styling yang diperbarui.

- Menambahkan file `UsersControllerTest` untuk memastikan kelengkapan pengujian:
  - Pengujian CRUD (Create, Read, Update, Delete) pengguna.
  - Pengujian pagination, sorting, dan filtering untuk datatable.
  - Pengujian pengelolaan file tanda tangan pengguna (penyimpanan baru dan penghapusan tanda tangan lama).
  - Pengujian pemulihan untuk soft-deleted users.
  - Pengujian validasi peran dan izin untuk setiap tindakan.

- Memastikan konsistensi dan reliabilitas proses pengelolaan pengguna melalui pengujian otomatis.
This commit is contained in:
Daeng Deni Mardaeni
2025-05-18 20:06:15 +07:00
parent 1e958c9dd7
commit 1968c14f68
3 changed files with 488 additions and 41 deletions

View File

@@ -14,6 +14,7 @@
use Modules\Usermanagement\Http\Requests\User as UserRequest;
use Modules\Usermanagement\Models\Role;
use Modules\Usermanagement\Models\User;
use Illuminate\Support\Facades\Storage;
/**
* Class UsersController
@@ -24,7 +25,7 @@
*/
class UsersController extends Controller
{
/**
/**
* @var \Illuminate\Contracts\Auth\Authenticatable|null
*/
public $user;
@@ -34,13 +35,10 @@
*
* Initializes the user property with the authenticated user.
*/
// public function __construct()
// {
// $this->middleware(function ($request, $next) {
// $this->user = Auth::guard('web')->user();
// return $next($request);
// });
// }
public function __construct()
{
$this->user = Auth::guard('web')->user();
}
/**
* Display a listing of the resource.
@@ -50,7 +48,7 @@
*/
public function index()
{
if (is_null($this->user) || !$this->user->can('users.view')) {
if (is_null($this->user) || !$this->user->can('usermanagement.read')) {
//abort(403, 'Sorry! You are not allowed to view users.');
}
@@ -67,7 +65,7 @@
*/
public function dataForDatatables(Request $request)
{
if (is_null($this->user) || !$this->user->can('users.view')) {
if (is_null($this->user) || !$this->user->can('usermanagement.view')) {
//abort(403, 'Sorry! You are not allowed to view users.');
}
@@ -137,7 +135,7 @@
*/
public function edit($id)
{
if (is_null($this->user) || !$this->user->can('users.edit')) {
if (is_null($this->user) || !$this->user->can('usermanagement.edit')) {
//abort(403, 'Sorry! You are not allowed to edit users.');
}
@@ -157,14 +155,14 @@
*/
public function destroy($id)
{
if (is_null($this->user) || !$this->user->can('users.delete')) {
if (is_null($this->user) || !$this->user->can('usermanagement.delete')) {
//abort(403, 'Sorry! You are not allowed to delete users.');
}
$user = User::find($id);
$user->delete();
echo json_encode(['message' => 'User deleted successfully.', 'success' => true]);
return response()->json(['message' => 'User deleted successfully.', 'success' => true]);
}
/**
@@ -177,7 +175,7 @@
*/
public function restore($id)
{
if (is_null($this->user) || !$this->user->can('users.restore')) {
if (is_null($this->user) || !$this->user->can('usermanagement.restore')) {
abort(403, 'Sorry! You are not allowed to restore users.');
}
@@ -224,7 +222,7 @@
*/
public function create()
{
if (is_null($this->user) || !$this->user->can('users.create')) {
if (is_null($this->user) || !$this->user->can('usermanagement.create')) {
//abort(403, 'Sorry! You are not allowed to create a user.');
}
@@ -262,12 +260,17 @@
if ($request->hasFile('sign')) {
// Delete old e-sign if exists
if ($user->sign) {
Storage::delete('public/signatures/' . $user->id . '/' . $user->sign);
Storage::disk('public')->delete('signatures/' . $user->id . '/' . $user->sign);
}
$sign = $request->file('sign');
$signName = time() . '.' . $sign->getClientOriginalExtension();
$sign->storeAs('public/signatures/' . $user->id, $signName);
// Make sure the directory exists
Storage::disk('public')->makeDirectory('signatures/' . $user->id);
// Store the file
$sign->storeAs('signatures/' . $user->id, $signName, 'public');
$user->sign = $signName;
}
@@ -312,7 +315,7 @@
*/
public function update(UserRequest $request, $id)
{
if (is_null($this->user) || !$this->user->can('users.update')) {
if (is_null($this->user) || !$this->user->can('usermanagement.update')) {
//abort(403, 'Sorry! You are not allowed to update users.');
}