<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 刪除用戶 本節將為管理員增添刪除功能,通過中間件鑒權來達到操作的目的。 ## 添加管理員 我們在之前的課程中,已經在數據庫中創建了一個 `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', '非法操作'); } } ~~~ 上述代碼中,當我們判斷到當前登入用戶是管理員時,即可直接返回下一步,即管理員擁有超級權限。 再刷新頁面并點擊刪除按鈕,現在如愿以償的工作了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看