2017-06-13 132 views
1

有一種方法authorizeResource()它將特定策略應用於所有路由(索引路由除外)。有沒有辦法只在特定的路線政策,類似於應用於此功能的方法:僅授權資源的特定路由

Route::resource('photo', 'PhotoController', ['only' => [ 
    'index', 'show' 
]]); 
+2

我認爲你需要使用這個門(通過中間件保護) –

回答

0

可以逼真地在控制器中定義的中間件:

public PhotoController extends Controller { 
    public function __construct() { 
     $this->middleware("can:save,photo")->only(["save","edit"]); //You get the idea 
    } 
} 

這裏假設你已經寫了一個正確的政策(檢查https://laravel.com/docs/5.4/authorization

+0

正如我所看到的那樣,只有在*既保存又照片通過的情況下,中間件纔會通過。然而'authorizeResource()'會將正確的策略映射到每個路由(例如'create()'到'create()'和'store()'),或者我錯了嗎? – Elwin

+0

@Elwin實際上'可以:動作,照片'將從名爲'save'或'edit'的動作(​​取決於哪一個被調用)獲得名爲'$ photo'的參數,然後它將找到適用於'$ photo'的類型(例如'Photo'),並從該策略中應用一個叫做'action(User $ user,Photo $ photo)'的方法。 – apokryfos

+0

@Elwin檢查https://github.com/laravel/framework/blob /7d116dc5a008e69c97f864af79ac46ab6a8d5895/src/Illuminate/Foundation/Auth/Access/AuthorizesRequests.php#L82:authorizeResource基本上將一些路由名稱映射到一些動作,例如'edit'資源路由到'update'策略動作。如果你需要一個子集那麼你可以直接手動完成它 – apokryfos

0

是,authorizeResource accepts an $options array as a third parameter。只需傳遞null作爲第二個參數,選項的語法與路由中間件的語法相同。

public function __construct() 
{ 
    $this->authorizeResource(Photo::class, null, [ 
     'only' => ['create', 'store'], 
    ]); 
} 
+0

這似乎不適用於我。我正在運行Laravel 5.5 –

0

儘管his answer指出的@JeffPucket的only選項並沒有爲我工作。我跑Laravel 5.5,哪些工作是反邏輯:

public function __construct() 
{ 
    $this->authorizeResource(Photo::class, null, [ 
     'except' => [ 'index', 'show' ], 
    ]); 
} 

請注意,您應該傳遞給該選項不希望的行動(控制器的方法),你申請你的政策。在這種情況下,indexshow將繞過授權中間件。

只是爲了比較,這裏有來自php artisan route:list結果使用每個選項時:

只有

+--------+-----------+------------------------+-----------------+------------------------------------------------+--------------------------------------------------+ 
| Domain | Method | URI     | Name   | Action           | Middleware          | 
+--------+-----------+------------------------+-----------------+------------------------------------------------+--------------------------------------------------+ 
|  | POST  | comment    | comment.store | App\Http\Controllers\[email protected] | web,auth,can:create,App\Http\Controllers\Comment | 
|  | GET|HEAD | comment    | comment.index | App\Http\Controllers\[email protected] | web,auth,can:view,App\Http\Controllers\Comment | 
|  | GET|HEAD | comment/create   | comment.create | App\Http\Controllers\[email protected] | web,auth,can:create,App\Http\Controllers\Comment | 
|  | GET|HEAD | comment/{comment}  | comment.show | App\Http\Controllers\[email protected] | web,auth,can:view,comment      | 
|  | PUT|PATCH | comment/{comment}  | comment.update | App\Http\Controllers\[email protected] | web,auth,can:update,comment      | 
|  | DELETE | comment/{comment}  | comment.destroy | App\Http\Controllers\[email protected] | web,auth,can:delete,comment      | 
|  | GET|HEAD | comment/{comment}/edit | comment.edit | App\Http\Controllers\[email protected] | web,auth,can:update,comment      | 
+--------+-----------+------------------------+-----------------+------------------------------------------------+--------------------------------------------------+ 

除了

+--------+-----------+------------------------+-----------------+------------------------------------------------+--------------------------------------------------+ 
| Domain | Method | URI     | Name   | Action           | Middleware          | 
+--------+-----------+------------------------+-----------------+------------------------------------------------+--------------------------------------------------+ 
|  | POST  | comment    | comment.store | App\Http\Controllers\[email protected] | web,auth,can:create,App\Http\Controllers\Comment | 
|  | GET|HEAD | comment    | comment.index | App\Http\Controllers\CommentController[email protected] | web,auth           | 
|  | GET|HEAD | comment/create   | comment.create | App\Http\Controllers\[email protected] | web,auth,can:create,App\Http\Controllers\Comment | 
|  | GET|HEAD | comment/{comment}  | comment.show | App\Http\Controllers\[email protected] | web,auth           | 
|  | PUT|PATCH | comment/{comment}  | comment.update | App\Http\Controllers\[email protected] | web,auth,can:update,comment      | 
|  | DELETE | comment/{comment}  | comment.destroy | App\Http\Controllers\[email protected] | web,auth,can:delete,comment      | 
|  | GET|HEAD | comment/{comment}/edit | comment.edit | App\Http\Controllers\[email protected] | web,auth,can:update,comment      | 
+--------+-----------+------------------------+-----------------+------------------------------------------------+--------------------------------------------------+ 

正如你可以在上面看到,該中間件只適用於使用的特定路由。

也許這是一個框架中的錯誤。但很難確認,因爲這個選項似乎沒有記錄。即使詳細信息authorizeResource()方法不存在。