source: TFP-WebServer/WebServer/src/WebAPI/APIs/Permissions/UserPermissions.cs@ 455

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

Added permission management APIs

File size: 6.1 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 UserPermissions : AbsRestApi {
10 private const string propertyName = "name";
11 private const string propertyUserId = "userId";
12 private const string propertyPermissionLevel = "permissionLevel";
13 private const string propertyGroupId = "groupId";
14 private const string propertyPermissionLevelMods = "permissionLevelMods";
15 private const string propertyPermissionLevelNormal = "permissionLevelNormal";
16
17 private static readonly byte[] jsonKeyUsers = JsonWriter.GetEncodedPropertyNameWithBeginObject ("users");
18 private static readonly byte[] jsonKeyGroups = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("groups");
19
20 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithBeginObject (propertyName);
21 private static readonly byte[] jsonKeyUserId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyUserId);
22
23 private static readonly byte[] jsonKeyPermissionLevel =
24 JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPermissionLevel);
25
26 private static readonly byte[] jsonKeyGroupId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyGroupId);
27
28 private static readonly byte[] jsonKeyPermissionLevelMods =
29 JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPermissionLevelMods);
30
31 private static readonly byte[] jsonKeyPermissionLevelNormal =
32 JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPermissionLevelNormal);
33
34 private static AdminUsers UsersInstance => GameManager.Instance.adminTools.Users;
35
36 protected override void HandleRestGet (RequestContext _context) {
37 string id = _context.RequestPath;
38
39 PrepareEnvelopedResult (out JsonWriter writer);
40
41 if (string.IsNullOrEmpty (id)) {
42 writer.WriteRaw (jsonKeyUsers);
43 writer.WriteBeginArray ();
44
45 bool first = true;
46 foreach ((_, AdminUsers.UserPermission userPermission) in UsersInstance.GetUsers ()) {
47 if (!first) {
48 writer.WriteValueSeparator ();
49 }
50
51 first = false;
52
53 writeUserJson (ref writer, userPermission);
54 }
55
56 writer.WriteEndArray ();
57
58 writer.WriteRaw (jsonKeyGroups);
59 writer.WriteBeginArray ();
60
61 first = true;
62 foreach ((_, AdminUsers.GroupPermission groupPermission) in UsersInstance.GetGroups ()) {
63 if (!first) {
64 writer.WriteValueSeparator ();
65 }
66
67 first = false;
68
69 writeGroupJson (ref writer, groupPermission);
70 }
71
72 writer.WriteEndArray ();
73
74 writer.WriteEndObject ();
75
76 SendEnvelopedResult (_context, ref writer);
77 return;
78 }
79
80 writer.WriteRaw (WebUtils.JsonEmptyData);
81 SendEnvelopedResult (_context, ref writer, HttpStatusCode.BadRequest);
82 }
83
84 private void writeUserJson (ref JsonWriter _writer, AdminUsers.UserPermission _userPermission) {
85 _writer.WriteRaw (jsonKeyName);
86 _writer.WriteString (_userPermission.Name ?? "");
87 _writer.WriteRaw (jsonKeyUserId);
88 JsonCommons.WritePlatformUserIdentifier (ref _writer, _userPermission.UserIdentifier);
89 _writer.WriteRaw (jsonKeyPermissionLevel);
90 _writer.WriteInt32 (_userPermission.PermissionLevel);
91 _writer.WriteEndObject ();
92 }
93
94 private void writeGroupJson (ref JsonWriter _writer, AdminUsers.GroupPermission _groupPermission) {
95 _writer.WriteRaw (jsonKeyName);
96 _writer.WriteString (_groupPermission.Name ?? "");
97 _writer.WriteRaw (jsonKeyGroupId);
98 _writer.WriteString (_groupPermission.SteamIdGroup);
99 _writer.WriteRaw (jsonKeyPermissionLevelMods);
100 _writer.WriteInt32 (_groupPermission.PermissionLevelMods);
101 _writer.WriteRaw (jsonKeyPermissionLevelNormal);
102 _writer.WriteInt32 (_groupPermission.PermissionLevelNormal);
103 _writer.WriteEndObject ();
104 }
105
106 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
107 if (!PermissionsApiHelpers.TryParseId (_context, _jsonInputData, out PlatformUserIdentifierAbs userId, out string groupId)) {
108 return;
109 }
110
111 if (userId != null) {
112 if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPermissionLevel, out int permissionLevel)) {
113 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_PERMISSION_LEVEL");
114 return;
115 }
116
117 JsonCommons.TryGetJsonField (_jsonInput, propertyName, out string name);
118
119 UsersInstance.AddUser (name, userId, permissionLevel);
120 } else {
121 if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPermissionLevelMods, out int permissionLevelMods)) {
122 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_PERMISSION_LEVEL_MODS");
123 return;
124 }
125
126 if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPermissionLevelNormal, out int permissionLevelNormal)) {
127 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_PERMISSION_LEVEL_NORMAL");
128 return;
129 }
130
131 JsonCommons.TryGetJsonField (_jsonInput, propertyName, out string name);
132
133 UsersInstance.AddGroup (name, groupId, permissionLevelNormal, permissionLevelMods);
134 }
135
136 SendEmptyResponse (_context, HttpStatusCode.Created);
137 }
138
139 protected override void HandleRestDelete (RequestContext _context) {
140 if (!PermissionsApiHelpers.TryParseId (_context, null, out PlatformUserIdentifierAbs userId, out string groupId)) {
141 return;
142 }
143
144 bool removed = userId != null ? UsersInstance.RemoveUser (userId) : UsersInstance.RemoveGroup (groupId);
145
146 SendEmptyResponse (_context, removed ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
147 }
148
149 protected override bool AllowPostWithId => true;
150
151 public override int[] DefaultMethodPermissionLevels () => new[] {
152 AdminWebModules.MethodLevelNotSupported,
153 AdminWebModules.MethodLevelInheritGlobal,
154 AdminWebModules.MethodLevelInheritGlobal,
155 AdminWebModules.MethodLevelNotSupported,
156 AdminWebModules.MethodLevelInheritGlobal
157 };
158 }
159}
Note: See TracBrowser for help on using the repository browser.