Skip to content

Commit 66bd119

Browse files
committed
guest user documentation
1 parent 566e481 commit 66bd119

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

authorization.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Writing Policies](#writing-policies)
1212
- [Policy Methods](#policy-methods)
1313
- [Methods Without Models](#methods-without-models)
14+
- [Guest Users](#guest-users)
1415
- [Policy Filters](#policy-filters)
1516
- [Authorizing Actions Using Policies](#authorizing-actions-using-policies)
1617
- [Via The User Model](#via-the-user-model)
@@ -235,6 +236,33 @@ When defining policy methods that will not receive a model instance, such as a `
235236
//
236237
}
237238

239+
<a name="guest-users"></a>
240+
### Guest Users
241+
242+
By default, all gates and policies automatically return `false` if the incoming HTTP request was not initiated by an authenticated user. However, you may allow these authorization checks to pass through to your gates and policies by declaring an "optional" type-hint or supplying a `null` default value for the user argument definition:
243+
244+
<?php
245+
246+
namespace App\Policies;
247+
248+
use App\User;
249+
use App\Post;
250+
251+
class PostPolicy
252+
{
253+
/**
254+
* Determine if the given post can be updated by the user.
255+
*
256+
* @param \App\User $user
257+
* @param \App\Post $post
258+
* @return bool
259+
*/
260+
public function update(?User $user, Post $post)
261+
{
262+
return $user->id === $post->user_id;
263+
}
264+
}
265+
238266
<a name="policy-filters"></a>
239267
### Policy Filters
240268

0 commit comments

Comments
 (0)