source: binary-improvements2/MapRendering/Web/API/ExecuteConsoleCommand.cs@ 389

Last change on this file since 389 was 387, checked in by alloc, 2 years ago

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

File size: 1.7 KB
RevLine 
[245]1using System;
2using System.Net;
3
[325]4namespace AllocsFixes.NetConnections.Servers.Web.API {
[387]5 public class ExecuteConsoleCommand : AbsWebAPI {
6 public override void HandleRequest (RequestContext _context) {
7 if (string.IsNullOrEmpty (_context.Request.QueryString ["command"])) {
8 WebUtils.WriteText (_context.Response, "No command given", HttpStatusCode.BadRequest);
[245]9 return;
10 }
11
[306]12 WebCommandResult.ResultType responseType =
[387]13 _context.Request.QueryString ["raw"] != null
[325]14 ? WebCommandResult.ResultType.Raw
[387]15 : (_context.Request.QueryString ["simple"] != null
[325]16 ? WebCommandResult.ResultType.ResultOnly
17 : WebCommandResult.ResultType.Full);
[306]18
[387]19 string commandline = _context.Request.QueryString ["command"];
[279]20 string commandPart = commandline.Split (' ') [0];
21 string argumentsPart = commandline.Substring (Math.Min (commandline.Length, commandPart.Length + 1));
[245]22
[279]23 IConsoleCommand command = SdtdConsole.Instance.GetCommand (commandline);
[245]24
25 if (command == null) {
[387]26 WebUtils.WriteText (_context.Response, "Unknown command", HttpStatusCode.NotFound);
[245]27 return;
28 }
29
[360]30 int commandPermissionLevel = GameManager.Instance.adminTools.GetCommandPermissionLevel (command.GetCommands ());
[245]31
[387]32 if (_context.PermissionLevel > commandPermissionLevel) {
33 WebUtils.WriteText (_context.Response, "You are not allowed to execute this command", HttpStatusCode.Forbidden);
[245]34 return;
35 }
36
[387]37 _context.Response.SendChunked = true;
38 WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _context.Response);
[279]39 SdtdConsole.Instance.ExecuteAsync (commandline, wcr);
[245]40 }
[279]41
42 public override int DefaultPermissionLevel () {
43 return 2000;
44 }
[245]45 }
[325]46}
Note: See TracBrowser for help on using the repository browser.