From 9ab76daa4b569e2cfb914a07bc2268a0769108d1 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 21 Nov 2022 15:47:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=91=98=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/AdminController.php | 131 +++++++++++++++++ resources/views/admin/admins/create.blade.php | 20 +++ resources/views/admin/admins/edit.blade.php | 36 +++++ resources/views/admin/admins/index.blade.php | 45 ++++++ resources/views/layouts/admin.blade.php | 137 +++++++++--------- routes/admin.php | 2 + 6 files changed, 304 insertions(+), 67 deletions(-) create mode 100644 app/Http/Controllers/Admin/AdminController.php create mode 100644 resources/views/admin/admins/create.blade.php create mode 100644 resources/views/admin/admins/edit.blade.php create mode 100644 resources/views/admin/admins/index.blade.php diff --git a/app/Http/Controllers/Admin/AdminController.php b/app/Http/Controllers/Admin/AdminController.php new file mode 100644 index 0000000..0105b46 --- /dev/null +++ b/app/Http/Controllers/Admin/AdminController.php @@ -0,0 +1,131 @@ +validate([ + 'email' => 'required|email|unique:admins,email', + ]); + + // 随机密码 + $password = Str::random(16); + + $admin = Admin::create([ + 'email' => $request->email, + 'password' => bcrypt($password), + ]); + + return redirect()->route('admin.admins.edit', $admin)->with('success', '管理员创建成功,密码为:' . $password . '。'); + } + + /** + * Show the form for editing the specified resource. + * + * @param Admin $admin + * + * @return View + */ + public function edit(Admin $admin): View + { + return view('admin.admins.edit', compact('admin')); + } + + /** + * Update the specified resource in storage. + * + * @param Request $request + * @param Admin $admin + * + * @return RedirectResponse + */ + public function update(Request $request, Admin $admin): RedirectResponse + { + $request->validate([ + 'email' => 'required|email|unique:admins,email,' . $admin->id, + ]); + + $msg = '管理员信息更新成功'; + + if ($request->filled('reset_password')) { + // 随机密码 + $password = Str::random(16); + + $msg .= ',新的密码为:' . $password; + + $admin->password = bcrypt($password); + } + + $msg .= '。'; + + $admin->email = $request->email; + + $admin->save(); + + return redirect()->back()->with('success', $msg); + } + + /** + * Remove the specified resource from storage. + * + * @param Admin $admin + * + * @return RedirectResponse + */ + public function destroy(Admin $admin): RedirectResponse + { + // 不能删除自己 + if ($admin->id == auth('admin')->id()) { + return redirect()->back()->with('error', '不能删除自己。'); + } + + // 不能删除最后一个管理员 + if (Admin::count() == 1) { + return redirect()->back()->with('error', '不能删除最后一个管理员。'); + } + + $admin->delete(); + + return redirect()->route('admin.admins.index')->with('success', '管理员已删除。'); + } +} diff --git a/resources/views/admin/admins/create.blade.php b/resources/views/admin/admins/create.blade.php new file mode 100644 index 0000000..7269f34 --- /dev/null +++ b/resources/views/admin/admins/create.blade.php @@ -0,0 +1,20 @@ +@extends('layouts.admin') + +@section('title', '新建管理员') + +@section('content') +

权力越大,责任越大

+ 返回管理员列表 + +
+ @csrf + +
+ + +
+ + +
+ +@endsection diff --git a/resources/views/admin/admins/edit.blade.php b/resources/views/admin/admins/edit.blade.php new file mode 100644 index 0000000..d8c37c7 --- /dev/null +++ b/resources/views/admin/admins/edit.blade.php @@ -0,0 +1,36 @@ +@extends('layouts.admin') + +@section('title', '管理员:' . $admin->name) + +@section('content') +

{{ $admin->name }}

+ +
+ @csrf + @method('PATCH') + +
+ + +
+ +
+ + +
+ + +
+ + +
+
+ @csrf + @method('DELETE') + +
+ +@endsection diff --git a/resources/views/admin/admins/index.blade.php b/resources/views/admin/admins/index.blade.php new file mode 100644 index 0000000..eb977de --- /dev/null +++ b/resources/views/admin/admins/index.blade.php @@ -0,0 +1,45 @@ +@extends('layouts.admin') + +@section('title', '管理员') + +@section('content') + +

管理员

+

权力越大,责任越大

+ 新建管理员 +
+ + + + + + + + + @foreach ($admins as $admin) + + + + + + @endforeach + +
ID邮件操作
+ + {{ $admin->id }} + + + {{ $admin->email }} + + 编辑 +
+
+ + {{-- 分页 --}} + {{ $admins->links() }} + + + + + +@endsection diff --git a/resources/views/layouts/admin.blade.php b/resources/views/layouts/admin.blade.php index c720874..f0acc21 100644 --- a/resources/views/layouts/admin.blade.php +++ b/resources/views/layouts/admin.blade.php @@ -19,91 +19,94 @@ -
-
- +
@yield('content')
- +
diff --git a/routes/admin.php b/routes/admin.php index 9ebbcfa..d69120e 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -1,5 +1,6 @@ 'auth:admin', ], function () { + Route::resource('admins', AdminController::class)->except('show'); Route::resource('users', UserController::class)->only(['index', 'show', 'edit', 'update']); Route::resource('modules', ModuleController::class); Route::resource('hosts', HostController::class)->only(['index', 'edit', 'update', 'destroy']);