public IHttpActionResult PunchIn([FromUri] int taskId, [FromUri] string timeZone, [FromUri] string latitude, [FromUri] string longitude, [FromUri] string sourceHardware, [FromUri] string sourceOperatingSystem, [FromUri] string sourceOperatingSystemVersion, [FromUri] string sourceBrowser, [FromUri] string sourceBrowserVersion) { string sourceHostname = ""; string sourceIpAddress = ""; try { // Attempt to get the client hostname. sourceHostname = HttpContext.Current.Request.UserHostName; } catch { } try { // Attempt to get the client IP address. sourceIpAddress = HttpContext.Current.Request.UserHostAddress; } catch { } var currentUser = CurrentUser(); var task = _context.Tasks .Include(t => t.Job.Customer) .Where(t => t.Job.Customer.OrganizationId == currentUser.OrganizationId) .Where(t => t.Id == taskId) .FirstOrDefault(); // Ensure that task was found. if (task == null) { return(NotFound()); } // Ensure job is open. if (task.Job.Status != "Open") { return(BadRequest("Cannot punch in on tasks for projects that are not open.")); } // Prevent double submission. var submission = cache.Get($"submission.punchin.{currentUser.Id}") as bool?; if (submission.HasValue) { return(BadRequest("Cannot punch in twice within 5 seconds.")); } try { var punch = repo.PunchIn( taskId, CurrentUser(), "", timeZone, latitude, longitude, sourceHardware, sourceHostname, sourceIpAddress, sourceOperatingSystem, sourceOperatingSystemVersion, sourceBrowser, sourceBrowserVersion); // Record the submission. var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.UtcNow.AddSeconds(5); cache.Set($"submission.punchin.{currentUser.Id}", true, policy); return(Ok(punch)); } catch (DbEntityValidationException ex) { string message = ""; foreach (var eve in ex.EntityValidationErrors) { foreach (var ve in eve.ValidationErrors) { message += string.Format("{0} has error '{1}'; ", ve.PropertyName, ve.ErrorMessage); } } Trace.TraceError(ex.ToString()); Trace.TraceError(ex.Message); return(BadRequest(message)); } catch (Exception ex) { Trace.TraceError(ex.ToString()); return(BadRequest(ex.Message)); } }
public IHttpActionResult PunchIn(ODataActionParameters parameters) { var taskId = (int)parameters["TaskId"]; var timezone = (string)parameters["InAtTimeZone"]; var latitudeForInAt = (string)parameters["LatitudeForInAt"]; var longitudeForInAt = (string)parameters["LongitudeForInAt"]; var sourceHardware = (string)parameters["SourceHardware"]; var sourceHostname = HttpContext.Current.Request.UserHostName; var sourceIpAddress = HttpContext.Current.Request.UserHostAddress; var sourceOperatingSystem = (string)parameters["SourceOperatingSystem"]; var sourceOperatingSystemVersion = (string)parameters["SourceOperatingSystemVersion"]; var sourceBrowser = (string)parameters["SourceBrowser"]; var sourceBrowserVersion = (string)parameters["SourceBrowserVersion"]; var currentUser = CurrentUser(); var task = db.Tasks .Include(t => t.Job.Customer) .Where(t => t.Id == taskId) .FirstOrDefault(); // Ensure task belongs to the same organization. if (currentUser.OrganizationId != task.Job.Customer.OrganizationId) { return(BadRequest()); } // Ensure job is open. if (task.Job.Status != "Open") { return(BadRequest()); } // Prevent double submission. var submission = cache.Get($"submission.punchin.{currentUser.Id}") as bool?; if (submission.HasValue) { return(BadRequest("Cannot punch in twice within 5 seconds.")); } try { var punch = repo.PunchIn( taskId, CurrentUser(), "", timezone, latitudeForInAt, longitudeForInAt, sourceHardware, sourceHostname, sourceIpAddress, sourceOperatingSystem, sourceOperatingSystemVersion, sourceBrowser, sourceBrowserVersion); // Record the submission. var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.UtcNow.AddSeconds(5); cache.Set($"submission.punchin.{currentUser.Id}", true, policy); return(Created(punch)); } catch (DbEntityValidationException e) { string message = ""; foreach (var eve in e.EntityValidationErrors) { foreach (var ve in eve.ValidationErrors) { message += string.Format("{0} has error '{1}'; ", ve.PropertyName, ve.ErrorMessage); } } return(Content(HttpStatusCode.BadRequest, message)); } catch (Exception ex) { return(Content(HttpStatusCode.BadRequest, ex.Message)); } }
public IActionResult PunchIn([FromQuery] int taskId, [FromQuery] string timeZone, [FromQuery] string latitude, [FromQuery] string longitude, [FromQuery] string sourceHardware, [FromQuery] string sourceOperatingSystem, [FromQuery] string sourceOperatingSystemVersion, [FromQuery] string sourceBrowser, [FromQuery] string sourceBrowserVersion) { var sourceHostname = ""; // Leave blank // Attempt to get the client IP address. var sourceIpAddress = $"{HttpContext.Connection.RemoteIpAddress}"; var currentUser = CurrentUser(); var task = _context.Tasks .Include(t => t.Job.Customer) .Where(t => t.Job.Customer.OrganizationId == currentUser.OrganizationId) .Where(t => t.Id == taskId) .FirstOrDefault(); // Ensure that task was found. if (task == null) { return(NotFound()); } // Ensure job is open. if (task.Job.Status != "Open") { return(BadRequest("Cannot punch in on tasks for projects that are not open.")); } // Prevent double submission. var submission = _memoryCache.Get($"submission.punchin.{currentUser.Id}") as bool?; if (submission.HasValue) { return(BadRequest("Cannot punch in twice within 5 seconds.")); } try { var repo = new PunchRepository(_context); var punch = repo.PunchIn( taskId, CurrentUser(), "", timeZone, latitude, longitude, sourceHardware, sourceHostname, sourceIpAddress, sourceOperatingSystem, sourceOperatingSystemVersion, sourceBrowser, sourceBrowserVersion); // Record the submission. _memoryCache.Set($"submission.punchin.{currentUser.Id}", true, DateTime.UtcNow.AddSeconds(5)); return(Ok(punch)); } catch (DbUpdateException ex) { Trace.TraceError(ex.ToString()); return(BadRequest(new { Message = ex.Message })); } catch (Exception ex) { Trace.TraceError(ex.ToString()); return(BadRequest(new { Message = ex.Message })); } }