source: TFP-WebServer/WebServer/src/WebAPI/APIs/Permissions/Whitelist.cs@ 500

Last change on this file since 500 was 434, checked in by alloc, 18 months ago

Added permission management APIs

File size: 4.2 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 Whitelist : AbsRestApi {
10 private const string propertyName = "name";
11 private const string propertyUserId = "userId";
12 private const string propertyGroupId = "groupId";
13
14 private static readonly byte[] jsonKeyUsers = JsonWriter.GetEncodedPropertyNameWithBeginObject ("users");
15 private static readonly byte[] jsonKeyGroups = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("groups");
16
17 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithBeginObject (propertyName);
18 private static readonly byte[] jsonKeyUserId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyUserId);
19
20 private static readonly byte[] jsonKeyGroupId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyGroupId);
21
22 private static AdminWhitelist WhitelistInstance => GameManager.Instance.adminTools.Whitelist;
23
24 protected override void HandleRestGet (RequestContext _context) {
25 string id = _context.RequestPath;
26
27 PrepareEnvelopedResult (out JsonWriter writer);
28
29 if (string.IsNullOrEmpty (id)) {
30 writer.WriteRaw (jsonKeyUsers);
31 writer.WriteBeginArray ();
32
33 bool first = true;
34 foreach ((_, AdminWhitelist.WhitelistUser user) in WhitelistInstance.GetUsers ()) {
35 if (!first) {
36 writer.WriteValueSeparator ();
37 }
38
39 first = false;
40
41 writeUserJson (ref writer, user);
42 }
43
44 writer.WriteEndArray ();
45
46 writer.WriteRaw (jsonKeyGroups);
47 writer.WriteBeginArray ();
48
49 first = true;
50 foreach ((_, AdminWhitelist.WhitelistGroup group) in WhitelistInstance.GetGroups ()) {
51 if (!first) {
52 writer.WriteValueSeparator ();
53 }
54
55 first = false;
56
57 writeGroupJson (ref writer, group);
58 }
59
60 writer.WriteEndArray ();
61
62 writer.WriteEndObject ();
63
64 SendEnvelopedResult (_context, ref writer);
65 return;
66 }
67
68 writer.WriteRaw (WebUtils.JsonEmptyData);
69 SendEnvelopedResult (_context, ref writer, HttpStatusCode.BadRequest);
70 }
71
72 private void writeUserJson (ref JsonWriter _writer, AdminWhitelist.WhitelistUser _userPermission) {
73 _writer.WriteRaw (jsonKeyName);
74 _writer.WriteString (_userPermission.Name ?? "");
75 _writer.WriteRaw (jsonKeyUserId);
76 JsonCommons.WritePlatformUserIdentifier (ref _writer, _userPermission.UserIdentifier);
77 _writer.WriteEndObject ();
78 }
79
80 private void writeGroupJson (ref JsonWriter _writer, AdminWhitelist.WhitelistGroup _groupPermission) {
81 _writer.WriteRaw (jsonKeyName);
82 _writer.WriteString (_groupPermission.Name ?? "");
83 _writer.WriteRaw (jsonKeyGroupId);
84 _writer.WriteString (_groupPermission.SteamIdGroup);
85 _writer.WriteEndObject ();
86 }
87
88 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
89 if (!PermissionsApiHelpers.TryParseId (_context, _jsonInputData, out PlatformUserIdentifierAbs userId, out string groupId)) {
90 return;
91 }
92
93 JsonCommons.TryGetJsonField (_jsonInput, propertyName, out string name);
94
95 if (userId != null) {
96 WhitelistInstance.AddUser (name, userId);
97 } else {
98 WhitelistInstance.AddGroup (name, groupId);
99 }
100
101 SendEmptyResponse (_context, HttpStatusCode.Created);
102 }
103
104 protected override void HandleRestDelete (RequestContext _context) {
105 if (!PermissionsApiHelpers.TryParseId (_context, null, out PlatformUserIdentifierAbs userId, out string groupId)) {
106 return;
107 }
108
109 bool removed = userId != null ? WhitelistInstance.RemoveUser (userId) : WhitelistInstance.RemoveGroup (groupId);
110
111 SendEmptyResponse (_context, removed ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
112 }
113
114 protected override bool AllowPostWithId => true;
115
116 public override int[] DefaultMethodPermissionLevels () => new[] {
117 AdminWebModules.MethodLevelNotSupported,
118 AdminWebModules.MethodLevelInheritGlobal,
119 AdminWebModules.MethodLevelInheritGlobal,
120 AdminWebModules.MethodLevelNotSupported,
121 AdminWebModules.MethodLevelInheritGlobal
122 };
123 }
124}
Note: See TracBrowser for help on using the repository browser.