source: TFP-WebServer/WebServer/src/WebAPI/APIs/Permissions/CommandPermissions.cs@ 494

Last change on this file since 494 was 487, checked in by alloc, 5 months ago

1.1.0.1 Release for V 1.0

File size: 4.3 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using JetBrains.Annotations;
4using Utf8Json;
5using Webserver.Permissions;
6
7namespace Webserver.WebAPI.APIs.Permissions {
8 [UsedImplicitly]
9 public class CommandPermissions : AbsRestApi {
10 private const string propertyCommand = "command";
11 private const string propertyPermissionLevel = "permissionLevel";
12 private const string propertyIsDefault = "default";
13
14 private static readonly byte[] jsonKeyCommand = JsonWriter.GetEncodedPropertyNameWithBeginObject (propertyCommand);
15 private static readonly byte[] jsonKeyPermissionLevel = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPermissionLevel);
16 private static readonly byte[] jsonKeyIsDefault = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyIsDefault);
17
18 private static AdminCommands CommandsInstance => GameManager.Instance.adminTools.Commands;
19
20 protected override void HandleRestGet (RequestContext _context) {
21 string id = _context.RequestPath;
22
23 PrepareEnvelopedResult (out JsonWriter writer);
24
25 if (string.IsNullOrEmpty (id)) {
26
27 writer.WriteBeginArray ();
28
29 bool first = true;
30
31 foreach (IConsoleCommand command in SdtdConsole.Instance.GetCommands ()) {
32 if (!first) {
33 writer.WriteValueSeparator ();
34 }
35
36 first = false;
37
38 AdminCommands.CommandPermission commandPermission = CommandsInstance.GetAdminToolsCommandPermission (command.GetCommands());
39 bool isDefault = commandPermission.PermissionLevel == command.DefaultPermissionLevel;
40 if (commandPermission.Command == "") {
41 commandPermission =
42 new AdminCommands.CommandPermission (command.GetCommands ()[0], commandPermission.PermissionLevel);
43 }
44
45 writeCommandJson (ref writer, commandPermission, isDefault);
46 }
47
48 writer.WriteEndArray ();
49
50 SendEnvelopedResult (_context, ref writer);
51 return;
52 }
53
54 writer.WriteRaw (WebUtils.JsonEmptyData);
55 SendEnvelopedResult (_context, ref writer, HttpStatusCode.BadRequest);
56 }
57
58 private void writeCommandJson(ref JsonWriter _writer, AdminCommands.CommandPermission _commandPermission, bool _isDefault) {
59 _writer.WriteRaw (jsonKeyCommand);
60 _writer.WriteString (_commandPermission.Command);
61 _writer.WriteRaw (jsonKeyPermissionLevel);
62 _writer.WriteInt32 (_commandPermission.PermissionLevel);
63 _writer.WriteRaw (jsonKeyIsDefault);
64 _writer.WriteBoolean (_isDefault);
65 _writer.WriteEndObject ();
66 }
67
68 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
69 string id = _context.RequestPath;
70
71 if (string.IsNullOrEmpty (id)) {
72 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_COMMAND);
73 return;
74 }
75
76 IConsoleCommand cmd = SdtdConsole.Instance.GetCommand (id);
77 if (cmd == null) {
78 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, EApiErrorCode.UNKNOWN_COMMAND);
79 return;
80 }
81
82 if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPermissionLevel, out int permissionLevel)) {
83 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_OR_INVALID_PERMISSION_LEVEL);
84 return;
85 }
86
87 CommandsInstance.AddCommand (cmd.GetCommands ()[0], permissionLevel);
88
89 SendEmptyResponse (_context, HttpStatusCode.Created);
90 }
91
92 protected override void HandleRestDelete (RequestContext _context) {
93 string id = _context.RequestPath;
94
95 IConsoleCommand cmd = SdtdConsole.Instance.GetCommand (id);
96 if (cmd == null || !CommandsInstance.IsPermissionDefined (cmd.GetCommands ())) {
97 SendEmptyResponse (_context, HttpStatusCode.NotFound);
98 return;
99 }
100
101 bool removed = CommandsInstance.RemoveCommand (cmd.GetCommands ());
102
103 SendEmptyResponse (_context, removed ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
104 }
105
106 protected override bool AllowPostWithId => true;
107
108 public override int[] DefaultMethodPermissionLevels () => new[] {
109 AdminWebModules.MethodLevelNotSupported,
110 AdminWebModules.MethodLevelInheritGlobal,
111 AdminWebModules.MethodLevelInheritGlobal,
112 AdminWebModules.MethodLevelNotSupported,
113 AdminWebModules.MethodLevelInheritGlobal
114 };
115 }
116}
Note: See TracBrowser for help on using the repository browser.