1 | using System.Collections.Generic;
|
---|
2 | using Utf8Json;
|
---|
3 | using Webserver.Permissions;
|
---|
4 |
|
---|
5 | namespace Webserver.UrlHandlers {
|
---|
6 | public class UserStatusHandler : AbsHandler {
|
---|
7 | public UserStatusHandler (string _moduleName = null) : base (_moduleName) {
|
---|
8 | }
|
---|
9 |
|
---|
10 | private static readonly byte[] jsonLoggedInKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("loggedIn");
|
---|
11 | private static readonly byte[] jsonUsernameKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("username");
|
---|
12 | private static readonly byte[] jsonPermissionLevelKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("permissionLevel");
|
---|
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 |
|
---|
18 | public override void HandleRequest (RequestContext _context) {
|
---|
19 | WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
|
---|
20 |
|
---|
21 | writer.WriteRaw (jsonLoggedInKey);
|
---|
22 | writer.WriteBoolean (_context.Connection != null);
|
---|
23 |
|
---|
24 | writer.WriteRaw (jsonUsernameKey);
|
---|
25 | writer.WriteString (_context.Connection != null ? _context.Connection.Username : string.Empty);
|
---|
26 |
|
---|
27 | writer.WriteRaw (jsonPermissionLevelKey);
|
---|
28 | writer.WriteInt32 (_context.PermissionLevel);
|
---|
29 |
|
---|
30 | writer.WriteRaw (jsonPermissionsKey);
|
---|
31 | writer.WriteBeginArray ();
|
---|
32 |
|
---|
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) {
|
---|
38 | writer.WriteValueSeparator ();
|
---|
39 | }
|
---|
40 |
|
---|
41 | writer.WriteRaw (jsonModuleKey);
|
---|
42 | writer.WriteString (perm.Name);
|
---|
43 |
|
---|
44 | writer.WriteRaw (jsonAllowedKey);
|
---|
45 | writer.WriteBoolean (perm.PermissionLevel >= _context.PermissionLevel);
|
---|
46 |
|
---|
47 | writer.WriteEndObject ();
|
---|
48 | }
|
---|
49 |
|
---|
50 | writer.WriteEndArray ();
|
---|
51 |
|
---|
52 | writer.WriteEndObject ();
|
---|
53 |
|
---|
54 | WebUtils.SendEnvelopedResult (_context, ref writer);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|