| 1 | using System; | 
|---|
| 2 | using System.Collections.Generic; | 
|---|
| 3 | using System.Net; | 
|---|
| 4 | using JetBrains.Annotations; | 
|---|
| 5 | using Utf8Json; | 
|---|
| 6 | using Webserver.Permissions; | 
|---|
| 7 |  | 
|---|
| 8 | namespace Webserver.WebAPI.APIs.Permissions { | 
|---|
| 9 | [UsedImplicitly] | 
|---|
| 10 | public class Blacklist : AbsRestApi { | 
|---|
| 11 | private const string propertyName = "name"; | 
|---|
| 12 | private const string propertyUserId = "userId"; | 
|---|
| 13 | private const string propertyBannedUntil = "bannedUntil"; | 
|---|
| 14 | private const string propertyBanReason = "banReason"; | 
|---|
| 15 |  | 
|---|
| 16 | private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithBeginObject (propertyName); | 
|---|
| 17 | private static readonly byte[] jsonKeyUserId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyUserId); | 
|---|
| 18 | private static readonly byte[] jsonKeyBannedUntil = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyBannedUntil); | 
|---|
| 19 | private static readonly byte[] jsonKeyBanReason = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyBanReason); | 
|---|
| 20 |  | 
|---|
| 21 | private static AdminBlacklist BlacklistInstance => GameManager.Instance.adminTools.Blacklist; | 
|---|
| 22 |  | 
|---|
| 23 | protected override void HandleRestGet (RequestContext _context) { | 
|---|
| 24 | string id = _context.RequestPath; | 
|---|
| 25 |  | 
|---|
| 26 | PrepareEnvelopedResult (out JsonWriter writer); | 
|---|
| 27 |  | 
|---|
| 28 | if (string.IsNullOrEmpty (id)) { | 
|---|
| 29 | writer.WriteBeginArray (); | 
|---|
| 30 |  | 
|---|
| 31 | bool first = true; | 
|---|
| 32 | foreach (AdminBlacklist.BannedUser ban in BlacklistInstance.GetBanned ()) { | 
|---|
| 33 | if (!first) { | 
|---|
| 34 | writer.WriteValueSeparator (); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | first = false; | 
|---|
| 38 |  | 
|---|
| 39 | writeBan (ref writer, ban); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | writer.WriteEndArray (); | 
|---|
| 43 |  | 
|---|
| 44 | SendEnvelopedResult (_context, ref writer); | 
|---|
| 45 | return; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | writer.WriteRaw (WebUtils.JsonEmptyData); | 
|---|
| 49 | SendEnvelopedResult (_context, ref writer, HttpStatusCode.BadRequest); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | private void writeBan (ref JsonWriter _writer, AdminBlacklist.BannedUser _ban) { | 
|---|
| 53 | _writer.WriteRaw (jsonKeyName); | 
|---|
| 54 | _writer.WriteString (_ban.Name ?? ""); | 
|---|
| 55 | _writer.WriteRaw (jsonKeyUserId); | 
|---|
| 56 | JsonCommons.WritePlatformUserIdentifier (ref _writer, _ban.UserIdentifier); | 
|---|
| 57 | _writer.WriteRaw (jsonKeyBannedUntil); | 
|---|
| 58 | JsonCommons.WriteDateTime (ref _writer, _ban.BannedUntil); | 
|---|
| 59 | _writer.WriteRaw (jsonKeyBanReason); | 
|---|
| 60 | _writer.WriteString (_ban.BanReason); | 
|---|
| 61 | _writer.WriteEndObject (); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) { | 
|---|
| 65 | if (!TryParseId (_context, _jsonInputData, out PlatformUserIdentifierAbs userId)) { | 
|---|
| 66 | return; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | if (!JsonCommons.TryReadDateTime (_jsonInput, propertyBannedUntil, out DateTime bannedUntil)) { | 
|---|
| 70 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_OR_INVALID_BANNED_UNTIL); | 
|---|
| 71 | return; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | JsonCommons.TryGetJsonField (_jsonInput, propertyBanReason, out string banReason); | 
|---|
| 75 |  | 
|---|
| 76 | JsonCommons.TryGetJsonField (_jsonInput, propertyName, out string name); | 
|---|
| 77 |  | 
|---|
| 78 | BlacklistInstance.AddBan (name, userId, bannedUntil, banReason); | 
|---|
| 79 |  | 
|---|
| 80 | SendEmptyResponse (_context, HttpStatusCode.Created); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | protected override void HandleRestDelete (RequestContext _context) { | 
|---|
| 84 | if (!TryParseId (_context, null, out PlatformUserIdentifierAbs userId)) { | 
|---|
| 85 | return; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | bool removed = BlacklistInstance.RemoveBan (userId); | 
|---|
| 89 |  | 
|---|
| 90 | SendEmptyResponse (_context, removed ? HttpStatusCode.NoContent : HttpStatusCode.NotFound); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | private bool TryParseId (RequestContext _context, byte[] _jsonInputData, out PlatformUserIdentifierAbs _userId) { | 
|---|
| 94 | string id = _context.RequestPath; | 
|---|
| 95 | _userId = default; | 
|---|
| 96 |  | 
|---|
| 97 | if (string.IsNullOrEmpty (id)) { | 
|---|
| 98 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_USER); | 
|---|
| 99 | return false; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | if (!PlatformUserIdentifierAbs.TryFromCombinedString (id, out _userId)) { | 
|---|
| 103 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.INVALID_USER); | 
|---|
| 104 | return false; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | return true; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | protected override bool AllowPostWithId => true; | 
|---|
| 111 |  | 
|---|
| 112 | public override int[] DefaultMethodPermissionLevels () => new[] { | 
|---|
| 113 | AdminWebModules.MethodLevelNotSupported, | 
|---|
| 114 | AdminWebModules.MethodLevelInheritGlobal, | 
|---|
| 115 | AdminWebModules.MethodLevelInheritGlobal, | 
|---|
| 116 | AdminWebModules.MethodLevelNotSupported, | 
|---|
| 117 | AdminWebModules.MethodLevelInheritGlobal | 
|---|
| 118 | }; | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|