1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Detectors; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelTracker\Contracts\Detectors\UserDetector as UserDetectorContract; |
4
|
|
|
use Illuminate\Contracts\Foundation\Application; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class UserDetector |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelTracker\Detectors |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class UserDetector implements UserDetectorContract |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** @var \Illuminate\Contracts\Foundation\Application */ |
19
|
|
|
private $app; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
private $auths = []; |
23
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
25
|
|
|
| Constructor |
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
27
|
|
|
*/ |
28
|
|
|
/** |
29
|
|
|
* UserDetector constructor. |
30
|
|
|
* |
31
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Application $app) |
34
|
|
|
{ |
35
|
|
|
$this->app = $app; |
36
|
|
|
|
37
|
|
|
$this->instantiateAuthentication(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/* ------------------------------------------------------------------------------------------------ |
41
|
|
|
| Getters & Setters |
42
|
|
|
| ------------------------------------------------------------------------------------------------ |
43
|
|
|
*/ |
44
|
|
|
/** |
45
|
|
|
* Get the config instance. |
46
|
|
|
* |
47
|
|
|
* @return \Illuminate\Contracts\Config\Repository |
48
|
|
|
*/ |
49
|
|
|
private function config() |
50
|
|
|
{ |
51
|
|
|
return $this->app['config']; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the tracker config. |
56
|
|
|
* |
57
|
|
|
* @param string $key |
58
|
|
|
* @param mixed|null $default |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
protected function getConfig($key, $default = null) |
63
|
|
|
{ |
64
|
|
|
return $this->config()->get("laravel-tracker.$key", $default); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/* ------------------------------------------------------------------------------------------------ |
68
|
|
|
| Main Functions |
69
|
|
|
| ------------------------------------------------------------------------------------------------ |
70
|
|
|
*/ |
71
|
|
|
/** |
72
|
|
|
* Check if the user is authenticated. |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function check() |
77
|
|
|
{ |
78
|
|
|
return $this->executeAuthMethod($this->getConfig('auth.methods.check')); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the authenticated user. |
83
|
|
|
* |
84
|
|
|
* @return false|mixed |
85
|
|
|
*/ |
86
|
|
|
public function user() |
87
|
|
|
{ |
88
|
|
|
return $this->executeAuthMethod($this->getConfig('auth.methods.user')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the current authenticated user id. |
93
|
|
|
* |
94
|
|
|
* @return int|null |
95
|
|
|
*/ |
96
|
|
|
public function getCurrentUserId() |
97
|
|
|
{ |
98
|
|
|
return $this->check() |
99
|
|
|
? $this->user()->{$this->getConfig('auth.columns.id', 'id')} |
100
|
|
|
: null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/* ------------------------------------------------------------------------------------------------ |
104
|
|
|
| Other Functions |
105
|
|
|
| ------------------------------------------------------------------------------------------------ |
106
|
|
|
*/ |
107
|
|
|
/** |
108
|
|
|
* Grab all the authentication bindings. |
109
|
|
|
*/ |
110
|
|
|
private function instantiateAuthentication() |
111
|
|
|
{ |
112
|
|
|
foreach ((array) $this->getConfig('auth.bindings', []) as $binding) { |
113
|
|
|
$this->auths[] = $this->app->make($binding); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Execute the auth method. |
119
|
|
|
* |
120
|
|
|
* @param string $method |
121
|
|
|
* |
122
|
|
|
* @return mixed|false |
123
|
|
|
*/ |
124
|
|
|
private function executeAuthMethod($method) |
125
|
|
|
{ |
126
|
|
|
foreach ($this->auths as $auth) { |
127
|
|
|
if (is_callable([$auth, $method], true)) { |
128
|
|
|
if ($data = $auth->{$method}()) return $data; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|