Add admin user management and improve UX
Features: - Add user management for admins (CRUD operations) - Add is_admin column to users table - Add AdminMiddleware for authorization - Add admin routes and views - Add artisan command: user:set-admin Improvements: - Fix Quick Switcher: add x-data for Alpine.js dispatch - Fix Quick Switcher: close on outside click - Redirect non-admin users to front page after login - Add Knowledge Base link in dashboard navigation - Change app logo from Laravel to book icon
This commit is contained in:
50
src/app/Console/Commands/SetUserAdmin.php
Normal file
50
src/app/Console/Commands/SetUserAdmin.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class SetUserAdmin extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'user:set-admin {email} {--remove : Remove admin status}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Set or remove admin status for a user';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$email = $this->argument('email');
|
||||
$user = User::where('email', $email)->first();
|
||||
|
||||
if (!$user) {
|
||||
$this->error("User with email '{$email}' not found.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$isAdmin = !$this->option('remove');
|
||||
$user->is_admin = $isAdmin;
|
||||
$user->save();
|
||||
|
||||
if ($isAdmin) {
|
||||
$this->info("User '{$user->name}' ({$email}) is now an admin.");
|
||||
} else {
|
||||
$this->info("Admin status removed from user '{$user->name}' ({$email}).");
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
104
src/app/Http/Controllers/Admin/UserController.php
Normal file
104
src/app/Http/Controllers/Admin/UserController.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the users.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$users = User::orderBy('created_at', 'desc')->paginate(20);
|
||||
|
||||
return view('admin.users.index', compact('users'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new user.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('admin.users.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created user in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:users'],
|
||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
||||
'is_admin' => ['boolean'],
|
||||
]);
|
||||
|
||||
$user = User::create([
|
||||
'name' => $validated['name'],
|
||||
'email' => $validated['email'],
|
||||
'password' => Hash::make($validated['password']),
|
||||
'is_admin' => $request->boolean('is_admin'),
|
||||
]);
|
||||
|
||||
return redirect()->route('admin.users.index')
|
||||
->with('success', 'ユーザーを作成しました。');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified user.
|
||||
*/
|
||||
public function edit(User $user)
|
||||
{
|
||||
return view('admin.users.edit', compact('user'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified user in storage.
|
||||
*/
|
||||
public function update(Request $request, User $user)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:users,email,' . $user->id],
|
||||
'password' => ['nullable', 'confirmed', Rules\Password::defaults()],
|
||||
'is_admin' => ['boolean'],
|
||||
]);
|
||||
|
||||
$user->name = $validated['name'];
|
||||
$user->email = $validated['email'];
|
||||
$user->is_admin = $request->boolean('is_admin');
|
||||
|
||||
if (!empty($validated['password'])) {
|
||||
$user->password = Hash::make($validated['password']);
|
||||
}
|
||||
|
||||
$user->save();
|
||||
|
||||
return redirect()->route('admin.users.index')
|
||||
->with('success', 'ユーザー情報を更新しました。');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified user from storage.
|
||||
*/
|
||||
public function destroy(User $user)
|
||||
{
|
||||
// 自分自身は削除できない
|
||||
if ($user->id === auth()->id()) {
|
||||
return back()->with('error', '自分自身を削除することはできません。');
|
||||
}
|
||||
|
||||
$user->delete();
|
||||
|
||||
return redirect()->route('admin.users.index')
|
||||
->with('success', 'ユーザーを削除しました。');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,12 @@ public function store(LoginRequest $request): RedirectResponse
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
// 管理者はダッシュボードへ、一般ユーザーはフロントページへリダイレクト
|
||||
if (Auth::user()->isAdmin()) {
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
}
|
||||
|
||||
return redirect()->intended('/');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
25
src/app/Http/Middleware/AdminMiddleware.php
Normal file
25
src/app/Http/Middleware/AdminMiddleware.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class AdminMiddleware
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (!$request->user() || !$request->user()->isAdmin()) {
|
||||
abort(403, 'Unauthorized. Admin access required.');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,19 @@ class User extends Authenticatable
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'is_admin',
|
||||
];
|
||||
|
||||
/**
|
||||
* Check if user is an administrator.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return (bool) $this->is_admin;
|
||||
}
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user