Skip to content

Commit e0da939

Browse files
committed
Add policy
1 parent 79ec4f5 commit e0da939

File tree

11 files changed

+476
-23
lines changed

11 files changed

+476
-23
lines changed

.tinkerun/inspiring.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use App\Models\UnitKerja;
1010
use App\Models\User;
1111
use Illuminate\Support\Facades\Auth;
12+
use Illuminate\Support\Facades\DB;
1213
use Illuminate\Support\Facades\Schema;
1314
use Laravel\Nova\Nova;
1415

1516

1617

1718

19+
1820
// $kodes = KodeArsip::cache()->get('all')->all();
1921

2022
// function setOptions($collection, $value, $label, $group='')
@@ -57,7 +59,9 @@
5759

5860
// }
5961
// $b = nomor('2024','6',1,1,'B');
60-
User::cache()->get('all')->where('role','koordinator');
62+
User::cache()->get('all')->where('unit_kerja_id',1)->pluck('id')->toArray();
63+
64+
6165

6266

6367

app/Models/IzinKeluar.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class IzinKeluar extends Model
1616
protected $casts = [
1717
'tanggal' => 'date',
1818
];
19-
2019
/**
2120
* The "booted" method of the model.
2221
*/

app/Nova/IzinKeluar.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ public static function label()
4747
'kegiatan',
4848
];
4949

50+
/**
51+
* Build an "index" query for the given resource.
52+
*
53+
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
54+
* @param \Illuminate\Database\Eloquent\Builder $query
55+
* @return \Illuminate\Database\Eloquent\Builder
56+
*/
57+
public static function indexQuery(NovaRequest $request, $query)
58+
{
59+
$users = User::cache()->get('all')->where('unit_kerja_id',$request->user()->unit_kerja_id)->pluck('id')->toArray();
60+
if (session('role') === 'anggota')
61+
return $query->where('user_id', $request->user()->id);
62+
if (session('role') === 'koordinator')
63+
return $query->whereIn('user_id', $users);
64+
}
65+
5066
/**
5167
* Get the fields displayed by the resource.
5268
*

app/Nova/JenisNaskah.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Laravel\Nova\Fields\BelongsTo;
77
use Laravel\Nova\Fields\File;
88
use Laravel\Nova\Fields\Text;
9-
10-
Laravel\Nova\Fields\Textarea;
119
use Laravel\Nova\Fields\URL;
1210
use Laravel\Nova\Http\Requests\NovaRequest;
1311

app/Policies/IzinKeluarPolicy.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Models\IzinKeluar;
6+
use App\Models\User;
7+
use Illuminate\Auth\Access\Response;
8+
9+
class IzinKeluarPolicy
10+
{
11+
/**
12+
* Determine whether the user can view any models.
13+
*/
14+
public function viewAny(): bool
15+
{
16+
return in_array(session('role'), ['kepala','anggota','koordinator']);
17+
}
18+
19+
/**
20+
* Determine whether the user can view the model.
21+
*/
22+
public function view(User $user, IzinKeluar $izinKeluar): bool
23+
{
24+
$allowedyear = ((session('year') == $izinKeluar->tahun));
25+
if (session('role') === 'kepala') {
26+
return $allowedyear;
27+
}
28+
if (session('role') === 'koordinator') {
29+
return $allowedyear && ($user->unit_kerja_id === $izinKeluar->user->unit_kerja_id);
30+
}
31+
if (session('role') === 'anggota') {
32+
return $allowedyear && ($user->id === $izinKeluar->user_id);
33+
}
34+
return false;
35+
}
36+
37+
/**
38+
* Determine whether the user can create models.
39+
*/
40+
public function create(User $user): bool
41+
{
42+
return in_array(session('role'), ['kepala','anggota','koordinator']);
43+
}
44+
45+
/**
46+
* Determine whether the user can update the model.
47+
*/
48+
public function update(User $user, IzinKeluar $izinKeluar): bool
49+
{
50+
$allowedyear = ((session('year') == $izinKeluar->tahun));
51+
return $allowedyear && ($user->id === $izinKeluar->user_id);
52+
}
53+
54+
/**
55+
* Determine whether the user can delete the model.
56+
*/
57+
public function delete(User $user, IzinKeluar $izinKeluar): bool
58+
{
59+
$allowedyear = ((session('year') == $izinKeluar->tahun));
60+
return $allowedyear && ($user->id === $izinKeluar->user_id);
61+
}
62+
63+
/**
64+
* Determine whether the user can restore the model.
65+
*/
66+
public function restore(User $user, IzinKeluar $izinKeluar): bool
67+
{
68+
$allowedyear = ((session('year') == $izinKeluar->tahun));
69+
return $allowedyear && ($user->id === $izinKeluar->user_id);
70+
}
71+
72+
/**
73+
* Determine whether the user can permanently delete the model.
74+
*/
75+
public function forceDelete(User $user, IzinKeluar $izinKeluar): bool
76+
{
77+
$allowedyear = ((session('year') == $izinKeluar->tahun));
78+
return $allowedyear && ($user->id === $izinKeluar->user_id);
79+
}
80+
81+
/**
82+
* Determine whether the user can replicate the model.
83+
*/
84+
public function replicate(User $user, IzinKeluar $izinKeluar): bool
85+
{
86+
$allowedyear = ((session('year') == $izinKeluar->tahun));
87+
return $allowedyear && ($user->id === $izinKeluar->user_id);
88+
}
89+
}

app/Policies/JenisNaskahPolicy.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,69 @@
22

