From 51fccbdda409a70f62ac92aa94cc69063cf0197b Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 3 Mar 2017 18:28:13 -0800 Subject: [PATCH] Added last login to user menu --- app/Http/Controllers/Api/UsersController.php | 3 ++- app/Http/Controllers/Auth/LoginController.php | 8 +++++++- app/Http/Transformers/UsersTransformer.php | 1 + app/Presenters/UserPresenter.php | 9 +++++++++ resources/lang/en/general.php | 1 + 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 9fb405f438..c341ee1309 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -36,6 +36,7 @@ class UsersController extends Controller 'users.created_at', 'users.notes', 'users.company_id', + 'users.last_login', 'users.deleted_at', 'users.activated' ])->with('manager', 'groups', 'userloc', 'company', 'throttle','assets','licenses','accessories','consumables') @@ -67,7 +68,7 @@ class UsersController extends Controller [ 'last_name','first_name','email','jobtitle','username','employee_num', 'assets','accessories', 'consumables','licenses','groups','activated','created_at', - 'two_factor_enrolled','two_factor_optin' + 'two_factor_enrolled','two_factor_optin','last_login' ]; $sort = in_array($request->get('sort'), $allowed_columns) ? $request->get('sort') : 'first_name'; diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index dbfc853914..7a96aa3d53 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -156,10 +156,16 @@ class LoginController extends Controller LOG::debug("Local authentication failed."); return redirect()->back()->withInput()->with('error', trans('auth/message.account_not_found')); } else { + $this->clearLoginAttempts($request); } } - + + if ($user = Auth::user()) { + $user->last_login = \Carbon::now(); + \Log::debug('Last login:'.$user->last_login); + $user->save(); + } // Redirect to the users page return redirect()->intended()->with('success', trans('auth/message.signin.success')); } diff --git a/app/Http/Transformers/UsersTransformer.php b/app/Http/Transformers/UsersTransformer.php index f21aba3448..eb791a23a1 100644 --- a/app/Http/Transformers/UsersTransformer.php +++ b/app/Http/Transformers/UsersTransformer.php @@ -43,6 +43,7 @@ class UsersTransformer 'company' => ($user->company) ? ['id' => $user->company->id,'name'=> e($user->company->name)] : null, 'created_at' => Helper::getFormattedDateObject($user->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($user->updated_at, 'datetime'), + 'last_login' => Helper::getFormattedDateObject($user->last_login, 'datetime'), ]; $permissions_array['available_actions'] = [ diff --git a/app/Presenters/UserPresenter.php b/app/Presenters/UserPresenter.php index 1f8045d4c3..1c0ecf85ae 100644 --- a/app/Presenters/UserPresenter.php +++ b/app/Presenters/UserPresenter.php @@ -176,6 +176,15 @@ class UserPresenter extends Presenter "visible" => false, 'formatter' => 'dateDisplayFormatter' ], + [ + "field" => "last_login", + "searchable" => false, + "sortable" => true, + "switchable" => true, + "title" => trans('general.last_login'), + "visible" => false, + 'formatter' => 'dateDisplayFormatter' + ], [ "field" => "actions", "searchable" => false, diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index 58c6bb1f8d..a972ceb2eb 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -88,6 +88,7 @@ 'insufficient_permissions' => 'Insufficient permissions!', 'language' => 'Language', 'last' => 'Last', + 'last_login' => 'Last Login', 'last_name' => 'Last Name', 'license' => 'License', 'license_report' => 'License Report',