diff --git a/app/Http/Controllers/RoleController.php b/app/Http/Controllers/RoleController.php new file mode 100644 index 0000000..542de1e --- /dev/null +++ b/app/Http/Controllers/RoleController.php @@ -0,0 +1,45 @@ +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'); + } +} diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..9a21442 --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,66 @@ +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'); + } +} diff --git a/app/Http/Requests/StoreRoleRequest.php b/app/Http/Requests/StoreRoleRequest.php new file mode 100644 index 0000000..83e7624 --- /dev/null +++ b/app/Http/Requests/StoreRoleRequest.php @@ -0,0 +1,29 @@ +|string> + */ + public function rules(): array + { + return [ + 'name' => ['required', 'string', 'max:255', 'unique:roles,name'], + ]; + } +} diff --git a/app/Http/Requests/UpdateUserRequest.php b/app/Http/Requests/UpdateUserRequest.php new file mode 100644 index 0000000..26a00b5 --- /dev/null +++ b/app/Http/Requests/UpdateUserRequest.php @@ -0,0 +1,39 @@ +|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'], + ]; + } +} diff --git a/app/Models/Role.php b/app/Models/Role.php new file mode 100644 index 0000000..1ff0fd7 --- /dev/null +++ b/app/Models/Role.php @@ -0,0 +1,19 @@ +belongsToMany(User::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index f6ba1d2..4ec3016 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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); + } } diff --git a/database/migrations/2026_05_11_033145_create_roles_table.php b/database/migrations/2026_05_11_033145_create_roles_table.php new file mode 100644 index 0000000..87bd721 --- /dev/null +++ b/database/migrations/2026_05_11_033145_create_roles_table.php @@ -0,0 +1,28 @@ +id(); + $table->string('name')->unique(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('roles'); + } +}; diff --git a/database/migrations/2026_05_11_040743_create_role_user_table.php b/database/migrations/2026_05_11_040743_create_role_user_table.php new file mode 100644 index 0000000..fce2c58 --- /dev/null +++ b/database/migrations/2026_05_11_040743_create_role_user_table.php @@ -0,0 +1,29 @@ +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'); + } +}; diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index c2d3a65..9e9e0a1 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -15,6 +15,12 @@ {{ __('Dashboard') }} + + {{ __('Users') }} + + + {{ __('Roles') }} + @@ -70,6 +76,12 @@ {{ __('Dashboard') }} + + {{ __('Users') }} + + + {{ __('Roles') }} + diff --git a/resources/views/role/create.blade.php b/resources/views/role/create.blade.php new file mode 100644 index 0000000..0428483 --- /dev/null +++ b/resources/views/role/create.blade.php @@ -0,0 +1,33 @@ + + +

+ {{ __('Create Role') }} +

+
+ +
+
+
+
+
+ @csrf + +
+ + + +
+ +
+ {{ __('Save') }} + + + {{ __('Cancel') }} + +
+
+
+
+
+
+
\ No newline at end of file diff --git a/resources/views/role/index.blade.php b/resources/views/role/index.blade.php new file mode 100644 index 0000000..604e19f --- /dev/null +++ b/resources/views/role/index.blade.php @@ -0,0 +1,54 @@ + + +
+

+ {{ __('Roles') }} +

+ + + {{ __('New Role') }} + +
+
+ +
+
+
+
+ @if (session('status') === 'role-created') +
+ {{ __('Role created successfully.') }} +
+ @endif + +
+ + + + + + + + + + @forelse ($roles as $role) + + + + + + @empty + + + + @endforelse + +
IDNameCreated
{{ $role->id }}{{ $role->name }}{{ $role->created_at?->format('Y-m-d H:i') }}
+ No roles found. +
+
+
+
+
+
+
diff --git a/resources/views/user/edit.blade.php b/resources/views/user/edit.blade.php new file mode 100644 index 0000000..9b93407 --- /dev/null +++ b/resources/views/user/edit.blade.php @@ -0,0 +1,70 @@ + + +

+ {{ __('Edit User') }} +

+
+ +
+
+
+
+ @if (session('status') === 'user-updated') +
+ {{ __('User updated successfully.') }} +
+ @endif + +
+ @csrf + @method('patch') + +
+ + + +
+ +
+ + + +
+ +
+ + +
+ @forelse ($roles as $role) + + @empty +

{{ __('No roles available.') }}

+ @endforelse +
+ + + +
+ +
+ {{ __('Save') }} + + + {{ __('Back') }} + +
+
+
+
+
+
+
\ No newline at end of file diff --git a/resources/views/user/index.blade.php b/resources/views/user/index.blade.php new file mode 100644 index 0000000..e1101c0 --- /dev/null +++ b/resources/views/user/index.blade.php @@ -0,0 +1,54 @@ + + +

+ {{ __('Users') }} +

+
+ +
+
+
+
+
+ + + + + + + + + + + + @forelse ($users as $user) + + + + + + + + @empty + + + + @endforelse + +
IDNameEmailCreatedActions
{{ $user->id }}{{ $user->name }}{{ $user->email }}{{ $user->created_at?->format('Y-m-d H:i') }} + + {{ __('Edit') }} + +
+ No users found. +
+
+ +
+ {{ $users->links() }} +
+
+
+
+
+
diff --git a/routes/web.php b/routes/web.php index 74bb7ca..74935d1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,8 @@ 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'); 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'); diff --git a/tests/Feature/RoleIndexTest.php b/tests/Feature/RoleIndexTest.php new file mode 100644 index 0000000..2458028 --- /dev/null +++ b/tests/Feature/RoleIndexTest.php @@ -0,0 +1,38 @@ +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', + ]); +}); diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php new file mode 100644 index 0000000..93393de --- /dev/null +++ b/tests/Feature/UserTest.php @@ -0,0 +1,74 @@ +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); +});