role listing
This commit is contained in:
9
.claude/settings.local.json
Normal file
9
.claude/settings.local.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"permissions": {
|
||||||
|
"allow": [
|
||||||
|
"mcp__laravel-boost__database-schema",
|
||||||
|
"Bash(php artisan *)",
|
||||||
|
"Bash(vendor/bin/pint --dirty --format agent)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
20
app/Http/Controllers/RoleController.php
Normal file
20
app/Http/Controllers/RoleController.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class RoleController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$roles = Role::orderBy('name')->paginate(10);
|
||||||
|
|
||||||
|
if ($request->ajax()) {
|
||||||
|
return view('roles._table', compact('roles'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return view('roles.index', compact('roles'));
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/Models/Role.php
Normal file
15
app/Models/Role.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Database\Factories\RoleFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Role extends Model
|
||||||
|
{
|
||||||
|
/** @use HasFactory<RoleFactory> */
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = ['name', 'description'];
|
||||||
|
}
|
||||||
25
database/factories/RoleFactory.php
Normal file
25
database/factories/RoleFactory.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<Role>
|
||||||
|
*/
|
||||||
|
class RoleFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => fake()->unique()->word(),
|
||||||
|
'description' => fake()->sentence(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
29
database/migrations/2026_05_11_042341_create_roles_table.php
Normal file
29
database/migrations/2026_05_11_042341_create_roles_table.php
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('roles', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name')->unique();
|
||||||
|
$table->string('description')->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('roles');
|
||||||
|
}
|
||||||
|
};
|
||||||
22
database/seeders/RoleSeeder.php
Normal file
22
database/seeders/RoleSeeder.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
|
class RoleSeeder extends Seeder
|
||||||
|
{
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
$roles = [
|
||||||
|
['name' => 'Admin', 'description' => 'Full access to all resources'],
|
||||||
|
['name' => 'Editor', 'description' => 'Can create and edit content'],
|
||||||
|
['name' => 'Viewer', 'description' => 'Read-only access'],
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($roles as $role) {
|
||||||
|
Role::firstOrCreate(['name' => $role['name']], $role);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,9 @@
|
|||||||
<x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
<x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||||
{{ __('Dashboard') }}
|
{{ __('Dashboard') }}
|
||||||
</x-nav-link>
|
</x-nav-link>
|
||||||
|
<x-nav-link :href="route('roles.index')" :active="request()->routeIs('roles.index')">
|
||||||
|
{{ __('Roles') }}
|
||||||
|
</x-nav-link>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -70,6 +73,9 @@
|
|||||||
<x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
<x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||||
{{ __('Dashboard') }}
|
{{ __('Dashboard') }}
|
||||||
</x-responsive-nav-link>
|
</x-responsive-nav-link>
|
||||||
|
<x-responsive-nav-link :href="route('roles.index')" :active="request()->routeIs('roles.index')">
|
||||||
|
{{ __('Roles') }}
|
||||||
|
</x-responsive-nav-link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Responsive Settings Options -->
|
<!-- Responsive Settings Options -->
|
||||||
|
|||||||
34
resources/views/roles/_table.blade.php
Normal file
34
resources/views/roles/_table.blade.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<table class="w-full text-sm text-left text-gray-900 dark:text-gray-100">
|
||||||
|
<thead class="text-xs text-gray-700 dark:text-gray-400 uppercase bg-gray-50 dark:bg-gray-700">
|
||||||
|
<tr>
|
||||||
|
<th class="px-6 py-3">#</th>
|
||||||
|
<th class="px-6 py-3">{{ __('Name') }}</th>
|
||||||
|
<th class="px-6 py-3">{{ __('Description') }}</th>
|
||||||
|
<th class="px-6 py-3">{{ __('Created') }}</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach ($roles as $role)
|
||||||
|
<tr class="{{ $loop->even ? 'bg-gray-50 dark:bg-gray-700' : 'bg-white dark:bg-gray-800' }} border-b border-gray-200 dark:border-gray-600">
|
||||||
|
<td class="px-6 py-4">{{ $roles->firstItem() + $loop->index }}</td>
|
||||||
|
<td class="px-6 py-4 font-medium">{{ $role->name }}</td>
|
||||||
|
<td class="px-6 py-4">{{ $role->description ?? '—' }}</td>
|
||||||
|
<td class="px-6 py-4">{{ $role->created_at->format('d M Y') }}</td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
|
||||||
|
@if ($roles->isEmpty())
|
||||||
|
<tr>
|
||||||
|
<td colspan="4" class="px-6 py-4 text-center text-gray-500 dark:text-gray-400">
|
||||||
|
{{ __('No roles found.') }}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@endif
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
@if ($roles->hasPages())
|
||||||
|
<div class="mt-4">
|
||||||
|
{{ $roles->links() }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
42
resources/views/roles/index.blade.php
Normal file
42
resources/views/roles/index.blade.php
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<x-app-layout>
|
||||||
|
<x-slot name="header">
|
||||||
|
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
|
||||||
|
{{ __('Roles') }}
|
||||||
|
</h2>
|
||||||
|
</x-slot>
|
||||||
|
|
||||||
|
<div class="py-12">
|
||||||
|
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||||
|
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
|
||||||
|
<div
|
||||||
|
class="p-6"
|
||||||
|
x-data="{
|
||||||
|
loading: false,
|
||||||
|
async paginate(url) {
|
||||||
|
this.loading = true;
|
||||||
|
const response = await fetch(url, {
|
||||||
|
headers: { 'X-Requested-With': 'XMLHttpRequest' }
|
||||||
|
});
|
||||||
|
this.$refs.tableContainer.innerHTML = await response.text();
|
||||||
|
this.loading = false;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
@click.prevent="
|
||||||
|
const link = $event.target.closest('a[href]');
|
||||||
|
if (link && $refs.tableContainer.contains(link)) {
|
||||||
|
paginate(link.href);
|
||||||
|
}
|
||||||
|
"
|
||||||
|
>
|
||||||
|
<div x-show="loading" class="text-center py-4 text-gray-500 dark:text-gray-400">
|
||||||
|
{{ __('Loading...') }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div x-ref="tableContainer" x-show="!loading">
|
||||||
|
@include('roles._table')
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</x-app-layout>
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\ProfileController;
|
use App\Http\Controllers\ProfileController;
|
||||||
|
use App\Http\Controllers\RoleController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
Route::get('/', function () {
|
Route::get('/', function () {
|
||||||
@@ -12,6 +13,7 @@ Route::get('/dashboard', function () {
|
|||||||
})->middleware(['auth', 'verified'])->name('dashboard');
|
})->middleware(['auth', 'verified'])->name('dashboard');
|
||||||
|
|
||||||
Route::middleware('auth')->group(function () {
|
Route::middleware('auth')->group(function () {
|
||||||
|
Route::get('/roles', [RoleController::class, 'index'])->name('roles.index');
|
||||||
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
||||||
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
||||||
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
||||||
|
|||||||
41
tests/Feature/RoleIndexTest.php
Normal file
41
tests/Feature/RoleIndexTest.php
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
test('guests are redirected to login from roles page', function () {
|
||||||
|
$this->get('/roles')->assertRedirect('/login');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated users can view the roles page', function () {
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->get('/roles')
|
||||||
|
->assertOk()
|
||||||
|
->assertViewIs('roles.index');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('roles page passes paginated roles to view', function () {
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->get('/roles')
|
||||||
|
->assertOk()
|
||||||
|
->assertViewHas('roles');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ajax request returns roles table partial', function () {
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->withHeader('X-Requested-With', 'XMLHttpRequest')
|
||||||
|
->get('/roles')
|
||||||
|
->assertOk()
|
||||||
|
->assertViewIs('roles._table');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ajax pagination returns correct page', function () {
|
||||||
|
Role::factory()->count(15)->create();
|
||||||
|
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->withHeader('X-Requested-With', 'XMLHttpRequest')
|
||||||
|
->get('/roles?page=2')
|
||||||
|
->assertOk()
|
||||||
|
->assertViewIs('roles._table')
|
||||||
|
->assertViewHas('roles', fn ($roles) => $roles->currentPage() === 2);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user