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

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