Skip to content

Commit 8a2b70f

Browse files
amandiobmjamesmills
authored andcommitted
Ability to provide a lookup table to fetch the remote address. (jamesmills#13)
* Added the ability to lookup within any attribute on the request helper, by any key. * Updated README and configuration description
1 parent 34deff5 commit 8a2b70f

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ By default, the timezone will be overwritten at each login with the current user
118118

119119
By default, the date format will be `jS F Y g:i:a`. To override this configuration, you just need to change the `format` property inside the configuration file `config/timezone.php` for the desired format.
120120

121+
### Lookup Array
122+
123+
This lookup array configuration makes it possible to find the remote address of the user in any attribute inside the Laravel `request` helper, by any key. Having in mind when the key is found inside the attribute, that key will be used. By default, we use the `server` attribute with the key `REMOTE_ADDR`. To override this configuration, you just need to change the `lookup` property inside the configuration file `config/timezone.php` for the desired lookup.
124+
121125
### Underlying GeoIp Package
122126

123127
If you wish to customise the underlying `torann/geoip` package you can publish the config file by using the command below.

src/Listeners/Auth/UpdateUsersTimezone.php

+43-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class UpdateUsersTimezone
1515
*/
1616
public function handle(Login $event)
1717
{
18-
$geoip_info = geoip()->getLocation(request()->server->get('REMOTE_ADDR', null));
18+
$ip = $this->getFromLookup();
19+
$geoip_info = geoip()->getLocation($ip);
1920

2021
if (auth()->user()->timezone != $geoip_info['timezone']) {
2122
if (config('timezone.overwrite') == true || auth()->user()->timezone == null) {
@@ -31,4 +32,45 @@ public function handle(Login $event)
3132
}
3233
}
3334
}
35+
36+
/**
37+
* @return mixed
38+
*/
39+
public function getFromLookup()
40+
{
41+
$result = null;
42+
43+
foreach (config('timezone.lookup') as $type => $keys) {
44+
if (empty($keys)) {
45+
continue;
46+
}
47+
48+
$result = $this->lookup($type, $keys);
49+
50+
if (is_null($result)) {
51+
continue;
52+
}
53+
}
54+
55+
return $result;
56+
}
57+
58+
/**
59+
* @param $type
60+
* @param $keys
61+
* @return string|null
62+
*/
63+
public function lookup($type, $keys)
64+
{
65+
$value = null;
66+
67+
foreach ($keys as $key) {
68+
if (!request()->$type->has($key)) {
69+
continue;
70+
}
71+
$value = request()->$type->get($key);
72+
}
73+
74+
return $value;
75+
}
3476
}

src/config/timezone.php

+19
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,23 @@
4040

4141
'format' => 'jS F Y g:i:a',
4242

43+
/*
44+
|--------------------------------------------------------------------------
45+
| Lookup Array
46+
|--------------------------------------------------------------------------
47+
|
48+
| Here you may configure the lookup array whom it will be used to fetch the user remote address.
49+
| When a key is found inside the lookup array that key it will be used.
50+
|
51+
*/
52+
53+
'lookup' => [
54+
'server' => [
55+
'REMOTE_ADDR',
56+
],
57+
'headers' => [
58+
59+
],
60+
],
61+
4362
];

0 commit comments

Comments
 (0)