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

Last change on this file since 439 was 434, checked in by alloc, 19 months ago

Added permission management APIs

File size: 3.5 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
13 private static readonly byte[] jsonKeyCommand = JsonWriter.GetEncodedPropertyNameWithBeginObject (propertyCommand);
14 private static readonly byte[] jsonKeyPermissionLevel = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPermissionLevel);
15
16 private static AdminCommands CommandsInstance => GameManager.Instance.adminTools.Commands;
17
18 protected override void HandleRestGet (RequestContext _context) {
19 string id = _context.RequestPath;
20
21 PrepareEnvelopedResult (out JsonWriter writer);
22
23 if (string.IsNullOrEmpty (id)) {
24
25 writer.WriteBeginArray ();
26
27 bool first = true;
28 foreach ((_, AdminCommands.CommandPermission commandPermission) in CommandsInstance.GetCommands ()) {
29 if (!first) {
30 writer.WriteValueSeparator ();
31 }
32
33 first = false;
34
35 writeCommandJson (ref writer, commandPermission);
36 }
37
38 writer.WriteEndArray ();
39
40 SendEnvelopedResult (_context, ref writer);
41 return;
42 }
43
44 writer.WriteRaw (WebUtils.JsonEmptyData);
45 SendEnvelopedResult (_context, ref writer, HttpStatusCode.BadRequest);
46 }
47
48 private void writeCommandJson (ref JsonWriter _writer, AdminCommands.CommandPermission _commandPermission) {
49 _writer.WriteRaw (jsonKeyCommand);
50 _writer.WriteString (_commandPermission.Command);
51 _writer.WriteRaw (jsonKeyPermissionLevel);
52 _writer.WriteInt32 (_commandPermission.PermissionLevel);
53 _writer.WriteEndObject ();
54 }
55
56 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
57 string id = _context.RequestPath;
58
59 if (string.IsNullOrEmpty (id)) {
60 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_COMMAND");
61 return;
62 }
63
64 IConsoleCommand cmd = SdtdConsole.Instance.GetCommand (id);
65 if (cmd == null) {
66 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "INVALID_COMMAND");
67 return;
68 }
69
70 if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPermissionLevel, out int permissionLevel)) {
71 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_PERMISSION_LEVEL");
72 return;
73 }
74
75 CommandsInstance.AddCommand (cmd.GetCommands ()[0], permissionLevel);
76
77 SendEmptyResponse (_context, HttpStatusCode.Created);
78 }
79
80 protected override void HandleRestDelete (RequestContext _context) {
81 string id = _context.RequestPath;
82
83 IConsoleCommand cmd = SdtdConsole.Instance.GetCommand (id);
84 if (cmd == null || !CommandsInstance.IsPermissionDefined (cmd.GetCommands ())) {
85 SendEmptyResponse (_context, HttpStatusCode.NotFound);
86 return;
87 }
88
89 bool removed = CommandsInstance.RemoveCommand (cmd.GetCommands ());
90
91 SendEmptyResponse (_context, removed ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
92 }
93
94 protected override bool AllowPostWithId => true;
95
96 public override int[] DefaultMethodPermissionLevels () => new[] {
97 AdminWebModules.MethodLevelNotSupported,
98 AdminWebModules.MethodLevelInheritGlobal,
99 AdminWebModules.MethodLevelInheritGlobal,
100 AdminWebModules.MethodLevelNotSupported,
101 AdminWebModules.MethodLevelInheritGlobal
102 };
103 }
104}
Note: See TracBrowser for help on using the repository browser.