1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Auth;
|
4
|
|
5
|
use Illuminate\Http\Request;
|
6
|
use Illuminate\Contracts\Auth\Guard;
|
7
|
|
8
|
class RequestGuard implements Guard
|
9
|
{
|
10
|
use GuardHelpers;
|
11
|
|
12
|
/**
|
13
|
* The guard callback.
|
14
|
*
|
15
|
* @var callable
|
16
|
*/
|
17
|
protected $callback;
|
18
|
|
19
|
/**
|
20
|
* The request instance.
|
21
|
*
|
22
|
* @var \Illuminate\Http\Request
|
23
|
*/
|
24
|
protected $request;
|
25
|
|
26
|
/**
|
27
|
* Create a new authentication guard.
|
28
|
*
|
29
|
* @param callable $callback
|
30
|
* @param \Illuminate\Http\Request $request
|
31
|
* @return void
|
32
|
*/
|
33
|
public function __construct(callable $callback, Request $request)
|
34
|
{
|
35
|
$this->request = $request;
|
36
|
$this->callback = $callback;
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Get the currently authenticated user.
|
41
|
*
|
42
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
43
|
*/
|
44
|
public function user()
|
45
|
{
|
46
|
// If we've already retrieved the user for the current request we can just
|
47
|
// return it back immediately. We do not want to fetch the user data on
|
48
|
// every call to this method because that would be tremendously slow.
|
49
|
if (! is_null($this->user)) {
|
50
|
return $this->user;
|
51
|
}
|
52
|
|
53
|
return $this->user = call_user_func($this->callback, $this->request);
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Validate a user's credentials.
|
58
|
*
|
59
|
* @param array $credentials
|
60
|
* @return bool
|
61
|
*/
|
62
|
public function validate(array $credentials = [])
|
63
|
{
|
64
|
return ! is_null((new static(
|
65
|
$this->callback, $credentials['request']
|
66
|
))->user());
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Set the current request instance.
|
71
|
*
|
72
|
* @param \Illuminate\Http\Request $request
|
73
|
* @return $this
|
74
|
*/
|
75
|
public function setRequest(Request $request)
|
76
|
{
|
77
|
$this->request = $request;
|
78
|
|
79
|
return $this;
|
80
|
}
|
81
|
}
|