Skip to content

Commit 52ef04d

Browse files
Refactor indexQuery method in KerangkaAcuan.php to filter results based on session year and user role
1 parent 59c6898 commit 52ef04d

File tree

8 files changed

+77
-57
lines changed

8 files changed

+77
-57
lines changed

app/Nova/KerangkaAcuan.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ public static function label()
3333
return 'Kerangka Acuan Kerja';
3434
}
3535

36+
public static function indexQuery(NovaRequest $request, $query)
37+
{
38+
$query->whereYear('tanggal', session('year'));
39+
if (Policy::make()->allowedFor('koordinator,anggota')->get()) {
40+
return $query->where('unit_kerja_id',Helper::getDataPegawaiByUserId($request->user()->id, now())->unit_kerja_id );
41+
}
42+
43+
return $query;
44+
}
45+
3646
/**
3747
* The model the resource corresponds to.
3848
*

app/Policies/BastMitraPolicy.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Policies;
44

55
use App\Helpers\Policy;
6+
use App\Models\BastMitra;
7+
use App\Models\User;
68

79
class BastMitraPolicy
810
{
@@ -19,9 +21,10 @@ public function viewAny(): bool
1921
/**
2022
* Determine whether the user can view the model.
2123
*/
22-
public function view(): bool
24+
public function view(User $user, BastMitra $bast): bool
2325
{
2426
return Policy::make()
27+
->withYear($bast->tahun)
2528
->allowedFor('all')
2629
->get();
2730
}
@@ -39,20 +42,22 @@ public function create(): bool
3942
/**
4043
* Determine whether the user can update the model.
4144
*/
42-
public function update(): bool
45+
public function update(User $user, BastMitra $bast): bool
4346
{
4447
return Policy::make()
4548
->allowedFor('ppk')
49+
->withYear($bast->tahun)
4650
->get();
4751
}
4852

4953
/**
5054
* Determine whether the user can delete the model.
5155
*/
52-
public function delete(): bool
56+
public function delete(User $user, BastMitra $bast): bool
5357
{
5458
return Policy::make()
5559
->allowedFor('ppk')
60+
->withYear($bast->tahun)
5661
->get();
5762
}
5863

app/Policies/HonorKegiatanPolicy.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Policies;
44

55
use App\Helpers\Policy;
6+
use App\Models\HonorKegiatan;
7+
use App\Models\User;
68

79
class HonorKegiatanPolicy
810
{
@@ -19,10 +21,11 @@ public function viewAny(): bool
1921
/**
2022
* Determine whether the user can view the model.
2123
*/
22-
public function view(): bool
24+
public function view(User $user, HonorKegiatan $honor): bool
2325
{
2426
return Policy::make()
2527
->allowedFor('all')
28+
->withYear($honor->tahun)
2629
->get();
2730
}
2831

@@ -37,20 +40,22 @@ public function create(): bool
3740
/**
3841
* Determine whether the user can update the model.
3942
*/
40-
public function update(): bool
43+
public function update(User $user, HonorKegiatan $honor): bool
4144
{
4245
return Policy::make()
4346
->allowedFor('koordinator,anggota')
47+
->withYear($honor->tahun)
4448
->get();
4549
}
4650

4751
/**
4852
* Determine whether the user can delete the model.
4953
*/
50-
public function delete(): bool
54+
public function delete(User $user, HonorKegiatan $honor): bool
5155
{
5256
return Policy::make()
5357
->allowedFor('koordinator,anggota')
58+
->withYear($honor->tahun)
5459
->get();
5560
}
5661

@@ -68,7 +73,7 @@ public function replicate(): bool
6873
public function runAction(): bool
6974
{
7075
return Policy::make()
71-
->allowedFor('all')
76+
->allowedFor('koordinator,anggota')
7277
->get();
7378
}
7479
}

app/Policies/KerangkaAcuanPolicy.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22

