source: binary-improvements/MapRendering/API/ExecuteConsoleCommand.cs@ 457

Last change on this file since 457 was 455, checked in by alloc, 16 months ago

25_30_44

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