private void AuditExecution(IAuthenticationOutput userTokenFromCache, CommandInput input, ICommand commandObject) { // Audit an AUTH Command if (CurrentHostContext.Default.Audit.Enabled) { var authCommand = commandObject as IAuditCommand; if (authCommand != null) { var auditRecord = authCommand.GetAuditCommand(input); if (auditRecord != null) { Task.Factory.StartNew(() => { var auditInput = new CommandInput(new CommandArgs("AddAudit")); auditInput.Add("record", auditRecord.ToJson()); if (userTokenFromCache != null) // enrich with UserId { auditInput.AssociateUserId(userTokenFromCache.UserId.GetValueOrDefault(-1)); } ExecuteCommandInternal(auditInput, _auditCommand); }) .ContinueWith(task => CurrentHostContext.Default.Log.ErrorFormat("Error adding audit record {0}", task.Exception.GetDetails()), TaskContinuationOptions.OnlyOnFaulted); } } } }
internal CommandOutput ExecuteCommand(ServiceRequestContext context) { var userTokenFromCache = CurrentHostContext.Default.Cache.Get <AuthenticationOutput>(context.Token); CommandOutput commandOutput = null; try { var input = new CommandInput(new CommandArgs(context.Command)); input.Add("Request.SessionId", context.SessionId); if (input.Keyword == null) { throw new InvalidDataException("Could not parse the command"); } var firstKeyword = input.Keyword.ToLower(); if (_commandFactory.Commands.Any(x => IsCommand(firstKeyword, x, userTokenFromCache))) { var commandObject = _commandFactory.Commands.FirstOrDefault(x => IsCommand(firstKeyword, x, userTokenFromCache)); if (userTokenFromCache != null) // enrich with UserId { input.AssociateUserId(userTokenFromCache.UserId.GetValueOrDefault(-1)); } if (RequiresCaching(commandObject)) { // cache it commandOutput = CurrentHostContext.Default.Cache.Get(context.Command, () => ExecuteCommandInternal(input, commandObject)); } else { commandOutput = ExecuteCommandInternal(input, commandObject); } AuditExecution(userTokenFromCache, input, commandObject); // Execute all subcommands commandOutput.SubCommands.ForEach(x => ExecuteCommand(new ServiceRequestContext { Command = x, SessionId = context.SessionId, Token = context.Token })); } else { commandOutput = _helpCommand.OnExecute(input); } } catch (DbEntityValidationException e) { var builder = new StringBuilder(); foreach (var eve in e.EntityValidationErrors) { builder.AppendFormat("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State).AppendLine(); foreach (var ve in eve.ValidationErrors) { builder.AppendFormat("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage).AppendLine(); } } CurrentHostContext.Default.Log.Error(builder.ToString()); commandOutput = new CommandOutput { MessageType = CommandOutput.DisplayMessageType.Error, DisplayMessage = "Errors: " + builder }; } catch (Exception ex) { CurrentHostContext.Default.Log.Error(ex.GetDetails()); commandOutput = new CommandOutput { MessageType = CommandOutput.DisplayMessageType.Error, DisplayMessage = "Errors: " + ex.GetDetails() }; } return(commandOutput); }