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 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)) {
|
---|
66 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_PASSWORD");
|
---|
67 | return;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPlatformUserId, out IDictionary<string, object> userIdField)) {
|
---|
71 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_PLATFORM_USER_ID");
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (!JsonCommons.TryReadPlatformUserIdentifier (userIdField, out PlatformUserIdentifierAbs platformUserId)) {
|
---|
76 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "INVALID_PLATFORM_USER_ID");
|
---|
77 | return;
|
---|
78 | }
|
---|
79 |
|
---|
80 | PlatformUserIdentifierAbs crossplatformUserId = null;
|
---|
81 |
|
---|
82 | if (JsonCommons.TryGetJsonField (_jsonInput, propertyCrossplatformUserId, out userIdField)) {
|
---|
83 | if (!JsonCommons.TryReadPlatformUserIdentifier (userIdField, out crossplatformUserId)) {
|
---|
84 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "INVALID_CROSSPLATFORM_USER_ID");
|
---|
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)) {
|
---|
109 | SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_USERNAME");
|
---|
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 | }
|
---|