Skip to content

Commit

Permalink
Add setting for recording IP address (#1221)
Browse files Browse the repository at this point in the history
* Add setting for recording IP address

* Record IP on registration
  • Loading branch information
nabeelio committed Jun 4, 2021
1 parent db532e0 commit 82825ef
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/Database/seeds/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@
options: ''
type: text
description: 'Enter your Google Analytics Tracking ID'
- key: general.record_user_ip
name: 'Record user IP address'
group: general
value: true
options: ''
type: boolean
description: Record the user's IP address on register/login
- key: units.currency
name: 'Currency'
group: units
Expand Down
10 changes: 8 additions & 2 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Contracts\Controller;
use App\Exceptions\PilotIdNotFound;
use App\Models\Enums\UserState;
use App\Models\User;
use App\Services\UserService;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -104,11 +105,16 @@ protected function validateLogin(Request $request)
*/
protected function sendLoginResponse(Request $request)
{
/** @var User $user */
$user = Auth::user();

if (setting('general.record_user_ip', true)) {
$user->last_ip = $request->ip();
$user->save();
}

if ($user->state !== UserState::ACTIVE && $user->state !== UserState::ON_LEAVE) {
Log::info('Trying to login '.$user->ident.', state '
.UserState::label($user->state));
Log::info('Trying to login '.$user->ident.', state '.UserState::label($user->state));

// Log them out
$this->guard()->logout();
Expand Down
9 changes: 7 additions & 2 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ protected function validator(array $data)
*
* @return User
*/
protected function create(array $opts)
protected function create(Request $request): User
{
// Default options
$opts = $request->all();
$opts['password'] = Hash::make($opts['password']);

if (setting('general.record_user_ip', true)) {
$opts['last_ip'] = $request->ip();
}

// Convert transfer hours into minutes
if (isset($opts['transfer_time'])) {
$opts['transfer_time'] *= 60;
Expand Down Expand Up @@ -158,7 +163,7 @@ public function register(Request $request)
{
$this->validator($request->all())->validate();

$user = $this->create($request->all());
$user = $this->create($request);
if ($user->state === UserState::PENDING) {
return view('auth.pending');
}
Expand Down
3 changes: 3 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @property int rank_id
* @property string discord_id
* @property int state
* @property string last_ip
* @property bool opt_in
* @property Pirep[] pireps
* @property string last_pirep_id
Expand Down Expand Up @@ -82,6 +83,7 @@ class User extends Authenticatable
'status',
'toc_accepted',
'opt_in',
'last_ip',
'created_at',
'updated_at',
];
Expand All @@ -93,6 +95,7 @@ class User extends Authenticatable
'api_key',
'discord_id',
'password',
'last_ip',
'remember_token',
];

Expand Down
4 changes: 4 additions & 0 deletions resources/views/admin/users/fields.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@
<td>Flight Time</td>
<td>@minutestotime($user->flight_time)</td>
</tr>
<tr>
<td>IP Address</td>
<td>{{ $user->last_ip ?? '-' }}</td>
</tr>
<tr>
<td>Registered On</td>
<td>{{ show_datetime($user->created_at) }}</td>
Expand Down

0 comments on commit 82825ef

Please sign in to comment.