new role functionality
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreRoleRequest;
|
||||
use App\Models\Role;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class RoleController extends Controller
|
||||
@@ -20,4 +22,24 @@ class RoleController extends Controller
|
||||
'roles' => $roles,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new role.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('role.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created role.
|
||||
*/
|
||||
public function store(StoreRoleRequest $request): RedirectResponse
|
||||
{
|
||||
Role::create($request->validated());
|
||||
|
||||
return redirect()
|
||||
->route('role.index')
|
||||
->with('status', 'role-created');
|
||||
}
|
||||
}
|
||||
|
||||
29
app/Http/Requests/StoreRoleRequest.php
Normal file
29
app/Http/Requests/StoreRoleRequest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255', 'unique:roles,name'],
|
||||
];
|
||||
}
|
||||
}
|
||||
33
resources/views/role/create.blade.php
Normal file
33
resources/views/role/create.blade.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<x-app-layout>
|
||||
<x-slot name="header">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||
{{ __('Create Role') }}
|
||||
</h2>
|
||||
</x-slot>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
|
||||
<div class="p-6 text-gray-900">
|
||||
<form method="POST" action="{{ route('role.store') }}" class="space-y-6 max-w-xl">
|
||||
@csrf
|
||||
|
||||
<div>
|
||||
<x-input-label for="name" :value="__('Name')" />
|
||||
<x-text-input id="name" class="mt-1 block w-full" type="text" name="name" :value="old('name')" required autofocus autocomplete="off" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('name')" />
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<x-primary-button>{{ __('Save') }}</x-primary-button>
|
||||
|
||||
<a href="{{ route('role.index') }}" class="text-sm text-gray-600 underline hover:text-gray-900">
|
||||
{{ __('Cancel') }}
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</x-app-layout>
|
||||
@@ -1,14 +1,26 @@
|
||||
<x-app-layout>
|
||||
<x-slot name="header">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||
{{ __('Roles') }}
|
||||
</h2>
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||
{{ __('Roles') }}
|
||||
</h2>
|
||||
|
||||
<a href="{{ route('role.create') }}" class="inline-flex items-center rounded-md border border-transparent bg-gray-800 px-4 py-2 text-xs font-semibold uppercase tracking-widest text-white transition hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2">
|
||||
{{ __('New Role') }}
|
||||
</a>
|
||||
</div>
|
||||
</x-slot>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
|
||||
<div class="p-6 text-gray-900">
|
||||
@if (session('status') === 'role-created')
|
||||
<div class="mb-4 rounded-md bg-green-50 px-4 py-3 text-sm text-green-700">
|
||||
{{ __('Role created successfully.') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="overflow-x-auto">
|
||||
<table class="min-w-full divide-y divide-gray-200">
|
||||
<thead class="bg-gray-50">
|
||||
|
||||
@@ -14,6 +14,8 @@ Route::get('/dashboard', function () {
|
||||
|
||||
Route::middleware('auth')->group(function () {
|
||||
Route::get('/role', [RoleController::class, 'index'])->name('role.index');
|
||||
Route::get('/role/create', [RoleController::class, 'create'])->name('role.create');
|
||||
Route::post('/role', [RoleController::class, 'store'])->name('role.store');
|
||||
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
||||
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
||||
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
||||
|
||||
@@ -21,3 +21,18 @@ test('authenticated users can view the role index', function () {
|
||||
->assertSee('Admin')
|
||||
->assertSee('Editor');
|
||||
});
|
||||
|
||||
test('authenticated users can create a role', function () {
|
||||
$response = $this
|
||||
->actingAs(User::factory()->create())
|
||||
->post('/role', [
|
||||
'name' => 'Manager',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertRedirect('/role');
|
||||
|
||||
$this->assertDatabaseHas('roles', [
|
||||
'name' => 'Manager',
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user