middleware('guest')->except('logout'); } public function toLogin() { return redirect()->to('/login'); } /** * Show the application's login form. * * Preserve an explicit `redirect` target (e.g. /techne-form after an * expired session) so the user is returned to it after authenticating. * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function showLoginForm() { $redirect = request()->input('redirect'); if (is_string($redirect)) { $parts = parse_url($redirect); // Only accept safe, internal paths (rejects //evil.com, http://..., etc). if (!empty($parts['path']) && empty($parts['scheme']) && empty($parts['host']) && str_starts_with($parts['path'], '/')) { session(['url.intended' => $parts['path']]); } } return view('auth.login'); } /** * Check is user is activated on login * * @param Request $request */ protected function validateLogin(Request $request) { if (config('bap.GOOGLE_RECAPTCHA_KEY')) { $this->validate($request, [ 'g-recaptcha-response' => 'required|recaptcha' ]); } $this->validate($request, [ $this->username() => 'required|exists:users,' . $this->username() . ',is_active,1', 'password' => 'required', ], [ $this->username() . '.exists' => trans('core::core.invalid_login') ]); } /** * Register Logged In Activity * * @param Request $request * @param $user * @return \Illuminate\Http\RedirectResponse */ public function authenticated(Request $request, User $user) { if (!$user->isAdmin()) { if (!$user->is_active) { $this->guard()->logout(); $request->session()->invalidate(); return redirect('/login')->withErrors([ 'email' => trans('core::core.your_account_is_not_active') ]); } if (!$user->company->is_enabled) { $this->guard()->logout(); $request->session()->invalidate(); return redirect('/login')->withErrors([ 'email' => trans('core::core.your_company_is_not_active') ]); } } // Attendance events are recorded only in the `attendance` table (via // ami:flush-events), not in activity_log. Redis::connection('ami_events')->rpush('ami:peer_events:list', json_encode([ 'action' => 'LOGGED_IN', 'extension' => (string) $user->id, 'name' => 'login-logout', 'datetime' => Carbon::now()->format('Y-m-d H:i:s'), 'ip_address'=> $request->ip(), ])); // create login event $this->createLoginEvent($user); // Send users back to the page they were trying to reach (e.g. /techne-form) // before falling back to the default post-login destination. if ($user->hasPermissionTo('reporting.browse') && !$request->session()->has('url.intended')) { return redirect()->to('/reporting'); } } public function createLoginEvent($user) { $calendarId = $this->getOrCreateDefaultCalendar(); $event = Event::where('owned_by_id', $user->id)->where('owned_by_type', User::class) ->where('calendar_id', $calendarId) ->where('updated_at','>', Carbon::now()->subHours(8)->format('Y-m-d H:i:s')) ->whereRaw('start_date = DATE_SUB(end_date, INTERVAL 1 SECOND)') ->where('event_status_id', 2) ->where('description', 'LogIn') ->first(); if(!$event){ try { $start_date = Carbon::now()->format('Y-m-d H:i:s'); $end_date = Carbon::now()->addSecond()->format('Y-m-d H:i:s'); $event = new Event(); $event->name = $user->id . " - " . $user->name; $event->start_date = $start_date; $event->end_date = $end_date; $event->full_day = false; $event->event_color = '#7fc82b'; $event->changeOwnerTo($user); $event->calendar_id = $calendarId; $event->event_priority_id = 1; $event->event_status_id = 2; $event->description = 'LogIn'; $event->company_id = 1; $event->save(); } catch (\Throwable $th) { echo $th; } } } public function updateLoginEvent($user){ $calendar = Calendar::where('name', 'Attendees Calendar') ->first(); $time = date('Y-m-d H:i:s'); try { $event = Event::where('owned_by_id', $user->id)->where('owned_by_type', User::class) ->where('calendar_id', $calendar->id) // ->whereRaw('start_date = DATE_SUB(end_date, INTERVAL 1 SECOND)') // ->where('event_status_id', 2) // ->where('description', 'LogIn') ->orderBy('updated_at', 'DESC') ->first(); $event->end_date = $time; $event->event_color = '#6d0d0d'; $event->description = 'LogIn - LogOut'; $event->event_status_id = 4; $event->save(); } catch (\Throwable $th) { // echo $th; } } public function getOrCreateDefaultCalendar() { $calendar = Calendar::where('name', 'Attendees Calendar') ->first(); if (empty($calendar)) { $calendar = new Calendar(); $calendar->name = 'Attendees Calendar'; $calendar->is_public = false; $calendar->default_view = Calendar::MONT_VIEW; $calendar->first_day = Calendar::WEEK_START_AT_MONDAY; $calendar->day_start_at = Calendar::DEFAULT_DAT_START_AT; $calendar->company_id = Landlord::getTenants()->first(); $calendar->save(); } return $calendar->id; } /** * Regiser logged Out * * @param Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function logout(Request $request) { // Attendance events are recorded only in the `attendance` table (via // ami:flush-events), not in activity_log. Redis::connection('ami_events')->rpush('ami:peer_events:list', json_encode([ 'action' => 'LOGGED_OUT', 'extension' => (string) \Auth::user()->id, 'name' => 'login-logout', 'datetime' => Carbon::now()->format('Y-m-d H:i:s'), 'ip_address'=> $request->ip(), ])); // dd(\Auth::user()); $this->updateLoginEvent(\Auth::user()); $this->guard()->logout(); $request->session()->invalidate(); return redirect(route('dashboard')); } }