tambah relation user-role
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
{
|
{
|
||||||
"permissions": {
|
"permissions": {
|
||||||
"allow": [
|
"allow": [
|
||||||
<<<<<<< HEAD
|
|
||||||
=======
|
|
||||||
"mcp__laravel-boost__database-schema",
|
"mcp__laravel-boost__database-schema",
|
||||||
>>>>>>> role-module
|
|
||||||
"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"
|
"mcp__laravel-boost__search-docs"
|
||||||
|
|||||||
@@ -2,8 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Http\Requests\UpdateUserRolesRequest;
|
||||||
|
use App\Models\Role;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\View\View;
|
||||||
|
|
||||||
class UserController extends Controller
|
class UserController extends Controller
|
||||||
{
|
{
|
||||||
@@ -17,4 +21,19 @@ class UserController extends Controller
|
|||||||
|
|
||||||
return view('users.index', compact('users'));
|
return view('users.index', compact('users'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function edit(User $user): View
|
||||||
|
{
|
||||||
|
$roles = Role::orderBy('name')->get();
|
||||||
|
|
||||||
|
return view('users.edit', compact('user', 'roles'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(UpdateUserRolesRequest $request, User $user): RedirectResponse
|
||||||
|
{
|
||||||
|
$user->roles()->sync($request->validated()['roles'] ?? []);
|
||||||
|
|
||||||
|
return redirect()->route('users.index')
|
||||||
|
->with('status', 'user-updated');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
app/Http/Requests/UpdateUserRolesRequest.php
Normal file
28
app/Http/Requests/UpdateUserRolesRequest.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateUserRolesRequest 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 [
|
||||||
|
'roles' => ['nullable', 'array'],
|
||||||
|
'roles.*' => ['integer', 'exists:roles,id'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
|||||||
use Database\Factories\RoleFactory;
|
use Database\Factories\RoleFactory;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
class Role extends Model
|
class Role extends Model
|
||||||
{
|
{
|
||||||
@@ -12,4 +13,9 @@ class Role extends Model
|
|||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
protected $fillable = ['name', 'description'];
|
protected $fillable = ['name', 'description'];
|
||||||
|
|
||||||
|
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\Fillable;
|
||||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
|
|
||||||
@@ -29,4 +30,9 @@ class User extends Authenticatable
|
|||||||
'password' => 'hashed',
|
'password' => 'hashed',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function roles(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Role::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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('role_user', function (Blueprint $table) {
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->foreignId('role_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->primary(['user_id', 'role_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('role_user');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -15,9 +15,10 @@
|
|||||||
<x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
<x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||||
{{ __('Dashboard') }}
|
{{ __('Dashboard') }}
|
||||||
</x-nav-link>
|
</x-nav-link>
|
||||||
<x-nav-link :href="route('users.index')" :active="request()->routeIs('users.index')">
|
<x-nav-link :href="route('users.index')" :active="request()->routeIs('users.*')">
|
||||||
{{ __('Users') }}
|
{{ __('Users') }}
|
||||||
<x-nav-link :href="route('roles.index')" :active="request()->routeIs('roles.index')">
|
</x-nav-link>
|
||||||
|
<x-nav-link :href="route('roles.index')" :active="request()->routeIs('roles.*')">
|
||||||
{{ __('Roles') }}
|
{{ __('Roles') }}
|
||||||
</x-nav-link>
|
</x-nav-link>
|
||||||
</div>
|
</div>
|
||||||
@@ -75,9 +76,10 @@
|
|||||||
<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>
|
||||||
<x-responsive-nav-link :href="route('users.index')" :active="request()->routeIs('users.index')">
|
<x-responsive-nav-link :href="route('users.index')" :active="request()->routeIs('users.*')">
|
||||||
{{ __('Users') }}
|
{{ __('Users') }}
|
||||||
<x-responsive-nav-link :href="route('roles.index')" :active="request()->routeIs('roles.index')">
|
</x-responsive-nav-link>
|
||||||
|
<x-responsive-nav-link :href="route('roles.index')" :active="request()->routeIs('roles.*')">
|
||||||
{{ __('Roles') }}
|
{{ __('Roles') }}
|
||||||
</x-responsive-nav-link>
|
</x-responsive-nav-link>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
<th class="px-6 py-3">{{ __('Name') }}</th>
|
<th class="px-6 py-3">{{ __('Name') }}</th>
|
||||||
<th class="px-6 py-3">{{ __('Email') }}</th>
|
<th class="px-6 py-3">{{ __('Email') }}</th>
|
||||||
<th class="px-6 py-3">{{ __('Joined') }}</th>
|
<th class="px-6 py-3">{{ __('Joined') }}</th>
|
||||||
|
<th class="px-6 py-3"></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -14,12 +15,17 @@
|
|||||||
<td class="px-6 py-4 font-medium">{{ $user->name }}</td>
|
<td class="px-6 py-4 font-medium">{{ $user->name }}</td>
|
||||||
<td class="px-6 py-4">{{ $user->email }}</td>
|
<td class="px-6 py-4">{{ $user->email }}</td>
|
||||||
<td class="px-6 py-4">{{ $user->created_at->format('d M Y') }}</td>
|
<td class="px-6 py-4">{{ $user->created_at->format('d M Y') }}</td>
|
||||||
|
<td class="px-6 py-4 text-right">
|
||||||
|
<a href="{{ route('users.edit', $user) }}" class="text-indigo-600 dark:text-indigo-400 hover:underline text-sm font-medium">
|
||||||
|
{{ __('Edit') }}
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
@endforeach
|
@endforeach
|
||||||
|
|
||||||
@if ($users->isEmpty())
|
@if ($users->isEmpty())
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="4" class="px-6 py-4 text-center text-gray-500 dark:text-gray-400">
|
<td colspan="5" class="px-6 py-4 text-center text-gray-500 dark:text-gray-400">
|
||||||
{{ __('No users found.') }}
|
{{ __('No users found.') }}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
65
resources/views/users/edit.blade.php
Normal file
65
resources/views/users/edit.blade.php
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<x-app-layout>
|
||||||
|
<x-slot name="header">
|
||||||
|
<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
|
||||||
|
{{ __('Edit User') }}: {{ $user->name }}
|
||||||
|
</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">
|
||||||
|
{{ __('Assign Roles') }}
|
||||||
|
</h2>
|
||||||
|
|
||||||
|
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
|
||||||
|
{{ __('Select the roles to assign to this user.') }}
|
||||||
|
</p>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<form method="post" action="{{ route('users.update', $user) }}" class="mt-6 space-y-6">
|
||||||
|
@csrf
|
||||||
|
@method('put')
|
||||||
|
|
||||||
|
<div class="space-y-3">
|
||||||
|
@forelse ($roles as $role)
|
||||||
|
<label class="flex items-start gap-3 cursor-pointer">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="roles[]"
|
||||||
|
value="{{ $role->id }}"
|
||||||
|
{{ $user->roles->contains($role) ? 'checked' : '' }}
|
||||||
|
class="mt-0.5 rounded border-gray-300 dark:border-gray-700 text-indigo-600 shadow-sm focus:ring-indigo-500 dark:focus:ring-indigo-600 dark:bg-gray-900"
|
||||||
|
/>
|
||||||
|
<div>
|
||||||
|
<span class="text-sm font-medium text-gray-900 dark:text-gray-100">{{ $role->name }}</span>
|
||||||
|
@if ($role->description)
|
||||||
|
<p class="text-xs text-gray-500 dark:text-gray-400">{{ $role->description }}</p>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
@empty
|
||||||
|
<p class="text-sm text-gray-500 dark:text-gray-400">{{ __('No roles available. Create one first.') }}</p>
|
||||||
|
@endforelse
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<x-input-error class="mt-2" :messages="$errors->get('roles')" />
|
||||||
|
<x-input-error class="mt-2" :messages="$errors->get('roles.*')" />
|
||||||
|
|
||||||
|
<div class="flex items-center gap-4">
|
||||||
|
<x-primary-button>{{ __('Save Roles') }}</x-primary-button>
|
||||||
|
|
||||||
|
<a href="{{ route('users.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>
|
||||||
@@ -21,9 +21,10 @@
|
|||||||
this.loading = false;
|
this.loading = false;
|
||||||
}
|
}
|
||||||
}"
|
}"
|
||||||
@click.prevent="
|
@click="
|
||||||
const link = $event.target.closest('a[href]');
|
const link = $event.target.closest('a[href]');
|
||||||
if (link && $refs.tableContainer.contains(link)) {
|
if (link && link.closest('nav[role=navigation]') && $refs.tableContainer.contains(link)) {
|
||||||
|
$event.preventDefault();
|
||||||
paginate(link.href);
|
paginate(link.href);
|
||||||
}
|
}
|
||||||
"
|
"
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\ProfileController;
|
use App\Http\Controllers\ProfileController;
|
||||||
use App\Http\Controllers\UserController;
|
|
||||||
use App\Http\Controllers\RoleController;
|
use App\Http\Controllers\RoleController;
|
||||||
|
use App\Http\Controllers\UserController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
Route::get('/', function () {
|
Route::get('/', function () {
|
||||||
@@ -15,6 +15,8 @@ Route::get('/dashboard', function () {
|
|||||||
|
|
||||||
Route::middleware('auth')->group(function () {
|
Route::middleware('auth')->group(function () {
|
||||||
Route::get('/users', [UserController::class, 'index'])->name('users.index');
|
Route::get('/users', [UserController::class, 'index'])->name('users.index');
|
||||||
|
Route::get('/users/{user}/edit', [UserController::class, 'edit'])->name('users.edit');
|
||||||
|
Route::put('/users/{user}', [UserController::class, 'update'])->name('users.update');
|
||||||
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::get('/roles/create', [RoleController::class, 'create'])->name('roles.create');
|
||||||
Route::post('/roles', [RoleController::class, 'store'])->name('roles.store');
|
Route::post('/roles', [RoleController::class, 'store'])->name('roles.store');
|
||||||
|
|||||||
85
tests/Feature/EditUserTest.php
Normal file
85
tests/Feature/EditUserTest.php
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
|
test('guests cannot access the edit user page', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->get(route('users.edit', $user))->assertRedirect('/login');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('authenticated users can access the edit user page', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->get(route('users.edit', $user))
|
||||||
|
->assertOk()
|
||||||
|
->assertViewIs('users.edit')
|
||||||
|
->assertViewHas('user', $user)
|
||||||
|
->assertViewHas('roles');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('edit page shows all available roles', function () {
|
||||||
|
$roles = Role::factory()->count(3)->create();
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->get(route('users.edit', $user))
|
||||||
|
->assertOk()
|
||||||
|
->assertViewHas('roles', fn ($viewRoles) => $viewRoles->count() === $roles->count());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can assign roles to a user', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$roles = Role::factory()->count(2)->create();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->put(route('users.update', $user), ['roles' => $roles->pluck('id')->all()])
|
||||||
|
->assertRedirect(route('users.index'))
|
||||||
|
->assertSessionHas('status', 'user-updated');
|
||||||
|
|
||||||
|
expect($user->roles()->count())->toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can remove all roles from a user', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$role = Role::factory()->create();
|
||||||
|
$user->roles()->attach($role);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->put(route('users.update', $user), [])
|
||||||
|
->assertRedirect(route('users.index'));
|
||||||
|
|
||||||
|
expect($user->roles()->count())->toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('syncs roles replacing previous assignments', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$oldRole = Role::factory()->create();
|
||||||
|
$newRole = Role::factory()->create();
|
||||||
|
$user->roles()->attach($oldRole);
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->put(route('users.update', $user), ['roles' => [$newRole->id]]);
|
||||||
|
|
||||||
|
expect($user->roles()->pluck('id')->all())->toBe([$newRole->id]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('role ids must exist in the database', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
|
||||||
|
$this->actingAs($user)
|
||||||
|
->put(route('users.update', $user), ['roles' => [999]])
|
||||||
|
->assertSessionHasErrors('roles.*');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('guests cannot update user roles', function () {
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$role = Role::factory()->create();
|
||||||
|
|
||||||
|
$this->put(route('users.update', $user), ['roles' => [$role->id]])
|
||||||
|
->assertRedirect('/login');
|
||||||
|
|
||||||
|
expect($user->roles()->count())->toBe(0);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user