add new role functionality
This commit is contained in:
@@ -3,7 +3,8 @@
|
|||||||
"allow": [
|
"allow": [
|
||||||
"mcp__laravel-boost__database-schema",
|
"mcp__laravel-boost__database-schema",
|
||||||
"Bash(php artisan *)",
|
"Bash(php artisan *)",
|
||||||
"Bash(vendor/bin/pint --dirty --format agent)"
|
"Bash(vendor/bin/pint --dirty --format agent)",
|
||||||
|
"mcp__laravel-boost__search-docs"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,11 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requests\StoreRoleRequest;
|
||||||
use App\Models\Role;
|
use App\Models\Role;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
|
||||||
class RoleController extends Controller
|
class RoleController extends Controller
|
||||||
{
|
{
|
||||||
@@ -17,4 +20,17 @@ class RoleController extends Controller
|
|||||||
|
|
||||||
return view('roles.index', compact('roles'));
|
return view('roles.index', compact('roles'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function create(): View
|
||||||
|
{
|
||||||
|
return view('roles.create');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(StoreRoleRequest $request): RedirectResponse
|
||||||
|
{
|
||||||
|
Role::create($request->validated());
|
||||||
|
|
||||||
|
return redirect()->route('roles.index')
|
||||||
|
->with('status', 'role-created');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
app/Http/Requests/StoreRoleRequest.php
Normal file
28
app/Http/Requests/StoreRoleRequest.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class StoreRoleRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => ['required', 'string', 'max:255', 'unique:roles,name'],
|
||||||
|
'description' => ['nullable', 'string', 'max:500'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
56
resources/views/roles/create.blade.php
Normal file
56
resources/views/roles/create.blade.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<x-app-layout>
|
||||||
|
<x-slot name="header">
|
||||||
|
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
|
||||||
|
{{ __('New Role') }}
|
||||||
|
</h2>
|
||||||
|
</x-slot>
|
||||||
|
|
||||||
|
<div class="py-12">
|
||||||
|
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||||
|
<div class="p-4 sm:p-8 bg-white dark:bg-gray-800 shadow sm:rounded-lg">
|
||||||
|
<div class="max-w-xl">
|
||||||
|
<section>
|
||||||
|
<header>
|
||||||
|
<h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
|
||||||
|
{{ __('Role Details') }}
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
|
||||||
|
{{ __('Create a new role to assign to users in the system.') }}
|
||||||
|
</p>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<form method="post" action="{{ route('roles.store') }}" class="mt-6 space-y-6">
|
||||||
|
@csrf
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<x-input-label for="name" :value="__('Name')" />
|
||||||
|
<x-text-input id="name" name="name" type="text" class="mt-1 block w-full" :value="old('name')" required autofocus autocomplete="off" />
|
||||||
|
<x-input-error class="mt-2" :messages="$errors->get('name')" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<x-input-label for="description" :value="__('Description')" />
|
||||||
|
<textarea
|
||||||
|
id="description"
|
||||||
|
name="description"
|
||||||
|
rows="3"
|
||||||
|
class="mt-1 block w-full border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600 rounded-md shadow-sm"
|
||||||
|
>{{ old('description') }}</textarea>
|
||||||
|
<x-input-error class="mt-2" :messages="$errors->get('description')" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center gap-4">
|
||||||
|
<x-primary-button>{{ __('Create Role') }}</x-primary-button>
|
||||||
|
|
||||||
|
<a href="{{ route('roles.index') }}" class="text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 underline">
|
||||||
|
{{ __('Cancel') }}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</x-app-layout>
|
||||||
@@ -1,12 +1,29 @@
|
|||||||
<x-app-layout>
|
<x-app-layout>
|
||||||
<x-slot name="header">
|
<x-slot name="header">
|
||||||
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
|
<div class="flex items-center justify-between">
|
||||||
{{ __('Roles') }}
|
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
|
||||||
</h2>
|
{{ __('Roles') }}
|
||||||
|
</h2>
|
||||||
|
<a href="{{ route('roles.create') }}">
|
||||||
|
<x-primary-button>{{ __('New Role') }}</x-primary-button>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
</x-slot>
|
</x-slot>
|
||||||
|
|
||||||
<div class="py-12">
|
<div class="py-12">
|
||||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||||
|
@if (session('status') === 'role-created')
|
||||||
|
<div
|
||||||
|
x-data="{ show: true }"
|
||||||
|
x-show="show"
|
||||||
|
x-transition
|
||||||
|
x-init="setTimeout(() => show = false, 3000)"
|
||||||
|
class="mb-4 p-4 bg-green-100 dark:bg-green-900 text-green-800 dark:text-green-200 text-sm rounded-lg"
|
||||||
|
>
|
||||||
|
{{ __('Role created successfully.') }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
|
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
|
||||||
<div
|
<div
|
||||||
class="p-6"
|
class="p-6"
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ Route::get('/dashboard', function () {
|
|||||||
|
|
||||||
Route::middleware('auth')->group(function () {
|
Route::middleware('auth')->group(function () {
|
||||||
Route::get('/roles', [RoleController::class, 'index'])->name('roles.index');
|
Route::get('/roles', [RoleController::class, 'index'])->name('roles.index');
|
||||||
|
Route::get('/roles/create', [RoleController::class, 'create'])->name('roles.create');
|
||||||
|
Route::post('/roles', [RoleController::class, 'store'])->name('roles.store');
|
||||||
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');
|
||||||
|
|||||||
52
tests/Feature/StoreRoleTest.php
Normal file
52
tests/Feature/StoreRoleTest.php
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
test('guests cannot access the create role page', function () {
|
||||||
|
$this->get('/roles/create')->assertRedirect('/login');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated users can access the create role page', function () {
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->get('/roles/create')
|
||||||
|
->assertOk()
|
||||||
|
->assertViewIs('roles.create');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated users can create a role', function () {
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->post('/roles', ['name' => 'Manager', 'description' => 'Manages things'])
|
||||||
|
->assertRedirect(route('roles.index'))
|
||||||
|
->assertSessionHas('status', 'role-created');
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('roles', ['name' => 'Manager', 'description' => 'Manages things']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('role name is required', function () {
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->post('/roles', ['name' => '', 'description' => 'Some description'])
|
||||||
|
->assertSessionHasErrors('name');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('role name must be unique', function () {
|
||||||
|
Role::factory()->create(['name' => 'Admin']);
|
||||||
|
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->post('/roles', ['name' => 'Admin'])
|
||||||
|
->assertSessionHasErrors('name');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('description is optional', function () {
|
||||||
|
$this->actingAs(User::factory()->create())
|
||||||
|
->post('/roles', ['name' => 'Viewer'])
|
||||||
|
->assertRedirect(route('roles.index'));
|
||||||
|
|
||||||
|
$this->assertDatabaseHas('roles', ['name' => 'Viewer', 'description' => null]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('guests cannot create a role', function () {
|
||||||
|
$this->post('/roles', ['name' => 'Admin'])->assertRedirect('/login');
|
||||||
|
|
||||||
|
$this->assertDatabaseMissing('roles', ['name' => 'Admin']);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user