429 lines
15 KiB
PHP
429 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Manajemen;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use App\Models\Verifikator\Team;
|
|
use App\Models\Verifikator\TeamUnitKerja;
|
|
use App\Models\Verifikator\UnitKerjaPegawai;
|
|
use App\Models\Verifikator\View\Pegawai;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Throwable;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class TeamController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
if (! isSuperuser()) {
|
|
$canReadTeam = auth()->user()->can('Baca Team');
|
|
$canReadAnggota = auth()->user()->can('Baca Team Anggota');
|
|
|
|
if (! $canReadTeam && $canReadAnggota) {
|
|
return redirect()->route('manajemen.team.detail', ['team' => session('team_id')]);
|
|
}
|
|
|
|
abort_if(! $canReadTeam, 403);
|
|
}
|
|
|
|
$title = 'Team';
|
|
|
|
$grouped = Permission::get()->groupBy('type');
|
|
$order = ['Create' => 1, 'Read' => 2, 'Update' => 3, 'Delete' => 4];
|
|
$permissions = $grouped->sortBy(function ($item, $key) use ($order) {
|
|
return $order[$key] ?? 999;
|
|
});
|
|
|
|
$units = UnitKerjaPegawai::orderBy('id')->get()->prepend(new UnitKerjaPegawai(['id' => 0, 'nama' => 'Universitas']));
|
|
|
|
return view('manajemen.team.index', compact('title', 'permissions', 'units'));
|
|
}
|
|
|
|
public function data()
|
|
{
|
|
if (isSuperuser()) {
|
|
$teams = Team::orderBy('id')->get();
|
|
} else {
|
|
abort_if(! auth()->user()->can('Baca Team'), 403);
|
|
|
|
$activeTeam = Team::with('teamUnitKerjas')->findOrFail(session('team_id'));
|
|
$unitKerjaIds = $activeTeam->teamUnitKerjas->pluck('unit_kerja_id');
|
|
|
|
if ($unitKerjaIds->contains(null)) {
|
|
$teams = Team::orderBy('id')->get();
|
|
} else {
|
|
$teams = Team::whereIn('unit_kerja_id', $unitKerjaIds)->orderBy('id')->get();
|
|
}
|
|
}
|
|
|
|
$canEdit = auth()->user()->can('Ubah Team');
|
|
$canDelete = auth()->user()->can('Hapus Team');
|
|
$canReadAnggota = auth()->user()->can('Baca Team Anggota');
|
|
|
|
return DataTables::of($teams)
|
|
->addIndexColumn()
|
|
->addColumn('action', function ($row) use ($canEdit, $canDelete, $canReadAnggota) {
|
|
$items = '';
|
|
|
|
if ($canReadAnggota) {
|
|
$items .= '
|
|
<li>
|
|
<a href="'.route('manajemen.team.detail', $row->id).'" class="dropdown-item">
|
|
<i class="uil-eye" style="margin-right: 5px"></i> Detail
|
|
</a>
|
|
</li>';
|
|
}
|
|
|
|
if ($canEdit) {
|
|
$items .= '
|
|
<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) {
|
|
$items .= '
|
|
<li>
|
|
<a href="javascript:void(0);" class="dropdown-item delete-btn" data-id="'.$row->id.'">
|
|
<i class="uil-trash-alt" style="margin-right: 5px"></i> Hapus
|
|
</a>
|
|
</li>';
|
|
}
|
|
|
|
return '
|
|
<div class="dropdown">
|
|
<span id="dropdownMenuButton" data-bs-toggle="dropdown" style="cursor: pointer;">
|
|
<i class="uil-ellipsis-v"></i>
|
|
</span>
|
|
<ul class="dropdown-menu">'.$items.'
|
|
</ul>
|
|
</div>
|
|
';
|
|
})
|
|
->escapeColumns([])
|
|
->make(true);
|
|
}
|
|
|
|
public function getTeamData(Team $team)
|
|
{
|
|
$team->load('permissions');
|
|
$grouped = Permission::get()->groupBy('type');
|
|
|
|
$order = ['Create' => 1, 'Read' => 2, 'Update' => 3, 'Delete' => 4];
|
|
$permissions = $grouped->sortBy(function ($item, $key) use ($order) {
|
|
return $order[$key] ?? 999;
|
|
});
|
|
|
|
$units = UnitKerjaPegawai::orderBy('id')->get()->prepend(new UnitKerjaPegawai(['id' => 0, 'nama' => 'Universitas']));
|
|
|
|
return response()->json([
|
|
'team' => $team,
|
|
'permissions' => $permissions,
|
|
'units' => $units,
|
|
'unit_kerja_ids' => $team->teamUnitKerjas()->pluck('unit_kerja_id'),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
abort_if(! auth()->user()->can('Tambah Team'), 403);
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$team = Team::create([
|
|
'nama' => $request->name,
|
|
'unit_kerja_id' => $request->unit_kerja ?: null,
|
|
]);
|
|
$team->permissions()->sync($request->permissions);
|
|
$this->syncTeamUnitKerjas($team, $request->unit_kerja_ids ?? []);
|
|
DB::commit();
|
|
|
|
// Check if AJAX request
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => true, 'message' => 'Team berhasil ditambahkan', 'team_id' => $team->id]);
|
|
}
|
|
|
|
return redirect()->route('manajemen.team.detail', ['team' => $team->id])->with('success', 'Team berhasil ditambahkan');
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
|
|
// Check if AJAX request
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => false, 'message' => 'Team gagal ditambahkan: '.$th->getMessage()], 500);
|
|
}
|
|
|
|
return redirect()->back()->with('error', 'Team gagal ditambahkan: '.$th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function detail(Team $team)
|
|
{
|
|
if (! isSuperuser()) {
|
|
$canReadTeam = auth()->user()->can('Baca Team');
|
|
$canReadAnggota = auth()->user()->can('Baca Team Anggota');
|
|
|
|
abort_if(! $canReadTeam && ! $canReadAnggota, 403);
|
|
|
|
// Tanpa 'Baca Team', hanya boleh lihat team sendiri
|
|
if (! $canReadTeam && $team->id != session('team_id')) {
|
|
return redirect()->route('manajemen.team.detail', ['team' => session('team_id')]);
|
|
}
|
|
|
|
// Dengan 'Baca Team', cek scope unit kerja untuk team lain
|
|
if ($canReadTeam && $team->id != session('team_id')) {
|
|
$activeTeam = Team::with('teamUnitKerjas')->findOrFail(session('team_id'));
|
|
$unitKerjaIds = $activeTeam->teamUnitKerjas->pluck('unit_kerja_id');
|
|
|
|
if (! $unitKerjaIds->contains(null) && ! $unitKerjaIds->contains($team->unit_kerja_id)) {
|
|
abort(403);
|
|
}
|
|
}
|
|
}
|
|
|
|
$title = 'Detail Team';
|
|
|
|
$originalTeamId = session('team_id');
|
|
|
|
setPermissionsTeamId($team->id);
|
|
$team->load('users.permissions', 'teamUnitKerjas.unitKerja');
|
|
$permissions = Permission::where('id', '>', 4)->get();
|
|
|
|
// Kembalikan team_id asli agar @can dalam view bekerja dengan benar
|
|
setPermissionsTeamId($originalTeamId);
|
|
|
|
return view('manajemen.team.detail', compact('title', 'team', 'permissions'));
|
|
}
|
|
|
|
public function dataAnggota($teamId)
|
|
{
|
|
$team = Team::findOrFail($teamId);
|
|
|
|
if (! isSuperuser()) {
|
|
abort_if(! auth()->user()->can('Baca Team Anggota'), 403);
|
|
|
|
$isOwnTeam = $team->id == session('team_id');
|
|
|
|
if (! $isOwnTeam) {
|
|
if (! auth()->user()->can('Baca Team')) {
|
|
return response()->json(['error' => 'Unauthorized'], 403);
|
|
}
|
|
|
|
$activeTeam = Team::with('teamUnitKerjas')->findOrFail(session('team_id'));
|
|
$unitKerjaIds = $activeTeam->teamUnitKerjas->pluck('unit_kerja_id');
|
|
|
|
if (! $unitKerjaIds->contains(null) && ! $unitKerjaIds->contains($team->unit_kerja_id)) {
|
|
return response()->json(['error' => 'Unauthorized'], 403);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Simpan team_id asli untuk Superuser
|
|
$originalTeamId = session('team_id');
|
|
|
|
setPermissionsTeamId($team->id);
|
|
$team->load('users.permissions');
|
|
|
|
// Filter users yang memiliki permissions selain readonly, deduplikasi by user id, sort by name
|
|
$members = $team->users->unique('id')->filter(function ($member) {
|
|
return $member->permissions->whereNotIn('id', config('permission.readonly', []))->count() > 0;
|
|
})->sortBy('name')->values();
|
|
|
|
// Kembalikan team_id asli agar can() diperiksa dalam konteks team sendiri
|
|
setPermissionsTeamId($originalTeamId);
|
|
|
|
$canEditAnggota = auth()->user()->can('Ubah Team Anggota');
|
|
$canDeleteAnggota = auth()->user()->can('Hapus Team Anggota');
|
|
|
|
return DataTables::of($members)
|
|
->addColumn('nip', function ($member) {
|
|
return '<code style="font-family:monospace;font-size:1em;background:transparent;color:inherit;">'.e($member->username).'</code>';
|
|
})
|
|
->addColumn('nama', function ($member) {
|
|
return $member->name ?? $member->username;
|
|
})
|
|
->addColumn('jumlah_akses', function ($member) {
|
|
return '<span class="text-primary" style="font-weight: bold;">'.$member->permissions->count().' Permission</span>';
|
|
})
|
|
->addColumn('action', function ($member) use ($canEditAnggota, $canDeleteAnggota) {
|
|
$items = '';
|
|
|
|
if ($canEditAnggota) {
|
|
$items .= '
|
|
<li>
|
|
<a href="javascript:void(0);" class="dropdown-item edit-member-btn" data-id="'.$member->id.'">
|
|
<i class="uil-edit" style="margin-right: 5px"></i> Edit Akses
|
|
</a>
|
|
</li>';
|
|
}
|
|
|
|
if ($canDeleteAnggota) {
|
|
$items .= '
|
|
<li>
|
|
<a href="javascript:void(0);" class="dropdown-item delete-member-btn" data-id="'.$member->id.'">
|
|
<i class="uil-trash-alt" style="margin-right: 5px"></i> Hapus
|
|
</a>
|
|
</li>';
|
|
}
|
|
|
|
return '
|
|
<div class="dropdown">
|
|
<span id="dropdownMenuButton" data-bs-toggle="dropdown" style="cursor: pointer;">
|
|
<i class="uil-ellipsis-v"></i>
|
|
</span>
|
|
<ul class="dropdown-menu">'.$items.'
|
|
</ul>
|
|
</div>
|
|
';
|
|
})
|
|
->escapeColumns([])
|
|
->make(true);
|
|
}
|
|
|
|
public function update(Request $request, Team $team)
|
|
{
|
|
abort_if(! auth()->user()->can('Ubah Team'), 403);
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$team->update([
|
|
'nama' => $request->name,
|
|
'unit_kerja_id' => $request->unit_kerja ?: null,
|
|
]);
|
|
$team->permissions()->sync($request->permissions);
|
|
$this->syncTeamUnitKerjas($team, $request->unit_kerja_ids ?? []);
|
|
DB::commit();
|
|
|
|
// Check if AJAX request
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => true, 'message' => 'Team berhasil diupdate']);
|
|
}
|
|
|
|
return redirect()->back()->with('success', 'Team berhasil diupdate');
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
|
|
// Check if AJAX request
|
|
if ($request->ajax()) {
|
|
return response()->json(['success' => false, 'message' => 'Team gagal diupdate: '.$th->getMessage()], 500);
|
|
}
|
|
|
|
return redirect()->back()->with('error', 'Team gagal diupdate');
|
|
}
|
|
}
|
|
|
|
public function destroy(Team $team)
|
|
{
|
|
abort_if(! auth()->user()->can('Hapus Team'), 403);
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// Hapus relasi dengan permissions
|
|
$team->permissions()->detach();
|
|
|
|
// Hapus relasi dengan users
|
|
$team->users()->detach();
|
|
|
|
// Hapus team
|
|
$team->delete();
|
|
|
|
DB::commit();
|
|
|
|
return response()->json(['success' => true, 'message' => 'Team berhasil dihapus']);
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
|
|
return response()->json(['success' => false, 'message' => 'Team gagal dihapus: '.$th->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function selectTeam(Request $request)
|
|
{
|
|
session(['team_id' => $request->team]);
|
|
|
|
return redirect()->route('dashboard');
|
|
}
|
|
|
|
public function getPegawaisData($teamId)
|
|
{
|
|
setPermissionsTeamId(1);
|
|
|
|
if (! isSuperuser()) {
|
|
setPermissionsTeamId(session('team_id'));
|
|
}
|
|
|
|
$team = Team::findOrFail($teamId);
|
|
|
|
// username di users = nip_siakad di pegawai (kunci yang konsisten)
|
|
$registeredNipSiakads = $team->users()->pluck('username')->filter()->values();
|
|
|
|
$pegawais = Pegawai::when(! isSuperuser(), function ($query) {
|
|
$unit = Team::find(session('team_id'))->unit_kerja_id;
|
|
$query->when($unit > 0, function ($subquery) use ($unit) {
|
|
$subquery->whereHas('jabatan.detail', function ($query) use ($unit) {
|
|
$query->where('unit_kerja_id', $unit);
|
|
});
|
|
});
|
|
})
|
|
->whereNotIn('nip_siakad', $registeredNipSiakads)
|
|
->orderBy('nama')
|
|
->get();
|
|
|
|
$permissions = $team->permissions->groupBy('type');
|
|
|
|
return response()->json([
|
|
'pegawais' => $pegawais,
|
|
'permissions' => $permissions,
|
|
]);
|
|
}
|
|
|
|
public function getMemberData($teamId, $memberId)
|
|
{
|
|
$team = Team::findOrFail($teamId);
|
|
|
|
if (! isSuperuser() && $team->id != session('team_id')) {
|
|
return response()->json(['error' => 'Unauthorized'], 403);
|
|
}
|
|
|
|
setPermissionsTeamId($teamId);
|
|
$member = User::findOrFail($memberId);
|
|
$member->load('permissions');
|
|
|
|
$permissions = $team->permissions->groupBy('type');
|
|
setPermissionsTeamId(session('team_id'));
|
|
|
|
return response()->json([
|
|
'member' => $member,
|
|
'permissions' => $permissions,
|
|
]);
|
|
}
|
|
|
|
private function syncTeamUnitKerjas(Team $team, array $unitKerjaIds): void
|
|
{
|
|
$team->teamUnitKerjas()->delete();
|
|
|
|
if (empty($unitKerjaIds)) {
|
|
TeamUnitKerja::create([
|
|
'team_id' => $team->id,
|
|
'unit_kerja_id' => null,
|
|
]);
|
|
|
|
return;
|
|
}
|
|
|
|
foreach ($unitKerjaIds as $unitKerjaId) {
|
|
TeamUnitKerja::create([
|
|
'team_id' => $team->id,
|
|
'unit_kerja_id' => (int) $unitKerjaId,
|
|
]);
|
|
}
|
|
}
|
|
}
|