using Neo.LegitimateLicences.Api.Helpers; using Neo.LegitimateLicences.Common; using Neo.LegitimateLicences.Data.Models; using Newtonsoft.Json.Linq; using Neo.Afx.Services.Documents; using Neo.Afx.Services.Documents.Entities; using Neo.Afx.Services.Security; using Neo.Afx.Services.Workflows; using Neo.Afx.Utilities; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Security.Claims; using System.Threading; using System.Web.Http; using System.Web.Http.Cors; using DPUruNet; namespace Neo.LegitimateLicences.Api { [EnableCors(origins: "*", headers: "*", methods: "*")] public partial class LegitimateLicencesApiController : ApiController { private const int DPFJ_PROBABILITY_ONE = 0x7fffffff; #region ApiErrors private const string Error_ClaimDetailsNotFound = "#EXPOSE_ERROR#User details not found."; #endregion #region Default constructor public LegitimateLicencesApiController() { } #endregion #region IsValidClaim public bool IsValidClaim { get { var currentUser = (ClaimsIdentity)Thread.CurrentPrincipal.Identity; if (currentUser != null) { var userClaim = currentUser.Claims.Where(a => a.Type == "UserID").FirstOrDefault(); if (userClaim == null) { return false; } else { return true; } } else { return false; } } } #endregion #region UserID public long UserID { get { var userID = -1; var identity = ((ClaimsIdentity)Thread.CurrentPrincipal.Identity); if (identity != null) { userID = Convert.ToInt32(identity.Claims.Where(a => a.Type == "UserID").FirstOrDefault().Value); } return userID; } } #endregion #region UserEmail public string UserEmail { get { var email = ""; var identity = ((ClaimsIdentity)Thread.CurrentPrincipal.Identity); if (identity != null) { email = identity.Claims.Where(a => a.Type == ClaimTypes.Email).FirstOrDefault().Value; } return email; } } #endregion #region Ping [Route("~/1.0/ping")] [HttpGet] public JsonBaseResult Ping() { var result = new JsonBaseResult(); if (!IsValidClaim) { string error = Error_ClaimDetailsNotFound.Replace("#EXPOSE_ERROR#", ""); throw new Exception(error); } else { result.Success = true; } return result; } #endregion #region GetError public string GetError(Exception ex) { string error = "An error has occured"; //ex = ErrorUtilities.GetInnerMostError(ex); if (!String.IsNullOrEmpty(ex.Message)) { if (ex.Message.StartsWith("#EXPOSE_ERROR#")) { error = ex.Message.Replace("#EXPOSE_ERROR#", ""); } } return error; } #endregion #region ProcessError public string ProcessError(Exception ex, string className, string apiMethod) { string error = ""; #region Send error to raygun var customData = new Dictionary(); customData.Add("Class", className); customData.Add("ApiMethod", apiMethod); ErrorHelper.LogError(ex, "", customData); #endregion #region Clean Error and Return The Error error = GetError(ex); #endregion return error; } #endregion #region GetJObjectString private string GetJObjectString(JObject dataObject, string property) { var tempValue = dataObject.GetValue(property); if (tempValue != null) { return tempValue.ToString(); } else { return null; } } #endregion } }