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