public ViewResult Index(string Cli, string Display) { AppSettings.ConnectionString = ConfigurationManager.ConnectionStrings["EntityContainer"].ConnectionString; if (Session["apiSessionId"] == null) Session["apiSessionId"] = new WebClient().DownloadString(ConfigurationManager.AppSettings["ApiUrl"] + "GetSessionId"); ModelState.Clear(); if (Session["commandContext"] != null) _commandContext = (CommandContext)Session["commandContext"]; _terminalApi.Username = User.Identity.IsAuthenticated ? User.Identity.Name : null; _terminalApi.CommandContext = _commandContext; _terminalApi.ParseAsHtml = true; var commandResult = _terminalApi.ExecuteCommand(Cli); if (commandResult.ClearScreen) Display = null; if (User.Identity.IsAuthenticated) { if (commandResult.CurrentUser == null) FormsAuthentication.SignOut(); } else { if (commandResult.CurrentUser != null) FormsAuthentication.SetAuthCookie(commandResult.CurrentUser.Username, false); } Session["commandContext"] = commandResult.CommandContext; var display = new StringBuilder(); foreach (var displayItem in commandResult.Display) { display.Append(displayItem.Text); display.Append("<br />"); } if (Display != null) Display += "<br />"; var viewModel = new TerminalViewModel { Cli = commandResult.EditText, ContextText = commandResult.CommandContext.Command + (commandResult.CommandContext.Text.IsNullOrEmpty() ? null : string.Format(" {0}", _terminalApi.CommandContext.Text)), Display = Display + display.ToString(), PasswordField = commandResult.PasswordField, Notifications = string.Empty, Title = commandResult.TerminalTitle, SessionId = Session["apiSessionId"].ToString() }; return View(viewModel); }
public string Index(string cli, string callback, bool parseAsHtml = false) { AppSettings.ConnectionString = ConfigurationManager.ConnectionStrings["EntityContainer"].ConnectionString; if (Session["commandContext"] != null) _commandContext = (CommandContext)Session["commandContext"]; _terminalApi.Username = Session["currentUser"] != null ? Session["currentUser"].ToString() : null; _terminalApi.CommandContext = _commandContext; _terminalApi.ParseAsHtml = parseAsHtml; var commandResult = _terminalApi.ExecuteCommand(cli); Session["currentUser"] = commandResult.CurrentUser != null ? commandResult.CurrentUser.Username : null; Session["commandContext"] = commandResult.CommandContext; var displayItems = new List<ApiDisplayItem>(); commandResult.Display.ForEach(x => displayItems.Add(new ApiDisplayItem { Text = x.Text, Bold = (x.DisplayMode & DisplayMode.Bold) != 0, Dim = (x.DisplayMode & DisplayMode.Dim) != 0, DontType = (x.DisplayMode & DisplayMode.DontType) != 0, Inverted = (x.DisplayMode & DisplayMode.Inverted) != 0, Italics = (x.DisplayMode & DisplayMode.Italics) != 0, Mute = (x.DisplayMode & DisplayMode.Mute) != 0, Parse = (x.DisplayMode & DisplayMode.Parse) != 0 })); var apiResult = new ApiResult { ClearScreen = commandResult.ClearScreen, Command = commandResult.Command, ContextStatus = commandResult.CommandContext.Status.ToString(), ContextText = commandResult.CommandContext.Command + (commandResult.CommandContext.Text.IsNullOrEmpty() ? null : string.Format(" {0}", _terminalApi.CommandContext.Text)), CurrentUser = commandResult.CurrentUser != null ? commandResult.CurrentUser.Username : null, DisplayItems = displayItems, EditText = commandResult.EditText, Exit = commandResult.Exit, PasswordField = commandResult.PasswordField, ScrollToBottom = commandResult.ScrollToBottom, SessionId = Session.SessionID, TerminalTitle = commandResult.TerminalTitle }; string json = new JavaScriptSerializer().Serialize(apiResult); return callback != null ? string.Format("{0}({1});", callback, json) : json; }
/// <summary> /// Creates a new instance of the API. Ideally this should be created by Ninject to ensure all dependencies are handled appropriately. /// Note: A U413Bindings class lives in the U413.Domain.Ninject.BindingModules namespace. Use this when building your Ninject kernel to ensure proper dependency injection. /// /// Sampel: IKernel kernel = new StandardKernel(new U413Bindings()); /// </summary> /// <param name="commands">A list of all commands available to the application.</param> /// <param name="userRepository">The user repository used to retrieve the current user from the database.</param> public TerminalApi( List<ICommand> commands, IUserRepository userRepository, IAliasRepository aliasRepository, IMessageRepository messageRepository ) { _commands = commands; _userRepository = userRepository; _aliasRepository = aliasRepository; _messageRepository = messageRepository; this._commandContext = new CommandContext(); }
/// <summary> /// Examine command result and perform relevant actions. /// </summary> /// <param name="commandResult">The command result returned by the terminal API.</param> private static void InterpretResult(CommandResult commandResult) { // Set the terminal API command context to the one returned in the result. _commandContext = commandResult.CommandContext; // If the result calls for the screen to be cleared, clear it. if (commandResult.ClearScreen) Console.Clear(); // Set the terminal API current user to the one returned in the result and display the username in the console title bar. _username = commandResult.CurrentUser != null ? commandResult.CurrentUser.Username : null; Console.Title = commandResult.TerminalTitle; // Add a blank line to the console before displaying results. if (commandResult.Display.Count > 0) Console.WriteLine(); // Iterate over the display collection and perform relevant display actions based on the type of the object. foreach (var displayInstruction in commandResult.Display) Display(displayInstruction); if (!commandResult.EditText.IsNullOrEmpty()) SendKeys.SendWait(commandResult.EditText.Replace("\n", "--")); // If the terminal is prompting for a password then set the global PasswordField bool to true. _passwordField = commandResult.PasswordField; // If the terminal is asking to be closed then kill the runtime loop for the console. _appRunning = !commandResult.Exit; }