33
namespace App\Policies;
44

5+
use App\Helpers\Helper;
56
use App\Helpers\Policy;
7+
use App\Models\KerangkaAcuan;
8+
use App\Models\User;
69

710
class KerangkaAcuanPolicy
811
{
912
/**
1013
* Determine whether the user can view any models.
1114
*/
12-
public function viewAny(): bool
15+
public function viewAny(User $user): bool
1316
{
1417
return Policy::make()
15-
->allowedFor('all')
18+
->notAllowedFor('kepala,admin')
1619
->get();
1720
}
1821

1922
/**
2023
* Determine whether the user can view the model.
2124
*/
22-
public function view(): bool
25+
public function view(User $user, KerangkaAcuan $kerangkaAcuan): bool
2326
{
2427
return Policy::make()
25-
->allowedFor('all')
28+
->notAllowedFor('kepala,admin')
29+
->withYear(Helper::getYearFromDate($kerangkaAcuan->tanggal))
30+
->andEqual($kerangkaAcuan->unit_kerja_id, Helper::getDataPegawaiByUserId($user->id, now())->unit_kerja_id)
2631
->get();
2732
}
2833

2934
/**
3035
* Determine whether the user can create models.
3136
*/
32-
public function create(): bool
37+
public function create(User $user): bool
3338
{
3439
return Policy::make()
3540
->allowedFor('koordinator,anggota')
@@ -39,37 +44,36 @@ public function create(): bool
3944
/**
4045
* Determine whether the user can update the model.
4146
*/
42-
public function update(): bool
47+
public function update(User $user, KerangkaAcuan $kerangkaAcuan): bool
4348
{
4449
return Policy::make()
4550
->allowedFor('koordinator,anggota')
51+
->withYear(Helper::getYearFromDate($kerangkaAcuan->tanggal))
52+
->andEqual($kerangkaAcuan->unit_kerja_id, Helper::getDataPegawaiByUserId($user->id, now())->unit_kerja_id)
4653
->get();
4754
}
4855

4956
/**
5057
* Determine whether the user can delete the model.
5158
*/
52-
public function delete(): bool
59+
public function delete(User $user, KerangkaAcuan $kerangkaAcuan): bool
5360
{
5461
return Policy::make()
5562
->allowedFor('koordinator,anggota')
63+
->withYear(Helper::getYearFromDate($kerangkaAcuan->tanggal))
64+
->andEqual($kerangkaAcuan->unit_kerja_id, Helper::getDataPegawaiByUserId($user->id, now())->unit_kerja_id)
5665
->get();
5766
}
5867

5968
/**
6069
* Determine whether the user can replicate the model.
6170
*/
62-
public function replicate(): bool
71+
public function replicate(User $user, KerangkaAcuan $kerangkaAcuan): bool
6372
{
6473
return Policy::make()
6574
->allowedFor('koordinator,anggota')
66-
->get();
67-
}
68-
69-
public function runAction(): bool
70-
{
71-
return Policy::make()
72-
->allowedFor('all')
75+
->withYear(Helper::getYearFromDate($kerangkaAcuan->tanggal))
76+
->andEqual($kerangkaAcuan->unit_kerja_id, Helper::getDataPegawaiByUserId($user->id, now())->unit_kerja_id)
7377
->get();
7478
}
7579
}

app/Policies/KontrakMitraPolicy.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Policies;
44

55
use App\Helpers\Policy;
6+
use App\Models\KontrakMitra;
7+
use App\Models\User;
68

79
class KontrakMitraPolicy
810
{
@@ -19,9 +21,10 @@ public function viewAny(): bool
1921
/**
2022
* Determine whether the user can view the model.
2123
*/
22-
public function view(): bool
24+
public function view(User $user, KontrakMitra $kontrak): bool
2325
{
2426
return Policy::make()
27+
->withYear($kontrak->tahun)
2528
->allowedFor('all')
2629
->get();
2730
}
@@ -39,20 +42,22 @@ public function create(): bool
3942
/**
4043
* Determine whether the user can update the model.
4144
*/
42-
public function update(): bool
45+
public function update(User $user, KontrakMitra $kontrak): bool
4346
{
4447
return Policy::make()
4548
->allowedFor('ppk')
49+
->withYear($kontrak->tahun)
4650
->get();
4751
}
4852

4953
/**
5054
* Determine whether the user can delete the model.
5155
*/
52-
public function delete(): bool
56+
public function delete(User $user, KontrakMitra $kontrak): bool
5357
{
5458
return Policy::make()
5559
->allowedFor('ppk')
60+
->withYear($kontrak->tahun)
5661
->get();
5762
}
5863

app/Policies/NaskahKeluarPolicy.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace App\Policies;
44

5+
use App\Helpers\Helper;
56
use App\Helpers\Policy;
7+
use App\Models\NaskahKeluar;
8+
use App\Models\User;
69

710
class NaskahKeluarPolicy
811
{
@@ -19,9 +22,10 @@ public function viewAny(): bool
1922
/**
2023
* Determine whether the user can view the model.
2124
*/
22-
public function view(): bool
25+
public function view(User $user, NaskahKeluar $naskahKeluar): bool
2326
{
2427
return Policy::make()
28+
->withYear(Helper::getYearFromDate($naskahKeluar->tanggal))
2529
->allowedFor('all')
2630
->get();
2731
}
@@ -39,20 +43,22 @@ public function create(): bool
3943
/**
4044
* Determine whether the user can update the model.
4145
*/
42-
public function update(): bool
46+
public function update(User $user, NaskahKeluar $naskahKeluar): bool
4347
{
4448
return Policy::make()
4549
->allowedFor('all')
50+
->withYear(Helper::getYearFromDate($naskahKeluar->tanggal))
4651
->get();
4752
}
4853

4954
/**
5055
* Determine whether the user can delete the model.
5156
*/
52-
public function delete(): bool
57+
public function delete(User $user, NaskahKeluar $naskahKeluar): bool
5358
{
5459
return Policy::make()
5560
->allowedFor('all')
61+
->withYear(Helper::getYearFromDate($naskahKeluar->tanggal))
5662
->get();
5763
}
5864

app/Policies/NaskahMasukPolicy.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace App\Policies;
44

5+
use App\Helpers\Helper;
56
use App\Helpers\Policy;
7+
use App\Models\NaskahMasuk;
8+
use App\Models\User;
69

710
class NaskahMasukPolicy
811
{
@@ -19,9 +22,10 @@ public function viewAny(): bool
1922
/**
2023
* Determine whether the user can view the model.
2124
*/
22-
public function view(): bool
25+
public function view(User $user, NaskahMasuk $naskahMasuk): bool
2326
{
2427
return Policy::make()
28+
->withYear(Helper::getYearFromDate($naskahMasuk->tanggal))
2529
->allowedFor('all')
2630
->get();
2731
}
@@ -39,20 +43,22 @@ public function create(): bool
3943
/**
4044
* Determine whether the user can update the model.
4145
*/
42-
public function update(): bool
46+
public function update(User $user, NaskahMasuk $naskahMasuk): bool
4347
{
4448
return Policy::make()
4549
->allowedFor('all')
50+
->withYear(Helper::getYearFromDate($naskahMasuk->tanggal))
4651
->get();
4752
}
4853

4954
/**
5055
* Determine whether the user can delete the model.
5156
*/
52-
public function delete(): bool
57+
public function delete(User $user, NaskahMasuk $naskahMasuk): bool
5358
{
5459
return Policy::make()
5560
->allowedFor('all')
61+
->withYear(Helper::getYearFromDate($naskahMasuk->tanggal))
5662
->get();
5763
}
5864

0 commit comments

Comments
 (0)