public UserErrorModel LoginUser(LoginModel login) { var errors = new UserErrorModel(); if (login == null) { errors.Messages.Add("Invalid data."); return(errors); } var email = login.Username; var password = login.Password; if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password)) { errors.Messages.Add("Invalid username/password."); return(errors); } var user = DbContext.GetUsers() .SingleOrDefault(u => string.Equals(email, u.Email, StringComparison.OrdinalIgnoreCase)); if (user != null && string.Equals(user.Password, password, StringComparison.Ordinal)) { errors.UserId = user.UserId; errors.IsProvider = DbContext.GetProviders().Any(p => p.ProviderId == user.UserId); } else { errors.Messages.Add("Invalid username/password."); } return(errors); }
/// <summary> /// If the request is an AJAX one and the exception is of type <see cref="UserException"/> /// or a descendant of <see cref="AccessDeniedException"/> or of <see cref="IntegrityViolationException"/>, /// transform it into a <see cref="UserErrorModel"/> and return it as a JSON /// with an appropriate error HTTP status code. /// </summary> /// <param name="filterContext">The context containing the exception.</param> /// <remarks> /// <see cref="AccessDeniedException"/> and descendants yeild HTTP status code 403 "Forbidden". /// <see cref="UniqueConstraintViolationException"/> and <see cref="ReferentialConstraintViolationException"/> /// yield HTTP Status 409 "Conflict". /// All other rexceptions result to 500 "Internal Server Error". /// </remarks> public void OnException(ExceptionContext filterContext) { if (!IsInteractiveRequest(filterContext)) { var exception = filterContext.Exception; UserErrorModel userErrorModel = null; var statusCode = HttpStatusCode.InternalServerError; if (exception is AccessDeniedException || exception is IntegrityViolationException) { string userMessage; switch (exception) { case AccessDeniedException _: case AccessDeniedDomainException _: statusCode = HttpStatusCode.Forbidden; userMessage = ErrorMessages.ACCESS_DENIED; var telemetry = new Microsoft.ApplicationInsights.TelemetryClient(); telemetry.TrackException(exception); break; case UniqueConstraintViolationException _: statusCode = HttpStatusCode.Conflict; userMessage = ErrorMessages.UNIQUENESS_CONSTRAINT_VIOLATION; break; case ReferentialConstraintViolationException _: statusCode = HttpStatusCode.Conflict; userMessage = ErrorMessages.RELATIONAL_CONSTRAINT_VIOLATION; break; default: statusCode = HttpStatusCode.InternalServerError; userMessage = ErrorMessages.GENERIC_ERROR; break; } userErrorModel = new UserErrorModel(userMessage); } else if (exception is UserException userException) { userErrorModel = new UserErrorModel(userException); } if (userErrorModel != null) { filterContext.Result = new JsonResult(userErrorModel); filterContext.HttpContext.Response.StatusCode = (int)statusCode; filterContext.ExceptionHandled = true; } } }
private static async Task <RecoveryOptionResultModel> HandleUserError(this UserErrorModel informationalUserError) { var r = await UserError.Throw(informationalUserError.MapTo <UserError>()); return(ConvertOption(r)); }
/// <summary> /// If the request is an AJAX one and the exception is of type <see cref="UserException"/> /// or a descendant of <see cref="AccessDeniedException"/> or of <see cref="IntegrityViolationException"/>, /// transform it into a <see cref="UserErrorModel"/> and return it as a JSON /// with an appropriate error HTTP status code. /// </summary> /// <param name="filterContext">The context containing the exception.</param> /// <remarks> /// <see cref="AccessDeniedException"/> and descendants yeild HTTP status code 403 "Forbidden". /// <see cref="UniqueConstraintViolationException"/> and <see cref="ReferentialConstraintViolationException"/> /// yield HTTP Status 409 "Conflict". /// All other rexceptions result to 500 "Internal Server Error". /// </remarks> public void OnException(ExceptionContext filterContext) { if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest") { var exception = filterContext.Exception; UserErrorModel userErrorModel = null; var statusCode = HttpStatusCode.InternalServerError; if (exception is AccessDeniedException || exception is IntegrityViolationException) { string userMessage; switch (exception) { case AccessDeniedException accessDeniedException: statusCode = HttpStatusCode.Forbidden; userMessage = ErrorMessages.ACCESS_DENIED; var telemetry = new Microsoft.ApplicationInsights.TelemetryClient(); telemetry.TrackException(exception); break; case UniqueConstraintViolationException uniqueConstraintViolationException: statusCode = HttpStatusCode.Conflict; userMessage = ErrorMessages.UNIQUENESS_CONSTRAINT_VIOLATION; break; case ReferentialConstraintViolationException referentialConstraintViolationException: statusCode = HttpStatusCode.Conflict; userMessage = ErrorMessages.RELATIONAL_CONSTRAINT_VIOLATION; break; default: statusCode = HttpStatusCode.InternalServerError; userMessage = ErrorMessages.GENERIC_ERROR; break; } userErrorModel = new UserErrorModel(userMessage); } else if (exception is UserException userException) { userErrorModel = new UserErrorModel(userException); } if (userErrorModel != null) { filterContext.Result = new JsonResult { Data = userErrorModel, ContentEncoding = Encoding.UTF8, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; filterContext.HttpContext.Response.StatusCode = (int)statusCode; filterContext.ExceptionHandled = true; } } }
public UserErrorModel RegisterUser(RegisterModel model) { var errors = new UserErrorModel(); if (model == null) { errors.Messages.Add("Invalid data."); return(errors); } if (model.UserInfo == null) { errors.Messages.Add("Invalid user data."); return(errors); } if ( DbContext.GetUsers() .Any( u => string.Equals(u.Email, model.UserInfo.Email, StringComparison.OrdinalIgnoreCase) || string.Equals(u.PhoneNumber, model.UserInfo.PhoneNumber, StringComparison.OrdinalIgnoreCase))) { errors.Messages.Add("The email address or phone number you are registering is not available. Please choose another email and phone number."); return(errors); } var newUser = model.UserInfo.ToEntity(); newUser.UserStatus = UserStatus.PendingVerification; var newProvider = default(ProviderEntity); if (model.ProviderInfo != null) { newProvider = model.ProviderInfo.ToEntity(); var coordinates = GeoServiceProvider.FindGeoLocationByAddress(model.ProviderInfo.Address.StateOrProvince, model.ProviderInfo.Address.ZipCode, model.ProviderInfo.Address.City, model.ProviderInfo.Address.FullAddressLine); if (coordinates == null || coordinates.Length != 2) { errors.Messages.Add("Cannot verify the provider address"); newProvider.ProviderStatus = ProviderStatus.FailedOnVerifyAddress; } else { newProvider.GeoLatitude = coordinates[0]; newProvider.GeoLongitude = coordinates[1]; newProvider.ProviderStatus = ProviderStatus.Verified; } } try { DbContext.SaveUsers(new List <UserEntity> { newUser }); errors.UserId = DbContext.GetUsers().Single(u => string.Equals(u.Email, newUser.Email, StringComparison.OrdinalIgnoreCase)).UserId; if (newProvider != null) { newProvider.ProviderId = errors.UserId; DbContext.SaveProviders(new List <ProviderEntity> { newProvider }); errors.IsProvider = true; } return(errors); } catch (Exception ex) { return(new UserErrorModel { Messages = new List <string> { ex.Message } }); } }