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