33
namespace App\Policies;
44

5-
use App\Models\JenisNaskah;
6-
use App\Models\User;
7-
85
class JenisNaskahPolicy
96
{
107
/**
118
* Determine whether the user can view any models.
129
*/
13-
public function viewAny(User $user): bool
10+
public function viewAny(): bool
1411
{
15-
return session('role') === 'admin';
12+
return (session('role') === 'admin');
1613
}
1714

1815
/**
1916
* Determine whether the user can view the model.
2017
*/
21-
public function view(User $user, JenisNaskah $jenisNaskah): bool
18+
public function view(): bool
2219
{
23-
return session('role') === 'admin';
20+
return (session('role') === 'admin');
2421
}
2522

2623
/**
2724
* Determine whether the user can create models.
2825
*/
29-
public function create(User $user): bool
26+
public function create(): bool
3027
{
31-
return session('role') === 'admin';
28+
return (session('role') === 'admin');
3229
}
3330

3431
/**
3532
* Determine whether the user can update the model.
3633
*/
37-
public function update(User $user, JenisNaskah $jenisNaskah): bool
34+
public function update(): bool
3835
{
39-
return session('role') === 'admin';
36+
return (session('role') === 'admin');
4037
}
4138

4239
/**
4340
* Determine whether the user can delete the model.
4441
*/
45-
public function delete(User $user, JenisNaskah $jenisNaskah): bool
42+
public function delete(): bool
4643
{
47-
return session('role') === 'admin';
44+
return (session('role') === 'admin');
4845
}
4946

5047
/**
5148
* Determine whether the user can restore the model.
5249
*/
53-
public function restore(User $user, JenisNaskah $jenisNaskah): bool
50+
public function restore(): bool
5451
{
55-
return session('role') === 'admin';
52+
return (session('role') === 'admin');
5653
}
5754

5855
/**
5956
* Determine whether the user can permanently delete the model.
6057
*/
61-
public function forceDelete(User $user, JenisNaskah $jenisNaskah): bool
58+
public function forceDelete(): bool
6259
{
63-
return session('role') === 'admin';
60+
return (session('role') === 'admin');
6461
}
6562

6663
/**
6764
* Determine whether the user can replicate the model.
6865
*/
69-
public function replicate(User $user, JenisNaskah $jenisNaskah): bool
66+
public function replicate(): bool
7067
{
71-
return session('role') === 'admin';
68+
return (session('role') === 'admin');
7269
}
7370
}

app/Policies/KodeArsipPolicy.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
class KodeArsipPolicy
6+
{
7+
/**
8+
* Determine whether the user can view any models.
9+
*/
10+
public function viewAny(): bool
11+
{
12+
return (session('role') === 'admin');
13+
}
14+
15+
/**
16+
* Determine whether the user can view the model.
17+
*/
18+
public function view(): bool
19+
{
20+
return (session('role') === 'admin');
21+
}
22+
23+
/**
24+
* Determine whether the user can create models.
25+
*/
26+
public function create(): bool
27+
{
28+
return (session('role') === 'admin');
29+
}
30+
31+
/**
32+
* Determine whether the user can update the model.
33+
*/
34+
public function update(): bool
35+
{
36+
return (session('role') === 'admin');
37+
}
38+
39+
/**
40+
* Determine whether the user can delete the model.
41+
*/
42+
public function delete(): bool
43+
{
44+
return (session('role') === 'admin');
45+
}
46+
47+
/**
48+
* Determine whether the user can restore the model.
49+
*/
50+
public function restore(): bool
51+
{
52+
return (session('role') === 'admin');
53+
}
54+
55+
/**
56+
* Determine whether the user can permanently delete the model.
57+
*/
58+
public function forceDelete(): bool
59+
{
60+
return (session('role') === 'admin');
61+
}
62+
63+
/**
64+
* Determine whether the user can replicate the model.
65+
*/
66+
public function replicate(): bool
67+
{
68+
return (session('role') === 'admin');
69+
}
70+
}

app/Policies/KodeNaskahPolicy.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
class KodeNaskahPolicy
6+
{
7+
/**
8+
* Determine whether the user can view any models.
9+
*/
10+
public function viewAny(): bool
11+
{
12+
return (session('role') === 'admin');
13+
}
14+
15+
/**
16+
* Determine whether the user can view the model.
17+
*/
18+
public function view(): bool
19+
{
20+
return (session('role') === 'admin');
21+
}
22+
23+
/**
24+
* Determine whether the user can create models.
25+
*/
26+
public function create(): bool
27+
{
28+
return (session('role') === 'admin');
29+
}
30+
31+
/**
32+
* Determine whether the user can update the model.
33+
*/
34+
public function update(): bool
35+
{
36+
return (session('role') === 'admin');
37+
}
38+
39+
/**
40+
* Determine whether the user can delete the model.
41+
*/
42+
public function delete(): bool
43+
{
44+
return (session('role') === 'admin');
45+
}
46+
47+
/**
48+
* Determine whether the user can restore the model.
49+
*/
50+
public function restore(): bool
51+
{
52+
return (session('role') === 'admin');
53+
}
54+
55+
/**
56+
* Determine whether the user can permanently delete the model.
57+
*/
58+
public function forceDelete(): bool
59+
{
60+
return (session('role') === 'admin');
61+
}
62+
63+
/**
64+
* Determine whether the user can replicate the model.
65+
*/
66+
public function replicate(): bool
67+
{
68+
return (session('role') === 'admin');
69+
}
70+
}

0 commit comments

Comments
 (0)