Exemple #1
0
        public IActionResult Login([FromBody] GeekLogin request)
        {
            if (Log.IsEnabled(LogLevel.Debug))
            {
                Log.LogDebug("POST: /Api/Geeks/Ping");
            }

            try
            {
                _geekService.Login(request.Username, request.LoginCode, request.NotAliveWarningWindow,
                                   request.NotAliveDangerWindow, request.LocalTime);
            }
            catch (Exception ex) when(ex is ObjectDisposedException || ex is MySqlException)
            {
                try
                {
                    // The database connection occasionally times out when this is hosted on a free
                    // Microsoft Azure instance. The second request should work
                    _geekService.Login(request.Username, request.LoginCode, request.NotAliveWarningWindow,
                                       request.NotAliveDangerWindow, request.LocalTime);
                }
                catch (Exception ex2) when(ex2 is ObjectDisposedException || ex2 is MySqlException)
                {
                    return(StatusCode(StatusCodes.Status504GatewayTimeout));
                }
            }

            return(Ok());
        }
        public IActionResult ManualLogin(GeeksIndexViewModel viewModel)
        {
            if (Log.IsEnabled(LogLevel.Debug))
            {
                Log.LogDebug("POST: /Geeks/ManualLogin");
            }

            bool isValid = ValidateManualLogin(viewModel);

            if (!isValid)
            {
                return(View("Index", viewModel));
            }

            string successMessage =
                $"Login was successful with username {viewModel.ManualLoginUsername} " +
                $"and login code {viewModel.ManualLoginCode}";

            var geek = GetGeek(viewModel.ManualLoginUsername);

            var localTime = new DateTimeOffset(DateTime.UtcNow).ToOffset(
                TimeSpan.FromMinutes(viewModel.ClientUtcOffset));

            if (geek.DoesGeekExist)
            {
                if (geek.LoginCode != viewModel.ManualLoginCode)
                {
                    ModelState.AddModelError("ManualLoginCode", "The login code must match the previously set login code");
                    return(View("Index", viewModel));
                }

                _geekService.Login(geek.Username, geek.LoginCode, geek.NotAliveWarningWindow,
                                   geek.NotAliveDangerWindow, localTime);
            }
            else
            {
                if (!viewModel.CreateNewGeekIfMissing)
                {
                    ModelState.AddModelError("ManualLoginUsername", "No geek has been found with that username");
                    return(View("Index", viewModel));
                }

                _geekService.Login(viewModel.ManualLoginUsername, viewModel.ManualLoginCode,
                                   Geek.DefaultNotAliveWarningWindow, Geek.DefaultNotAliveDangerWindow, localTime);
            }

            return(RedirectToAction("Index", new { message = successMessage }));
        }