source: TFP-WebServer/WebServer/src/WebAPI/APIs/Permissions/WebUsers.cs@ 496

Last change on this file since 496 was 486, checked in by alloc, 9 months ago

22.0.1.1 WebServer release

  • Moved API error codes from string literals to enum
File size: 4.6 KB
RevLine 
[434]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 WebUsers : AbsRestApi {
10 private const string propertyName = "name";
11 private const string propertyPassword = "password";
12 private const string propertyPlatformUserId = "platformUserId";
13 private const string propertyCrossplatformUserId = "crossplatformUserId";
14
15 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithBeginObject (propertyName);
16 private static readonly byte[] jsonKeyPlatformUserId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPlatformUserId);
17 private static readonly byte[] jsonKeyCrossplatformUserId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyCrossplatformUserId);
18
19 private static AdminWebUsers WebUsersInstance => AdminWebUsers.Instance;
20
21 protected override void HandleRestGet (RequestContext _context) {
22 string id = _context.RequestPath;
23
24 PrepareEnvelopedResult (out JsonWriter writer);
25
26 if (string.IsNullOrEmpty (id)) {
27 writer.WriteBeginArray ();
28
29 bool first = true;
30 foreach ((_, AdminWebUsers.WebUser user) in WebUsersInstance.GetUsers ()) {
31 if (!first) {
32 writer.WriteValueSeparator ();
33 }
34
35 first = false;
36
37 writeUserJson (ref writer, user);
38 }
39
40 writer.WriteEndArray ();
41
42 SendEnvelopedResult (_context, ref writer);
43 return;
44 }
45
46 writer.WriteRaw (WebUtils.JsonEmptyData);
47 SendEnvelopedResult (_context, ref writer, HttpStatusCode.BadRequest);
48 }
49
50 private void writeUserJson (ref JsonWriter _writer, AdminWebUsers.WebUser _user) {
51 _writer.WriteRaw (jsonKeyName);
52 _writer.WriteString (_user.Name ?? "");
53 _writer.WriteRaw (jsonKeyPlatformUserId);
54 JsonCommons.WritePlatformUserIdentifier (ref _writer, _user.PlatformUser);
55 _writer.WriteRaw (jsonKeyCrossplatformUserId);
56 JsonCommons.WritePlatformUserIdentifier (ref _writer, _user.CrossPlatformUser);
57 _writer.WriteEndObject ();
58 }
59
60 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
61 if (!TryParseName (_context, _jsonInputData, out string userName)) {
62 return;
63 }
64
65 if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPassword, out string password)) {
[486]66 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_OR_INVALID_PASSWORD);
[434]67 return;
68 }
69
70 if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPlatformUserId, out IDictionary<string, object> userIdField)) {
[486]71 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_PLATFORM_USER_ID);
[434]72 return;
73 }
74
75 if (!JsonCommons.TryReadPlatformUserIdentifier (userIdField, out PlatformUserIdentifierAbs platformUserId)) {
[486]76 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.INVALID_PLATFORM_USER_ID);
[434]77 return;
78 }
79
80 PlatformUserIdentifierAbs crossplatformUserId = null;
81
82 if (JsonCommons.TryGetJsonField (_jsonInput, propertyCrossplatformUserId, out userIdField)) {
83 if (!JsonCommons.TryReadPlatformUserIdentifier (userIdField, out crossplatformUserId)) {
[486]84 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.INVALID_CROSSPLATFORM_USER_ID);
[434]85 return;
86 }
87 }
88
89 WebUsersInstance.AddUser (userName, password, platformUserId, crossplatformUserId);
90
91 SendEmptyResponse (_context, HttpStatusCode.Created);
92 }
93
94 protected override void HandleRestDelete (RequestContext _context) {
95 if (!TryParseName (_context, null, out string userName)) {
96 return;
97 }
98
99 bool removed = WebUsersInstance.RemoveUser (userName);
100
101 SendEmptyResponse (_context, removed ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
102 }
103
104 private bool TryParseName (RequestContext _context, byte[] _jsonInputData, out string _userName) {
105 string id = _context.RequestPath;
106 _userName = default;
107
108 if (string.IsNullOrEmpty (id)) {
[486]109 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, EApiErrorCode.NO_USERNAME);
[434]110 return false;
111 }
112
113 _userName = id;
114 return true;
115 }
116
117 protected override bool AllowPostWithId => true;
118
119 public override int[] DefaultMethodPermissionLevels () => new[] {
120 AdminWebModules.MethodLevelNotSupported,
121 AdminWebModules.MethodLevelInheritGlobal,
122 AdminWebModules.MethodLevelInheritGlobal,
123 AdminWebModules.MethodLevelNotSupported,
124 AdminWebModules.MethodLevelInheritGlobal
125 };
126 }
127}
Note: See TracBrowser for help on using the repository browser.