1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JamesMills\LaravelTimezone\Listeners\Auth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Events\Login; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Laravel\Passport\Events\AccessTokenCreated; |
|
|
|
|
8
|
|
|
use Torann\GeoIP\Location; |
9
|
|
|
|
10
|
|
|
class UpdateUsersTimezone |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Handle the event. |
14
|
|
|
* |
15
|
|
|
* @return void |
16
|
|
|
*/ |
17
|
|
|
public function handle($event) |
18
|
|
|
{ |
19
|
|
|
$user = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* If the event is AccessTokenCreated, |
23
|
|
|
* we logged the user and return, |
24
|
|
|
* stopping the execution. |
25
|
|
|
* |
26
|
|
|
* The Auth::loginUsingId dispatches a Login event, |
27
|
|
|
* making this listener be called again. |
28
|
|
|
*/ |
29
|
|
|
if ($event instanceof AccessTokenCreated) { |
30
|
|
|
Auth::loginUsingId($event->userId); |
31
|
|
|
|
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* If the event is Login, we get the user from the web guard. |
37
|
|
|
*/ |
38
|
|
|
if ($event instanceof Login) { |
39
|
|
|
$user = Auth::user(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* If no user is found, we just return. Nothing to do here. |
44
|
|
|
*/ |
45
|
|
|
if (is_null($user)) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$ip = $this->getFromLookup(); |
50
|
|
|
$geoip_info = geoip()->getLocation($ip); |
51
|
|
|
|
52
|
|
|
if ($user->timezone != $geoip_info['timezone']) { |
|
|
|
|
53
|
|
|
if (config('timezone.overwrite') == true || $user->timezone == null) { |
54
|
|
|
$user->timezone = $geoip_info['timezone'] ?? $geoip_info->time_zone['name']; |
55
|
|
|
$user->save(); |
56
|
|
|
|
57
|
|
|
$this->notify($geoip_info); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Location $geoip_info |
64
|
|
|
*/ |
65
|
|
|
private function notify(Location $geoip_info) |
66
|
|
|
{ |
67
|
|
|
if (request()->hasSession() && config('timezone.flash') == 'off') { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$message = sprintf(config('timezone.message', 'We have set your timezone to %s'), $geoip_info['timezone']); |
72
|
|
|
|
73
|
|
|
if (config('timezone.flash') == 'laravel') { |
74
|
|
|
request()->session()->flash('success', $message); |
75
|
|
|
|
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (config('timezone.flash') == 'laracasts') { |
80
|
|
|
flash()->success($message); |
|
|
|
|
81
|
|
|
|
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (config('timezone.flash') == 'mercuryseries') { |
86
|
|
|
flashy()->success($message); |
|
|
|
|
87
|
|
|
|
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (config('timezone.flash') == 'spatie') { |
92
|
|
|
flash()->success($message); |
93
|
|
|
|
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (config('timezone.flash') == 'mckenziearts') { |
98
|
|
|
notify()->success($message); |
|
|
|
|
99
|
|
|
|
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (config('timezone.flash') == 'tall-toasts') { |
104
|
|
|
toast()->success($message)->pushOnNextPage(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
private function getFromLookup() |
114
|
|
|
{ |
115
|
|
|
$result = null; |
116
|
|
|
|
117
|
|
|
foreach (config('timezone.lookup') as $type => $keys) { |
118
|
|
|
if (empty($keys)) { |
119
|
|
|
continue; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$result = $this->lookup($type, $keys); |
123
|
|
|
|
124
|
|
|
if (is_null($result)) { |
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param $type |
134
|
|
|
* @param $keys |
135
|
|
|
* @return string|null |
136
|
|
|
*/ |
137
|
|
|
private function lookup($type, $keys) |
138
|
|
|
{ |
139
|
|
|
$value = null; |
140
|
|
|
|
141
|
|
|
foreach ($keys as $key) { |
142
|
|
|
if (! request()->$type->has($key)) { |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
|
|
$value = request()->$type->get($key); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $value; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths