06.10.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Authorization 231<br />

15 * @return void<br />

16 */<br />

17 public function boot(GateContract $gate)<br />

18 {<br />

19 $this->registerPolicies($gate);<br />

20<br />

21 $gate->define('update-post', function ($user, $post) {<br />

22 return $user->id === $post->user_id;<br />

23 });<br />

24 }<br />

25 }<br />

Note that we did not check if the given $user is not NULL. The Gate will automatically return false<br />

for all abilities when there is not an authenticated user or a specific user has not been specified<br />

using the forUser method.<br />

Class Based Abilities<br />

In addition to registering Closures as authorization callbacks, you may register class methods by<br />

passing a string containing the class name and the method. When needed, the class will be resolved<br />

via the service container:<br />

1 $gate->define('update-post', 'Class@method');<br />

#### Intercepting Authorization Checks {#authorizationintercepting-authorization-checks}<br />

Sometimes, you may wish to grant all abilities to a specific user. For this situation, use the before<br />

method to define a callback that is run before all other authorization checks:<br />

1 $gate->before(function ($user, $ability) {<br />

2 if ($user->isSuperAdmin()) {<br />

3 return true;<br />

4 }<br />

5 });<br />

If the before callback returns a non-null result that result will be considered the result of the check.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!