Dodanie uprawnien all dashboard

This commit is contained in:
Tomasz Boruc 2026-09-04 22:01:19 +02:00
parent e920dabf89
commit 10f77f8e05
6 changed files with 92 additions and 5 deletions

View File

@ -16,11 +16,13 @@ class DashboardController extends Controller
{
$user = auth()->user();
if ($user && $user->hasRole('pracownik')) {
$showAllDashboard = $user?->hasPermissionTo('show_all_dashbaord') ?? false;
if (! $showAllDashboard) {
$myVehicles = $user->vehicles()->orderBy('brand')->orderBy('model')->get();
$myAdvances = $user->advances()->with('type')->orderByDesc('date')->orderByDesc('id')->get();
return view('dashboard', compact('myVehicles', 'myAdvances'));
return view('dashboard', compact('myVehicles', 'myAdvances', 'showAllDashboard'));
}
$totalUsers = User::whereNull('deleted_at')->count();
@ -64,7 +66,8 @@ class DashboardController extends Controller
'workerCount',
'recentUsers',
'lowestAdvanceBalances',
'vehiclesDueSoon'
'vehiclesDueSoon',
'showAllDashboard'
));
}
}

View File

@ -0,0 +1,55 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\PermissionRegistrar;
return new class extends Migration
{
public function up(): void
{
$permissionsTable = config('permission.table_names.permissions', 'permissions');
DB::table($permissionsTable)->updateOrInsert(
['name' => 'show_all_dashbaord', 'guard_name' => 'web'],
['created_at' => now(), 'updated_at' => now()]
);
$permissionId = DB::table($permissionsTable)
->where('name', 'show_all_dashbaord')
->where('guard_name', 'web')
->value('id');
$rolesTable = config('permission.table_names.roles', 'roles');
$rolePermissionsTable = config('permission.table_names.role_has_permissions', 'role_has_permissions');
$roleIds = DB::table($rolesTable)
->whereIn('name', ['admin', 'kierownik'])
->where('guard_name', 'web')
->pluck('id');
foreach ($roleIds as $roleId) {
DB::table($rolePermissionsTable)->insertOrIgnore([
'permission_id' => $permissionId,
'role_id' => $roleId,
]);
}
app(PermissionRegistrar::class)->forgetCachedPermissions();
}
public function down(): void
{
$permissionsTable = config('permission.table_names.permissions', 'permissions');
$rolePermissionsTable = config('permission.table_names.role_has_permissions', 'role_has_permissions');
$permissionId = DB::table($permissionsTable)
->where('name', 'show_all_dashbaord')
->where('guard_name', 'web')
->value('id');
if ($permissionId) {
DB::table($rolePermissionsTable)->where('permission_id', $permissionId)->delete();
DB::table($permissionsTable)->where('id', $permissionId)->delete();
}
app(PermissionRegistrar::class)->forgetCachedPermissions();
}
};

View File

@ -24,6 +24,7 @@ class DatabaseSeeder extends Seeder
'zaliczki_access',
'users_access',
'setting_access',
'show_all_dashbaord',
];
foreach ($permissions as $permission) {
@ -41,6 +42,7 @@ class DatabaseSeeder extends Seeder
$kierownikRole->syncPermissions([
'users_access',
'setting_access',
'show_all_dashbaord',
'raporty_access',
'flota_access',
'zaliczki_access',

View File

@ -1,14 +1,14 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
{{ auth()->user()->hasRole('pracownik') ? 'Mój dashboard' : __('Dashboard') }}
{{ $showAllDashboard ? __('Dashboard') : 'Mój dashboard' }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8 space-y-6">
@if(auth()->user()->hasRole('pracownik'))
@if(! $showAllDashboard)
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div class="bg-white dark:bg-gray-800 p-6 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700/50">
<h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Mój samochód</h3>

View File

@ -15,6 +15,12 @@ function userWithAdvanceAccess(string $roleName = 'pracownik'): User
$user = User::factory()->create();
$role = Role::firstOrCreate(['name' => $roleName]);
$role->givePermissionTo('zaliczki_access');
if ($roleName === 'kierownik') {
Permission::firstOrCreate(['name' => 'show_all_dashbaord']);
$role->givePermissionTo('show_all_dashbaord');
}
$user->assignRole($role);
return $user;
@ -307,6 +313,21 @@ test('dashboard shows the lowest advance balances for employees', function () {
$response->assertSee('-200,00');
});
test('role without show all dashboard permission sees only its own dashboard', function () {
Permission::firstOrCreate(['name' => 'zaliczki_access']);
$user = User::factory()->create();
$role = Role::firstOrCreate(['name' => 'brygadzista']);
$role->givePermissionTo('zaliczki_access');
$user->assignRole($role);
$response = $this->actingAs($user)->get(route('dashboard'));
$response->assertOk();
$response->assertSee('Mój dashboard');
$response->assertDontSee('Wszyscy użytkownicy');
});
test('dashboard prioritizes negative balances over zero balances', function () {
$manager = userWithAdvanceAccess('kierownik');
$fuel = AdvanceType::create(['name' => 'Paliwo']);

View File

@ -14,6 +14,12 @@ function createFleetUser(string $roleName = 'pracownik'): User
$user = User::factory()->create();
$role = Role::firstOrCreate(['name' => $roleName]);
$role->givePermissionTo('flota_access');
if ($roleName === 'kierownik') {
Permission::firstOrCreate(['name' => 'show_all_dashbaord']);
$role->givePermissionTo('show_all_dashbaord');
}
$user->assignRole($role);
return $user;