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

Last change on this file since 410 was 404, checked in by alloc, 22 months ago

Latest state including reworking to the permissions system

File size: 2.1 KB
RevLine 
[404]1using System.Collections.Generic;
[402]2using Utf8Json;
[404]3using Webserver.Permissions;
[391]4
5namespace Webserver.UrlHandlers {
6 public class UserStatusHandler : AbsHandler {
7 public UserStatusHandler (string _moduleName = null) : base (_moduleName) {
8 }
9
[402]10 private static readonly byte[] jsonLoggedInKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("loggedIn");
11 private static readonly byte[] jsonUsernameKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("username");
[404]12 private static readonly byte[] jsonPermissionLevelKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("permissionLevel");
[402]13 private static readonly byte[] jsonPermissionsKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("permissions");
14
15 private static readonly byte[] jsonModuleKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("module");
16 private static readonly byte[] jsonAllowedKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("allowed");
17
[391]18 public override void HandleRequest (RequestContext _context) {
[402]19 WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
20
21 writer.WriteRaw (jsonLoggedInKey);
22 writer.WriteBoolean (_context.Connection != null);
23
24 writer.WriteRaw (jsonUsernameKey);
[404]25 writer.WriteString (_context.Connection != null ? _context.Connection.Username : string.Empty);
[402]26
[404]27 writer.WriteRaw (jsonPermissionLevelKey);
28 writer.WriteInt32 (_context.PermissionLevel);
29
[402]30 writer.WriteRaw (jsonPermissionsKey);
31 writer.WriteBeginArray ();
[391]32
[404]33 List<AdminWebModules.WebModule> list = AdminWebModules.Instance.GetModules ();
34 for (int i = 0; i < list.Count; i++) {
35 AdminWebModules.WebModule perm = list [i];
36
37 if (i > 0) {
[402]38 writer.WriteValueSeparator ();
39 }
[391]40
[402]41 writer.WriteRaw (jsonModuleKey);
[404]42 writer.WriteString (perm.Name);
43
[402]44 writer.WriteRaw (jsonAllowedKey);
[404]45 writer.WriteBoolean (perm.PermissionLevel >= _context.PermissionLevel);
46
[402]47 writer.WriteEndObject ();
[391]48 }
49
[402]50 writer.WriteEndArray ();
51
52 writer.WriteEndObject ();
53
54 WebUtils.SendEnvelopedResult (_context, ref writer);
[391]55 }
56 }
57}
Note: See TracBrowser for help on using the repository browser.