role & user module merged. conflicts resolved
This commit is contained in:
45
app/Http/Controllers/RoleController.php
Normal file
45
app/Http/Controllers/RoleController.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Display a listing of roles.
|
||||||
|
*/
|
||||||
|
public function index(): View
|
||||||
|
{
|
||||||
|
$roles = Role::query()
|
||||||
|
->orderBy('name')
|
||||||
|
->get(['id', 'name', 'created_at']);
|
||||||
|
|
||||||
|
return view('role.index', [
|
||||||
|
'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'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
9
app/Models/Role.php
Normal file
9
app/Models/Role.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
#[Fillable(['name'])]
|
||||||
|
class Role extends Model {}
|
||||||
28
database/migrations/2026_05_11_033145_create_roles_table.php
Normal file
28
database/migrations/2026_05_11_033145_create_roles_table.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?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->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('roles');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -18,6 +18,9 @@
|
|||||||
<x-nav-link :href="route('user.index')" :active="request()->routeIs('user.index')">
|
<x-nav-link :href="route('user.index')" :active="request()->routeIs('user.index')">
|
||||||
{{ __('Users') }}
|
{{ __('Users') }}
|
||||||
</x-nav-link>
|
</x-nav-link>
|
||||||
|
<x-nav-link :href="route('role.index')" :active="request()->routeIs('role.index')">
|
||||||
|
{{ __('Roles') }}
|
||||||
|
</x-nav-link>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -73,8 +76,13 @@
|
|||||||
<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>
|
||||||
|
<<<<<<< HEAD
|
||||||
<x-responsive-nav-link :href="route('user.index')" :active="request()->routeIs('user.index')">
|
<x-responsive-nav-link :href="route('user.index')" :active="request()->routeIs('user.index')">
|
||||||
{{ __('Users') }}
|
{{ __('Users') }}
|
||||||
|
=======
|
||||||
|
<x-responsive-nav-link :href="route('role.index')" :active="request()->routeIs('role.index')">
|
||||||
|
{{ __('Roles') }}
|
||||||
|
>>>>>>> role-module
|
||||||
</x-responsive-nav-link>
|
</x-responsive-nav-link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
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>
|
||||||
54
resources/views/role/index.blade.php
Normal file
54
resources/views/role/index.blade.php
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<x-app-layout>
|
||||||
|
<x-slot name="header">
|
||||||
|
<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">
|
||||||
|
<tr>
|
||||||
|
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
|
||||||
|
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
|
||||||
|
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody class="divide-y divide-gray-200">
|
||||||
|
@forelse ($roles as $role)
|
||||||
|
<tr class="odd:bg-white even:bg-gray-50">
|
||||||
|
<td class="px-4 py-3 text-sm text-gray-700">{{ $role->id }}</td>
|
||||||
|
<td class="px-4 py-3 text-sm text-gray-900">{{ $role->name }}</td>
|
||||||
|
<td class="px-4 py-3 text-sm text-gray-700">{{ $role->created_at?->format('Y-m-d H:i') }}</td>
|
||||||
|
</tr>
|
||||||
|
@empty
|
||||||
|
<tr>
|
||||||
|
<td colspan="3" class="px-4 py-6 text-center text-sm text-gray-500">
|
||||||
|
No roles found.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@endforelse
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</x-app-layout>
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use App\Http\Controllers\ProfileController;
|
use App\Http\Controllers\ProfileController;
|
||||||
use App\Http\Controllers\UserController;
|
use App\Http\Controllers\UserController;
|
||||||
|
use App\Http\Controllers\RoleController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
Route::get('/', function () {
|
Route::get('/', function () {
|
||||||
@@ -16,6 +17,9 @@ Route::middleware('auth')->group(function () {
|
|||||||
Route::get('/user', [UserController::class, 'index'])->name('user.index');
|
Route::get('/user', [UserController::class, 'index'])->name('user.index');
|
||||||
Route::get('/user/{user}/edit', [UserController::class, 'edit'])->name('user.edit');
|
Route::get('/user/{user}/edit', [UserController::class, 'edit'])->name('user.edit');
|
||||||
Route::patch('/user/{user}', [UserController::class, 'update'])->name('user.update');
|
Route::patch('/user/{user}', [UserController::class, 'update'])->name('user.update');
|
||||||
|
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::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');
|
||||||
|
|||||||
38
tests/Feature/RoleIndexTest.php
Normal file
38
tests/Feature/RoleIndexTest.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
test('role index requires authentication', function () {
|
||||||
|
$this->get('/role')->assertRedirect('/login');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated users can view the role index', function () {
|
||||||
|
Role::query()->create(['name' => 'Admin']);
|
||||||
|
Role::query()->create(['name' => 'Editor']);
|
||||||
|
|
||||||
|
$response = $this
|
||||||
|
->actingAs(User::factory()->create())
|
||||||
|
->get('/role');
|
||||||
|
|
||||||
|
$response
|
||||||
|
->assertSuccessful()
|
||||||
|
->assertSee('Roles')
|
||||||
|
->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