source: binary-improvements2/WebServer/src/WebAPI/APIs/Command.cs@ 404

Last change on this file since 404 was 404, checked in by alloc, 21 months ago

Latest state including reworking to the permissions system

File size: 4.6 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using JetBrains.Annotations;
4using Utf8Json;
5
6namespace Webserver.WebAPI.APIs {
7 [UsedImplicitly]
8 public class Command : AbsRestApi {
9 private static readonly byte[] jsonCommandsKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("commands");
10
11 private static readonly byte[] jsonOverloadsKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("overloads");
12 private static readonly byte[] jsonCommandKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("command");
13 private static readonly byte[] jsonDescriptionKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("description");
14 private static readonly byte[] jsonHelpKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("help");
15 private static readonly byte[] jsonAllowedKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("allowed");
16
17 protected override void HandleRestGet (RequestContext _context) {
18 string id = _context.RequestPath;
19 int permissionLevel = _context.PermissionLevel;
20
21 PrepareEnvelopedResult (out JsonWriter writer);
22
23 writer.WriteRaw (jsonCommandsKey);
24 writer.WriteBeginArray ();
25
26 if (string.IsNullOrEmpty (id)) {
27 IList<IConsoleCommand> ccs = SdtdConsole.Instance.GetCommands ();
28 for (int i = 0; i < ccs.Count; i++) {
29 IConsoleCommand cc = ccs [i];
30
31 if (i > 0) {
32 writer.WriteValueSeparator ();
33 }
34
35 writeCommandJson (ref writer, cc, permissionLevel);
36 }
37 } else if (SdtdConsole.Instance.GetCommand (id) is { } command) {
38 writeCommandJson (ref writer, command, permissionLevel);
39 } else {
40 writer.WriteEndArray ();
41 writer.WriteEndObject ();
42 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
43 return;
44 }
45
46 writer.WriteEndArray ();
47 writer.WriteEndObject ();
48
49 SendEnvelopedResult (_context, ref writer);
50 }
51
52 private void writeCommandJson (ref JsonWriter _writer, IConsoleCommand _command, int _userPermissionLevel) {
53 _writer.WriteRaw (jsonOverloadsKey);
54 _writer.WriteBeginArray ();
55
56 string cmd = string.Empty;
57
58 bool firstOverload = true;
59 foreach (string s in _command.GetCommands ()) {
60 if (!firstOverload) {
61 _writer.WriteValueSeparator ();
62 }
63 firstOverload = false;
64
65 _writer.WriteString (s);
66
67 if (s.Length > cmd.Length) {
68 cmd = s;
69 }
70 }
71
72 _writer.WriteEndArray ();
73
74 _writer.WriteRaw (jsonCommandKey);
75 _writer.WriteString (cmd);
76
77 _writer.WriteRaw (jsonDescriptionKey);
78 _writer.WriteString (_command.GetDescription ());
79
80 _writer.WriteRaw (jsonHelpKey);
81 _writer.WriteString (_command.GetHelp ());
82
83 int commandPermissionLevel = GameManager.Instance.adminTools.Commands.GetCommandPermissionLevel (_command.GetCommands ());
84 _writer.WriteRaw (jsonAllowedKey);
85 _writer.WriteBoolean (_userPermissionLevel <= commandPermissionLevel);
86
87 _writer.WriteEndObject ();
88 }
89
90 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
91 if (!TryGetJsonField (_jsonInput, "command", out string commandString)) {
92 SendErrorResult (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_COMMAND");
93 return;
94 }
95
96 WebCommandResult.ResultType responseType = WebCommandResult.ResultType.Full;
97
98 if (TryGetJsonField (_jsonInput, "format", out string formatString)) {
99 if (formatString.EqualsCaseInsensitive ("raw")) {
100 responseType = WebCommandResult.ResultType.Raw;
101 } else if (formatString.EqualsCaseInsensitive ("simple")) {
102 responseType = WebCommandResult.ResultType.ResultOnly;
103 }
104 }
105
106 int commandSepIndex = commandString.IndexOf (' ');
107 string commandPart = commandSepIndex > 0 ? commandString.Substring (0, commandSepIndex) : commandString;
108 string argumentsPart = commandSepIndex > 0
109 ? commandString.Substring (commandPart.Length + 1)
110 : "";
111
112 IConsoleCommand command = SdtdConsole.Instance.GetCommand (commandPart, true);
113
114 if (command == null) {
115 SendErrorResult (_context, HttpStatusCode.NotFound, _jsonInputData, "UNKNOWN_COMMAND");
116 return;
117 }
118
119 int commandPermissionLevel = GameManager.Instance.adminTools.Commands.GetCommandPermissionLevel (command.GetCommands ());
120
121 if (_context.PermissionLevel > commandPermissionLevel) {
122 SendErrorResult (_context, HttpStatusCode.Forbidden, _jsonInputData, "NO_PERMISSION");
123 return;
124 }
125
126 _context.Response.SendChunked = true;
127 WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _context);
128 SdtdConsole.Instance.ExecuteAsync (commandString, wcr);
129 }
130
131 public override int DefaultPermissionLevel () {
132 return 2000;
133 }
134 }
135}
Note: See TracBrowser for help on using the repository browser.