feat: restrict payroll index visibility to current user for Admin role
This commit is contained in:
parent
ad6b9e9618
commit
002b6fa468
@ -24,10 +24,16 @@ public function index(): Response
|
|||||||
return Inertia::render('admin/finance/payroll/index', [
|
return Inertia::render('admin/finance/payroll/index', [
|
||||||
'payrolls' => Payroll::with(['user.profile', 'adjustments'])
|
'payrolls' => Payroll::with(['user.profile', 'adjustments'])
|
||||||
->where('period_month', '!=', $currentMonth)
|
->where('period_month', '!=', $currentMonth)
|
||||||
|
->when(auth()->user()->hasRole('Admin'), function ($query) {
|
||||||
|
$query->where('user_id', auth()->id());
|
||||||
|
})
|
||||||
->latest()
|
->latest()
|
||||||
->get(),
|
->get(),
|
||||||
'currentMonthPayrolls' => Payroll::with(['user.profile', 'adjustments'])
|
'currentMonthPayrolls' => Payroll::with(['user.profile', 'adjustments'])
|
||||||
->where('period_month', $currentMonth)
|
->where('period_month', $currentMonth)
|
||||||
|
->when(auth()->user()->hasRole('Admin'), function ($query) {
|
||||||
|
$query->where('user_id', auth()->id());
|
||||||
|
})
|
||||||
->get(),
|
->get(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
use App\Enums\SalaryAdjustmentType;
|
use App\Enums\SalaryAdjustmentType;
|
||||||
use App\Models\Payroll;
|
use App\Models\Payroll;
|
||||||
|
use App\Models\User;
|
||||||
use Illuminate\Support\Facades\Artisan;
|
use Illuminate\Support\Facades\Artisan;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
|
|
||||||
use function Pest\Laravel\actingAs;
|
use function Pest\Laravel\actingAs;
|
||||||
use function Pest\Laravel\assertDatabaseHas;
|
use function Pest\Laravel\assertDatabaseHas;
|
||||||
@ -54,6 +56,62 @@
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('filters payrolls for Admin role to only see their own records', function () {
|
||||||
|
Role::findOrCreate('Admin');
|
||||||
|
$admin = createAuthorizedUser(['View:Payroll']);
|
||||||
|
$admin->assignRole('Admin');
|
||||||
|
|
||||||
|
$otherUser = User::factory()->create();
|
||||||
|
$currentMonth = now()->format('Y-m');
|
||||||
|
|
||||||
|
// Payroll for admin
|
||||||
|
$adminPayroll = Payroll::factory()->create([
|
||||||
|
'user_id' => $admin->id,
|
||||||
|
'period_month' => $currentMonth,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Payroll for other user
|
||||||
|
Payroll::factory()->create([
|
||||||
|
'user_id' => $otherUser->id,
|
||||||
|
'period_month' => $currentMonth,
|
||||||
|
]);
|
||||||
|
|
||||||
|
actingAs($admin)
|
||||||
|
->get(route('payroll.index'))
|
||||||
|
->assertOk()
|
||||||
|
->assertInertia(fn ($page) => $page
|
||||||
|
->component('admin/finance/payroll/index')
|
||||||
|
->has('currentMonthPayrolls', 1)
|
||||||
|
->where('currentMonthPayrolls.0.id', $adminPayroll->id)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows all payrolls for users without Admin role', function () {
|
||||||
|
$user = createAuthorizedUser(['View:Payroll']);
|
||||||
|
// No Admin role assigned
|
||||||
|
|
||||||
|
$otherUser = User::factory()->create();
|
||||||
|
$currentMonth = now()->format('Y-m');
|
||||||
|
|
||||||
|
Payroll::factory()->create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'period_month' => $currentMonth,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Payroll::factory()->create([
|
||||||
|
'user_id' => $otherUser->id,
|
||||||
|
'period_month' => $currentMonth,
|
||||||
|
]);
|
||||||
|
|
||||||
|
actingAs($user)
|
||||||
|
->get(route('payroll.index'))
|
||||||
|
->assertOk()
|
||||||
|
->assertInertia(fn ($page) => $page
|
||||||
|
->component('admin/finance/payroll/index')
|
||||||
|
->has('currentMonthPayrolls', 2)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it('can generate payroll', function () {
|
it('can generate payroll', function () {
|
||||||
// Mocking artisan call is tricky if we want to check side effects,
|
// Mocking artisan call is tricky if we want to check side effects,
|
||||||
// but here we check the response and session.
|
// but here we check the response and session.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user