Compare commits
5 Commits
role-modul
...
user-modul
| Author | SHA1 | Date | |
|---|---|---|---|
| 8c909adf62 | |||
| b0eef8fca1 | |||
| 3b1f50b96d | |||
| f110790e09 | |||
| 27c2869109 |
66
app/Http/Controllers/UserController.php
Normal file
66
app/Http/Controllers/UserController.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\UpdateUserRequest;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of users.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
$users = User::query()
|
||||
->orderBy('name')
|
||||
->paginate(10, ['id', 'name', 'email', 'created_at']);
|
||||
|
||||
return view('user.index', [
|
||||
'users' => $users,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified user.
|
||||
*/
|
||||
public function edit(User $user): View
|
||||
{
|
||||
$user->load('roles:id');
|
||||
$roles = Role::query()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name']);
|
||||
|
||||
return view('user.edit', [
|
||||
'user' => $user,
|
||||
'roles' => $roles,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified user.
|
||||
*/
|
||||
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
||||
{
|
||||
$validated = $request->validated();
|
||||
$roleIds = $validated['roles'] ?? [];
|
||||
|
||||
unset($validated['roles']);
|
||||
|
||||
$user->fill($validated);
|
||||
|
||||
if ($user->isDirty('email')) {
|
||||
$user->email_verified_at = null;
|
||||
}
|
||||
|
||||
$user->save();
|
||||
$user->roles()->sync($roleIds);
|
||||
|
||||
return redirect()
|
||||
->route('user.edit', $user)
|
||||
->with('status', 'user-updated');
|
||||
}
|
||||
}
|
||||
39
app/Http/Requests/UpdateUserRequest.php
Normal file
39
app/Http/Requests/UpdateUserRequest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UpdateUserRequest 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'],
|
||||
'email' => [
|
||||
'required',
|
||||
'string',
|
||||
'email',
|
||||
'max:255',
|
||||
Rule::unique('users', 'email')->ignore($this->route('user')),
|
||||
],
|
||||
'roles' => ['nullable', 'array'],
|
||||
'roles.*' => ['integer', 'exists:roles,id'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,16 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
#[Fillable(['name'])]
|
||||
class Role extends Model {}
|
||||
class Role extends Model
|
||||
{
|
||||
/**
|
||||
* The users that belong to the role.
|
||||
*/
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
@@ -29,4 +30,12 @@ class User extends Authenticatable
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The roles that belong to the user.
|
||||
*/
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Role::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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('role_user', function (Blueprint $table) {
|
||||
$table->foreignId('role_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
|
||||
$table->primary(['role_id', 'user_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('role_user');
|
||||
}
|
||||
};
|
||||
@@ -15,6 +15,9 @@
|
||||
<x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||
{{ __('Dashboard') }}
|
||||
</x-nav-link>
|
||||
<x-nav-link :href="route('user.index')" :active="request()->routeIs('user.index')">
|
||||
{{ __('Users') }}
|
||||
</x-nav-link>
|
||||
<x-nav-link :href="route('role.index')" :active="request()->routeIs('role.index')">
|
||||
{{ __('Roles') }}
|
||||
</x-nav-link>
|
||||
@@ -73,6 +76,9 @@
|
||||
<x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||
{{ __('Dashboard') }}
|
||||
</x-responsive-nav-link>
|
||||
<x-responsive-nav-link :href="route('user.index')" :active="request()->routeIs('user.index')">
|
||||
{{ __('Users') }}
|
||||
</x-responsive-nav-link>
|
||||
<x-responsive-nav-link :href="route('role.index')" :active="request()->routeIs('role.index')">
|
||||
{{ __('Roles') }}
|
||||
</x-responsive-nav-link>
|
||||
|
||||
70
resources/views/user/edit.blade.php
Normal file
70
resources/views/user/edit.blade.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<x-app-layout>
|
||||
<x-slot name="header">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||
{{ __('Edit User') }}
|
||||
</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">
|
||||
@if (session('status') === 'user-updated')
|
||||
<div class="mb-4 rounded-md bg-green-50 px-4 py-3 text-sm text-green-700">
|
||||
{{ __('User updated successfully.') }}
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<form method="POST" action="{{ route('user.update', $user) }}" class="space-y-6 max-w-xl">
|
||||
@csrf
|
||||
@method('patch')
|
||||
|
||||
<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', $user->name)" required autofocus autocomplete="name" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('name')" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<x-input-label for="email" :value="__('Email')" />
|
||||
<x-text-input id="email" class="mt-1 block w-full" type="email" name="email" :value="old('email', $user->email)" required autocomplete="username" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('email')" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<x-input-label :value="__('Roles')" />
|
||||
|
||||
<div class="mt-2 space-y-2 rounded-md border border-gray-200 p-4">
|
||||
@forelse ($roles as $role)
|
||||
<label class="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
name="roles[]"
|
||||
value="{{ $role->id }}"
|
||||
@checked(in_array($role->id, old('roles', $user->roles->pluck('id')->all()), true))
|
||||
class="rounded border-gray-300 text-indigo-600 shadow-sm focus:ring-indigo-500"
|
||||
>
|
||||
<span class="text-sm text-gray-700">{{ $role->name }}</span>
|
||||
</label>
|
||||
@empty
|
||||
<p class="text-sm text-gray-500">{{ __('No roles available.') }}</p>
|
||||
@endforelse
|
||||
</div>
|
||||
|
||||
<x-input-error class="mt-2" :messages="$errors->get('roles')" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('roles.*')" />
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<x-primary-button>{{ __('Save') }}</x-primary-button>
|
||||
|
||||
<a href="{{ route('user.index') }}" class="text-sm text-gray-600 underline hover:text-gray-900">
|
||||
{{ __('Back') }}
|
||||
</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</x-app-layout>
|
||||
54
resources/views/user/index.blade.php
Normal file
54
resources/views/user/index.blade.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<x-app-layout>
|
||||
<x-slot name="header">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||
{{ __('Users') }}
|
||||
</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">
|
||||
<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">Email</th>
|
||||
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created</th>
|
||||
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-gray-200">
|
||||
@forelse ($users as $user)
|
||||
<tr class="odd:bg-white even:bg-gray-50">
|
||||
<td class="px-4 py-3 text-sm text-gray-700">{{ $user->id }}</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-900">{{ $user->name }}</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-700">{{ $user->email }}</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-700">{{ $user->created_at?->format('Y-m-d H:i') }}</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-700">
|
||||
<a href="{{ route('user.edit', $user) }}" class="font-medium text-indigo-600 hover:text-indigo-500">
|
||||
{{ __('Edit') }}
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="5" class="px-4 py-6 text-sm text-center text-gray-500">
|
||||
No users found.
|
||||
</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="mt-6">
|
||||
{{ $users->links() }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</x-app-layout>
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\ProfileController;
|
||||
use App\Http\Controllers\UserController;
|
||||
use App\Http\Controllers\RoleController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
@@ -13,6 +14,9 @@ Route::get('/dashboard', function () {
|
||||
})->middleware(['auth', 'verified'])->name('dashboard');
|
||||
|
||||
Route::middleware('auth')->group(function () {
|
||||
Route::get('/user', [UserController::class, 'index'])->name('user.index');
|
||||
Route::get('/user/{user}/edit', [UserController::class, 'edit'])->name('user.edit');
|
||||
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');
|
||||
|
||||
74
tests/Feature/UserTest.php
Normal file
74
tests/Feature/UserTest.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
|
||||
test('user edit page requires authentication', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->get("/user/{$user->id}/edit")->assertRedirect('/login');
|
||||
});
|
||||
|
||||
test('authenticated users can view the user edit page', function () {
|
||||
$actingUser = User::factory()->create();
|
||||
$user = User::factory()->create();
|
||||
$role = Role::query()->create(['name' => 'Admin']);
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
$response = $this
|
||||
->actingAs($actingUser)
|
||||
->get("/user/{$user->id}/edit");
|
||||
|
||||
$response
|
||||
->assertSuccessful()
|
||||
->assertSee('Edit User')
|
||||
->assertSee($user->name)
|
||||
->assertSee($user->email)
|
||||
->assertSee('Roles')
|
||||
->assertSee('Admin')
|
||||
->assertSee('checked', false);
|
||||
});
|
||||
|
||||
test('authenticated users can update a user', function () {
|
||||
$actingUser = User::factory()->create();
|
||||
$user = User::factory()->create();
|
||||
$adminRole = Role::query()->create(['name' => 'Admin']);
|
||||
$editorRole = Role::query()->create(['name' => 'Editor']);
|
||||
|
||||
$response = $this
|
||||
->actingAs($actingUser)
|
||||
->patch("/user/{$user->id}", [
|
||||
'name' => 'Updated User',
|
||||
'email' => 'updated@example.com',
|
||||
'roles' => [$adminRole->id, $editorRole->id],
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertRedirect("/user/{$user->id}/edit");
|
||||
|
||||
$user->refresh();
|
||||
|
||||
$this->assertSame('Updated User', $user->name);
|
||||
$this->assertSame('updated@example.com', $user->email);
|
||||
expect($user->roles()->pluck('roles.id')->all())
|
||||
->toMatchArray([$adminRole->id, $editorRole->id]);
|
||||
});
|
||||
|
||||
test('user roles can be cleared by submitting no roles', function () {
|
||||
$actingUser = User::factory()->create();
|
||||
$user = User::factory()->create();
|
||||
$role = Role::query()->create(['name' => 'Admin']);
|
||||
|
||||
$user->roles()->attach($role->id);
|
||||
|
||||
$response = $this
|
||||
->actingAs($actingUser)
|
||||
->patch("/user/{$user->id}", [
|
||||
'name' => $user->name,
|
||||
'email' => $user->email,
|
||||
]);
|
||||
|
||||
$response->assertRedirect("/user/{$user->id}/edit");
|
||||
|
||||
expect($user->refresh()->roles()->count())->toBe(0);
|
||||
});
|
||||
Reference in New Issue
Block a user