增加 管理员管理
This commit is contained in:
parent
af2b26ee0e
commit
9ab76daa4b
131
app/Http/Controllers/Admin/AdminController.php
Normal file
131
app/Http/Controllers/Admin/AdminController.php
Normal file
@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Admin;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AdminController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return View
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
$admins = Admin::paginate(100);
|
||||
|
||||
return view('admin.admins.index', compact('admins'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*
|
||||
* @return View
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('admin.admins.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
//
|
||||
|
||||
$request->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', '管理员已删除。');
|
||||
}
|
||||
}
|
20
resources/views/admin/admins/create.blade.php
Normal file
20
resources/views/admin/admins/create.blade.php
Normal file
@ -0,0 +1,20 @@
|
||||
@extends('layouts.admin')
|
||||
|
||||
@section('title', '新建管理员')
|
||||
|
||||
@section('content')
|
||||
<h3>权力越大,责任越大</h3>
|
||||
<a class="mt-3" href="{{ route('admin.admins.index') }}">返回管理员列表</a>
|
||||
|
||||
<form method="POST" action="{{ route('admin.admins.store')}}">
|
||||
@csrf
|
||||
|
||||
<div class="form-group mt-1">
|
||||
<label for="email">Email</label>
|
||||
<input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary mt-3">提交</button>
|
||||
</form>
|
||||
|
||||
@endsection
|
36
resources/views/admin/admins/edit.blade.php
Normal file
36
resources/views/admin/admins/edit.blade.php
Normal file
@ -0,0 +1,36 @@
|
||||
@extends('layouts.admin')
|
||||
|
||||
@section('title', '管理员:' . $admin->name)
|
||||
|
||||
@section('content')
|
||||
<h3>{{ $admin->name }}</h3>
|
||||
|
||||
<form method="POST" action="{{ route('admin.admins.update', $admin)}}">
|
||||
@csrf
|
||||
@method('PATCH')
|
||||
|
||||
<div class="form-group mt-1">
|
||||
<label for="email">Email</label>
|
||||
<input type="text" class="form-control" id="email" name="email" value="{{ $admin->email }}">
|
||||
</div>
|
||||
|
||||
<div class="form-check mt-1">
|
||||
<input class="form-check-input" type="checkbox" value="1" id="reset_password" name="reset_password">
|
||||
<label class="form-check-label" for="reset_password">
|
||||
重置密码。
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary mt-3">提交</button>
|
||||
</form>
|
||||
|
||||
|
||||
<hr/>
|
||||
<form method="POST" action="{{ route('admin.admins.destroy', $admin)}}"
|
||||
onsubmit="return confirm('此管理员将不复存在。')">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="btn btn-danger">删除</button>
|
||||
</form>
|
||||
|
||||
@endsection
|
45
resources/views/admin/admins/index.blade.php
Normal file
45
resources/views/admin/admins/index.blade.php
Normal file
@ -0,0 +1,45 @@
|
||||
@extends('layouts.admin')
|
||||
|
||||
@section('title', '管理员')
|
||||
|
||||
@section('content')
|
||||
|
||||
<h3>管理员</h3>
|
||||
<p>权力越大,责任越大</p>
|
||||
<a href="{{ route('admin.admins.create') }}">新建管理员</a>
|
||||
<div class="overflow-auto">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<th>ID</th>
|
||||
<th>邮件</th>
|
||||
<th>操作</th>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
@foreach ($admins as $admin)
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{{ route('admin.admins.edit', $admin) }}">
|
||||
{{ $admin->id }}
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
{{ $admin->email }}
|
||||
</td>
|
||||
<td>
|
||||
<a href="{{ route('admin.admins.edit', $admin) }}" class="btn btn-primary btn-sm">编辑</a>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{{-- 分页 --}}
|
||||
{{ $admins->links() }}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@endsection
|
@ -19,7 +19,7 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<div id="app">
|
||||
<nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
|
||||
<div class="container">
|
||||
<a class="navbar-brand" href="{{ route('admin.index') }}">
|
||||
@ -49,6 +49,9 @@
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('admin.transactions') }}">交易记录</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('admin.admins.index') }}">管理员</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{ route('admin.commands') }}">命令速查表</a>
|
||||
</li>
|
||||
@ -66,8 +69,8 @@
|
||||
@else
|
||||
@if (Auth::guard('web')->check())
|
||||
<li class="nav-item">
|
||||
<a class="nav-link"
|
||||
href="{{ route('index') }}">切换到 {{ Auth::guard('web')->user()->name }}</a>
|
||||
<a class="nav-link" href="{{ route('index') }}">切换到
|
||||
{{ Auth::guard('web')->user()->name }}</a>
|
||||
</li>
|
||||
@endif
|
||||
<li class="nav-item dropdown">
|
||||
@ -96,14 +99,14 @@ class="d-none">
|
||||
</nav>
|
||||
|
||||
<main class="py-4">
|
||||
<x-alert/>
|
||||
<x-alert />
|
||||
|
||||
<div class="container">
|
||||
@yield('content')
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<x-module-script/>
|
||||
<x-module-script />
|
||||
|
||||
</div>
|
||||
</body>
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\Admin\AdminController;
|
||||
use App\Http\Controllers\Admin\AuthController;
|
||||
use App\Http\Controllers\Admin\HomeController;
|
||||
use App\Http\Controllers\Admin\HostController;
|
||||
@ -19,6 +20,7 @@
|
||||
Route::group([
|
||||
'middleware' => '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']);
|
||||
|
Loading…
Reference in New Issue
Block a user