source: binary-improvements2/WebServer/src/UrlHandlers/UserStatusHandler.cs@ 402

Last change on this file since 402 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 1.8 KB
Line 
1using Utf8Json;
2
3namespace Webserver.UrlHandlers {
4 public class UserStatusHandler : AbsHandler {
5 public UserStatusHandler (string _moduleName = null) : base (_moduleName) {
6 }
7
8 private static readonly byte[] jsonLoggedInKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("loggedIn");
9 private static readonly byte[] jsonUsernameKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("username");
10 private static readonly byte[] jsonPermissionsKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("permissions");
11
12 private static readonly byte[] jsonModuleKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("module");
13 private static readonly byte[] jsonAllowedKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("allowed");
14
15 public override void HandleRequest (RequestContext _context) {
16 WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
17
18 writer.WriteRaw (jsonLoggedInKey);
19 writer.WriteBoolean (_context.Connection != null);
20
21 writer.WriteRaw (jsonUsernameKey);
22 writer.WriteString (_context.Connection != null ? _context.Connection.UserId.ToString () : string.Empty);
23
24 writer.WriteRaw (jsonPermissionsKey);
25 writer.WriteBeginArray ();
26
27 bool first = true;
28 foreach (WebPermissions.WebModulePermission perm in WebPermissions.Instance.GetModules ()) {
29 if (!first) {
30 writer.WriteValueSeparator ();
31 }
32
33 first = false;
34
35 writer.WriteRaw (jsonModuleKey);
36 writer.WriteString (perm.module);
37
38 writer.WriteRaw (jsonAllowedKey);
39 writer.WriteBoolean (perm.permissionLevel >= _context.PermissionLevel);
40
41 writer.WriteEndObject ();
42 }
43
44 writer.WriteEndArray ();
45
46 writer.WriteEndObject ();
47
48 WebUtils.SendEnvelopedResult (_context, ref writer);
49 }
50 }
51}
Note: See TracBrowser for help on using the repository browser.