# 刪除用戶
本節將為管理員增添刪除功能,通過中間件鑒權來達到操作的目的。
## 添加管理員
我們在之前的課程中,已經在數據庫中創建了一個 `god` 字段,但是現在還未存在任何管理員,所以我們在上一節的 `seed` 中設置第一位用戶為管理員。
`database\seeds\Users.php`
~~~php
<?php
use think\migration\Seeder;
use app\user\model\User;
class Users extends Seeder
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$faker = Faker\Factory::create();
$data = [];
for ($i = 0; $i < 200; $i++) {
$data[] = [
'name' => $faker->userName,
'password' => $faker->password,
'email' => $faker->email,
];
}
$this->table('users')->insert($data)->save();
// allowField(false) 屏蔽 Model 中設置的保護字段 $field
User::get(1)->allowField(false)->save([
'god' => true
]);
}
}
~~~
再次運行 `php think seed:run` 并刷新數據庫,即可看到第一位用戶已經被設置為管理員
## 刪除功能
在刪除功能之前,我們需要判斷是否是管理員,在 8.4權限系統 中已經設置過當前用戶的策略,只需要再創建一個管理員策略即可。
創建文件 `application\behavior\GodPolicy.php`
~~~php
<?php
namespace app\behavior;
use think\facade\Session;
class GodPolicy
{
public function run()
{
$is_god = Session::get('user.god');
return $is_god;
}
}
~~~
接著創建中間件,在控制臺中鍵入命令 `php think make:middleware GodAuthorize`
`application\http\middleware\GodAuthorize.php`
~~~php
<?php
namespace app\http\middleware;
use think\facade\Hook;
class GodAuthorize
{
public function handle($request, \Closure $next)
{
$result = Hook::exec('app\\behavior\\GodPolicy');
return
$result
? $next($request)
: redirect('user/session/create')->with('validate', '非法操作');
}
}
~~~
`application\user\controller\Auth.php`
~~~php
...
class Auth extends Controller
{
protected $middleware = [
'UserAuthorize' => [
'except' => [
'create',
'save'
]
],
'GodAuthorize' => [
'only' => [
'delete'
]
]
];
...
~~~
通過綜上操作,我們為 `Auth` 控制臺中的 `delete` 創建了一個管理員權限校驗的方法,接下來編寫前端模板。
## 添加刪除按鈕
編輯前端模板
`resources\views\user\auth\index.blade.php`
~~~html
...
<div class="list-group list-group-flush">
@foreach ($users as $user)
<div class="list-group-item">
<a href="{{ url('user/auth/read', ['id' => $user->id]) }}">
{{ $user->name }}
</a>
<form action="{{ url('user/auth/delete', ['id' => $user->id]) }}" method="post" class="float-right">
@php echo token() @endphp
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-sm btn-danger delete-btn">刪除</button>
</form>
</div>
@endforeach
</div>
...
~~~
刷新頁面,則可以看到刪除按鈕,可是我們以任意用戶來訪問這個頁面,都能出現刪除按鈕,所以要在添加一個管理員才能查看的功能,打開控制器
`application\user\controller\Auth.php`
~~~php
...
use think\facade\Hook;
...
...
public function index()
{
$this->assign([
'users' => User::paginate(10),
'god' => Hook::exec('app\\behavior\\GodPolicy')
]);
return $this->fetch();
}
...
~~~
可以看到,我們在 8.4 花了很大功夫解耦權限判斷現在起了作用,只需要執行 `Hook` 就能快速的判斷是否通過此類操作,再也不用重復的書寫一大堆判斷條件,現在再來編寫前端頁面。
`resources\views\user\auth\index.blade.php`
~~~html
...
@if ($god)
<form action="{{ url('user/auth/delete', ['id' => $user->id]) }}" method="post" class="float-right">
@php echo token() @endphp
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-sm btn-danger delete-btn">刪除</button>
</form>
@endif
...
~~~
這時候重新注冊一個沒有管理員權限的賬號,訪問 http://thinkphp.test/user/auth/index 就不會看到有任何的刪除按鈕了,下面來編寫刪除功能。
`application\user\controller\Auth.php`
~~~php
...
public function delete($id)
{
User::where('id', $id)->where('god', false)->delete();
return redirect('user/auth/index');
}
...
~~~
請注意,我們在刪除語句中添加了一句 `where('god', false)`,這是因為管理員不能夠刪除自己。
刷新頁面,點擊刪除按鈕,卻被提示 `非法操作`,這是因為在 `UserPolicy` 的策略中,我們判斷當前登入用戶必須要和傳入用戶一致才能夠進行下一步操作,現在來修改中間件。
`application\http\middleware\UserAuthorize.php`
~~~php
<?php
namespace app\http\middleware;
use Closure;
use think\facade\Hook;
class UserAuthorize
{
public function handle($request, Closure $next)
{
$is_god = Hook::exec('app\\behavior\\GodPolicy');
if ($is_god) {
return $next($request);
}
$result = Hook::exec('app\\behavior\\UserPolicy', $request->id);
return
$result
? $next($request)
: $this->redirect();
}
private function redirect()
{
return redirect('user/session/create')->with('validate', '非法操作');
}
}
~~~
上述代碼中,當我們判斷到當前登入用戶是管理員時,即可直接返回下一步,即管理員擁有超級權限。
再刷新頁面并點擊刪除按鈕,現在如愿以償的工作了。
- 第一章. 基礎信息
- 1.1 序言
- 1.2 關于作者
- 1.3 本書源碼
- 1.4 反饋糾錯
- 1.5 安全指南
- 1.6 捐助作者
- 第二章. 開發環境布置
- 2.1 編輯器選用
- 2.2 命令行工具
- 2.3 開發環境搭建
- 2.4 瀏覽器選擇
- 2.5 第一個應用
- 2.6 Git 工作流
- 第三章. 構建頁面
- 3.1 章節說明
- 3.2 靜態頁面
- 3.3 Think 命令
- 3.4 小結
- 第四章. 優化頁面
- 4.1 章節說明
- 4.2 樣式美化
- 4.3 局部視圖
- 4.4 路由鏈接
- 4.5 用戶注冊頁面
- 4.6 集中視圖
- 4.7 小結
- 第五章. 用戶模型
- 5.1 章節說明
- 5.2 數據庫遷移
- 5.3 查看數據表
- 5.4 模型文件
- 5.5 小結
- 第六章. 用戶注冊
- 6.1 章節說明
- 6.2 注冊表單
- 6.3 用戶數據驗證
- 6.4 注冊失敗錯誤信息
- 6.5 注冊成功
- 6.6 小結
- 第七章. 會話管理
- 7.1 章節說明
- 7.2 會話
- 7.3 用戶登錄
- 7.4 退出
- 7.5 小結
- 第八章. 用戶 CRUD
- 8.1 章節說明
- 8.2 重構代碼
- 8.3 更新用戶
- 8.4 權限系統
- 8.5 列出所有用戶
- 8.6 刪除用戶
- 8.7 訪客模式
- 8.8 優化前端
- 8.9 小結
- 第九章. 微博 CRUD
- 9.1 章節說明
- 9.2 微博模型
- 9.3 顯示微博
- 9.4 發布微博
- 9.5 微博數據流
- 9.6 刪除微博
- 9.7 小結