166 lines
5.5 KiB
PHP
166 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Manajemen;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Verifikator\Permission;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class PermissionController extends Controller
|
|
{
|
|
public function index(): View
|
|
{
|
|
abort_if(! auth()->user()->canAny(['Baca Permission', 'Tambah Permission', 'Ubah Permission', 'Hapus Permission']), 403);
|
|
|
|
$title = 'Permission';
|
|
$canTambah = auth()->user()->can('Tambah Permission');
|
|
$canAksi = auth()->user()->canAny(['Ubah Permission', 'Hapus Permission']);
|
|
|
|
return view('manajemen.permission.index', compact('title', 'canTambah', 'canAksi'));
|
|
}
|
|
|
|
public function getData(Request $request): JsonResponse
|
|
{
|
|
abort_if(! auth()->user()->can('Baca Permission'), 403);
|
|
|
|
$canEdit = auth()->user()->can('Ubah Permission');
|
|
$canDelete = auth()->user()->can('Hapus Permission');
|
|
|
|
$data = Permission::select(['id', 'name', 'guard_name', 'type'])
|
|
->orderBy('name', 'asc');
|
|
|
|
return DataTables::of($data)
|
|
->addIndexColumn()
|
|
->editColumn('type', function ($row) {
|
|
return $row->type ? ucfirst($row->type) : '-';
|
|
})
|
|
->addColumn('action', function ($row) use ($canEdit, $canDelete) {
|
|
$actions = '';
|
|
|
|
if ($canEdit) {
|
|
$actions .= '<li>
|
|
<a href="javascript:void(0);" class="dropdown-item edit-btn" data-id="'.$row->id.'">
|
|
<i class="uil-edit" style="margin-right: 5px"></i> Edit
|
|
</a>
|
|
</li>';
|
|
}
|
|
|
|
if ($canDelete) {
|
|
$actions .= '<li>
|
|
<a href="javascript:void(0);" class="dropdown-item delete-btn" data-id="'.$row->id.'" data-name="'.$row->name.'">
|
|
<i class="uil-trash-alt" style="margin-right: 5px"></i> Hapus
|
|
</a>
|
|
</li>';
|
|
}
|
|
|
|
if (! $actions) {
|
|
return '-';
|
|
}
|
|
|
|
return '
|
|
<div class="dropdown">
|
|
<span id="dropdownMenuButton" data-bs-toggle="dropdown">
|
|
<i class="uil-ellipsis-v"></i>
|
|
</span>
|
|
<ul class="dropdown-menu">
|
|
'.$actions.'
|
|
</ul>
|
|
</div>
|
|
';
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
abort_if(! auth()->user()->can('Tambah Permission'), 403);
|
|
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'guard_name' => 'required|string|max:255',
|
|
'type' => 'required|in:Create,Read,Update,Delete',
|
|
]);
|
|
|
|
// Check if permission already exists
|
|
$exists = Permission::where('name', $request->name)->exists();
|
|
if ($exists) {
|
|
return response()->json(['success' => false, 'message' => 'Permission dengan nama tersebut sudah ada'], 422);
|
|
}
|
|
|
|
Permission::create([
|
|
'name' => $request->name,
|
|
'guard_name' => $request->guard_name,
|
|
'type' => $request->type,
|
|
]);
|
|
|
|
return response()->json(['success' => true, 'message' => 'Permission berhasil ditambahkan']);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$permission = Permission::findOrFail($id);
|
|
|
|
return response()->json([
|
|
'id' => $permission->id,
|
|
'name' => $permission->name,
|
|
'guard_name' => $permission->guard_name,
|
|
'type' => $permission->type,
|
|
]);
|
|
}
|
|
|
|
public function update(Request $request, $id): JsonResponse
|
|
{
|
|
abort_if(! auth()->user()->can('Ubah Permission'), 403);
|
|
|
|
$permission = Permission::findOrFail($id);
|
|
|
|
$request->validate([
|
|
'type' => 'required|in:Create,Read,Update,Delete',
|
|
]);
|
|
|
|
$permission->update([
|
|
'type' => $request->type,
|
|
]);
|
|
|
|
return response()->json(['success' => true, 'message' => 'Permission berhasil diupdate']);
|
|
}
|
|
|
|
public function destroy($id): JsonResponse
|
|
{
|
|
abort_if(! auth()->user()->can('Hapus Permission'), 403);
|
|
|
|
$permission = Permission::findOrFail($id);
|
|
|
|
$usedByUsers = \DB::table('verifikator.model_has_permissions')
|
|
->where('permission_id', $permission->id)
|
|
->count();
|
|
|
|
$usedByRoles = \DB::table('verifikator.role_has_permissions')
|
|
->where('permission_id', $permission->id)
|
|
->count();
|
|
|
|
if ($usedByUsers > 0 || $usedByRoles > 0) {
|
|
$details = [];
|
|
if ($usedByUsers > 0) {
|
|
$details[] = "{$usedByUsers} user";
|
|
}
|
|
if ($usedByRoles > 0) {
|
|
$details[] = "{$usedByRoles} role";
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Permission tidak dapat dihapus karena masih digunakan oleh '.implode(' dan ', $details).'.',
|
|
], 422);
|
|
}
|
|
|
|
$permission->delete();
|
|
|
|
return response()->json(['success' => true, 'message' => 'Permission berhasil dihapus']);
|
|
}
|
|
}
|