<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] Guard 定義了用戶在每個請求中如何實現認證 # 配置 在config/auth.php中 ``` 'guards' => [ 'admin'=>[ 'driver' => 'session', //驅動 'provider' => 'admin', //由什么提供 ], ], 'providers' => [ 'admin' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], ], ``` 在Admin.php這個模型中編輯 ``` namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; class Admin extends Authenticatable { // } ``` # 使用 在控制器中編輯 ``` public function user() { //我們獲取到的要和數據庫的字段比對 //use Illuminate\Support\Facades\Auth; //laravel驗證密碼要字段是password這點還沒想到怎么改 if (Auth::guard('admin')->attempt(['username' => '1', 'password' => '123'])) { // 登錄進來 return Auth::user(); //返回用戶信息 } return 0; } ``` --- 首先創建一張文章表 `php artisan make:migration create_posts_table --create=posts` 然后創建模型,控制器.在ModelFactory.php中編輯 ```php //新增 $factory->define(App\Post::class, function (Faker\Generator $faker) { return [ 'user_id'=>factory(\App\User::class)->create()->id, 'title'=>$faker->sentence, 'body'=>$faker->paragraph, ]; }); ``` 運行 `php artisan tinker` 進入交互界面,執行 ``` factory('App\Post')->create(); ``` 生成文章的測試數據 如果使用的是laravel5.1的話,會在User.php模型中看到他use了Authorizable這個類,他會對用戶權限進行細分 在Providers/AuthServiceProvider.php中boot方法編輯 Laravel5.2 ```php public function boot(GateContract $gate) { $this->registerPolicies($gate); //這邊意思是判斷用戶是不是文章的作者,這邊的user是已經登錄進來的用戶 //如果用戶沒有登錄進來,自動判斷為錯誤,show-post為自定義的字符串 $gate->define('show-post', function($user,$post){ return $user->id==$post->user_id; }); } ``` Laravel5.4 ``` public function boot() { $this->registerPolicies(); //顯示文章.show-post自己定義 //門面 Gate::define('show-post',function ($user,$post){ //判斷這篇文章是不是這個用戶創建的 return $user->id == $post->user_id; }); } ``` 然后在路由中寫 `Route::resource('posts','PostsController');` 在 `PostsController` 控制器中編輯 ```php public function show($id) { $post=Post::findOrFail($id); //讓用戶登錄進來 \Auth::loginUsingId(1); //use Gate; if(Gate::denies('show-post',$post)){ abort(403,'sorry'); } return $post->title; } ``` 也可以這樣寫 ```php public function show($id) { $post=Post::findOrFail($id); //讓用戶登錄進來 \Auth::loginUsingId(1); $this->authorize('show-post',$post); return $post->title; } ``` 一樣可以實現. 我們還可以對Providers/AuthServiceProvider.php中boot方法,優化一下 ```php public function boot(GateContract $gate) { $this->registerPolicies($gate); $gate->define('show-post', function($user,$post){ return $user->owns($post); }); } ``` 然后我們在User.php模型中寫 ```php public function owns($post) { return $this->id==$post->user_id; } ``` # 如果做資源列表(/posts)的權限怎么做? 我沒進行思考,就直接想加到 policy 里面。 ~~~ //PostPolicy ... public function viewList(User $user, array $posts) { //todo } ... //PostController ... $this->authorize('viewList', $posts); ... ~~~ 發現不行啊,細細的查看了 Gate 的實現,給你看一個函數你就懂了: ~~~ //Gate.php protected function firstArgumentCorrespondsToPolicy(array $arguments) { if (! isset($arguments[0])) { return false; } if (is_object($arguments[0])) { return isset($this->policies[get_class($arguments[0])]); } return is_string($arguments[0]) && isset($this->policies[$arguments[0]]); } ~~~ 在 authorize 的時候,Gate 會通過第一個參數(除去 user)來判斷是哪個對象,知道了這個對象,才能找到對應的 policy class,所以一般我們這么用: ~~~ $this->authorize('update', $post); $this->authorize('create', Post::class); ~~~ 如果你想用 viewList,可以這么做: ~~~ //PostPolicy ... public function viewList(User $user, $class, array $posts) { //todo } ... //PostController ... $this->authorize('viewList', Post::class, $posts); ... ~~~ 是不是覺得很變扭,$class 這個參數我們根本就沒有用,回過頭想一想,這么做好像哪里不太對。。。。。。。。。。。 是的,Policy 本來就是針對單個對象的,我現在要控制列表的權限,正確的做法應該是這樣的: ~~~ \Gate::define('view-post-list', function ($user, $posts) { // }); ~~~ 使用 ~~~ //PostController ... $this->authorize('view-post-list', $posts); ... ~~~ 查看 Gate 的代碼,你會發現,他會優先 check policy 是否存在,不存在就會去檢查是否有定義的 abilities,如果有就會使用,貼一段代碼你就懂了: ~~~ protected function resolveAuthCallback($user, $ability, array $arguments) { if ($this->firstArgumentCorrespondsToPolicy($arguments)) { return $this->resolvePolicyCallback($user, $ability, $arguments); } elseif (isset($this->abilities[$ability])) { return $this->abilities[$ability]; } else { return function () { return false; }; } } ~~~
                  <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>

                              哎呀哎呀视频在线观看