[434] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Net;
|
---|
| 3 | using JetBrains.Annotations;
|
---|
| 4 | using Utf8Json;
|
---|
| 5 | using Webserver.Permissions;
|
---|
| 6 |
|
---|
| 7 | namespace Webserver.WebAPI.APIs.Permissions {
|
---|
| 8 | [UsedImplicitly]
|
---|
| 9 | public class CommandPermissions : AbsRestApi {
|
---|
| 10 | private const string propertyCommand = "command";
|
---|
| 11 | private const string propertyPermissionLevel = "permissionLevel";
|
---|
[487] | 12 | private const string propertyIsDefault = "default";
|
---|
[434] | 13 |
|
---|
| 14 | private static readonly byte[] jsonKeyCommand = JsonWriter.GetEncodedPropertyNameWithBeginObject (propertyCommand);
|
---|
| 15 | private static readonly byte[] jsonKeyPermissionLevel = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPermissionLevel);
|
---|
[487] | 16 | private static readonly byte[] jsonKeyIsDefault = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyIsDefault);
|
---|
[434] | 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;
|
---|
[487] | 30 |
|
---|
| 31 | foreach (IConsoleCommand command in SdtdConsole.Instance.GetCommands ()) {
|
---|
[434] | 32 | if (!first) {
|
---|
| 33 | writer.WriteValueSeparator ();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | first = false;
|
---|
| 37 |
|
---|
[487] | 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);
|
---|
[434] | 46 | }
|
---|
[487] | 47 |
|
---|
[434] | 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 |
|
---|
[487] | 58 | private void writeCommandJson(ref JsonWriter _writer, AdminCommands.CommandPermission _commandPermission, bool _isDefault) {
|
---|
[434] | 59 | _writer.WriteRaw (jsonKeyCommand);
|
---|
| 60 | _writer.WriteString (_commandPermission.Command);
|
---|
| 61 | _writer.WriteRaw (jsonKeyPermissionLevel);
|
---|
| 62 | _writer.WriteInt32 (_commandPermission.PermissionLevel);
|
---|
[487] | 63 | _writer.WriteRaw (jsonKeyIsDefault);
|
---|
| 64 | _writer.WriteBoolean (_isDefault);
|
---|
[434] | 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)) {
|
---|
[486] | 72 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_COMMAND);
|
---|
[434] | 73 | return;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | IConsoleCommand cmd = SdtdConsole.Instance.GetCommand (id);
|
---|
| 77 | if (cmd == null) {
|
---|
[486] | 78 | SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, EApiErrorCode.UNKNOWN_COMMAND);
|
---|
[434] | 79 | return;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPermissionLevel, out int permissionLevel)) {
|
---|
[486] | 83 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_OR_INVALID_PERMISSION_LEVEL);
|
---|
[434] | 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 | }
|
---|