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 | private static readonly byte[][] jsonMethodNameKeys;
|
---|
19 |
|
---|
20 | static UserStatusHandler () {
|
---|
21 | jsonMethodNameKeys = new byte[(int)ERequestMethod.Count][];
|
---|
22 | for (int i = 0; i < jsonMethodNameKeys.Length; i++) {
|
---|
23 | ERequestMethod method = (ERequestMethod)i;
|
---|
24 | jsonMethodNameKeys [i] = JsonWriter.GetEncodedPropertyName (method.ToStringCached ());
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | public override void HandleRequest (RequestContext _context) {
|
---|
29 | WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
|
---|
30 |
|
---|
31 | writer.WriteRaw (jsonLoggedInKey);
|
---|
32 | writer.WriteBoolean (_context.Connection != null);
|
---|
33 |
|
---|
34 | writer.WriteRaw (jsonUsernameKey);
|
---|
35 | writer.WriteString (_context.Connection != null ? _context.Connection.Username : string.Empty);
|
---|
36 |
|
---|
37 | writer.WriteRaw (jsonPermissionLevelKey);
|
---|
38 | writer.WriteInt32 (_context.PermissionLevel);
|
---|
39 |
|
---|
40 | writer.WriteRaw (jsonPermissionsKey);
|
---|
41 | writer.WriteBeginArray ();
|
---|
42 |
|
---|
43 | List<AdminWebModules.WebModule> list = AdminWebModules.Instance.GetModules ();
|
---|
44 | for (int iModule = 0; iModule < list.Count; iModule++) {
|
---|
45 | AdminWebModules.WebModule perm = list [iModule];
|
---|
46 |
|
---|
47 | if (iModule > 0) {
|
---|
48 | writer.WriteValueSeparator ();
|
---|
49 | }
|
---|
50 |
|
---|
51 | writer.WriteRaw (jsonModuleKey);
|
---|
52 | writer.WriteString (perm.Name);
|
---|
53 |
|
---|
54 | writer.WriteRaw (jsonAllowedKey);
|
---|
55 |
|
---|
56 | writer.WriteBeginObject ();
|
---|
57 |
|
---|
58 | if (perm.LevelPerMethod == null) {
|
---|
59 | writer.WriteRaw (jsonMethodNameKeys [(int)ERequestMethod.GET]);
|
---|
60 | writer.WriteBoolean (perm.LevelGlobal >= _context.PermissionLevel);
|
---|
61 | } else {
|
---|
62 | bool first = true;
|
---|
63 | for (int iMethod = 0; iMethod < perm.LevelPerMethod.Length; iMethod++) {
|
---|
64 | int methodLevel = perm.LevelPerMethod [iMethod];
|
---|
65 |
|
---|
66 | if (methodLevel == AdminWebModules.MethodLevelNotSupported) {
|
---|
67 | continue;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (methodLevel == AdminWebModules.MethodLevelInheritGlobal) {
|
---|
71 | methodLevel = perm.LevelGlobal;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (!first) {
|
---|
75 | writer.WriteValueSeparator ();
|
---|
76 | }
|
---|
77 |
|
---|
78 | first = false;
|
---|
79 |
|
---|
80 | writer.WriteRaw (jsonMethodNameKeys [iMethod]);
|
---|
81 | writer.WriteBoolean (methodLevel >= _context.PermissionLevel);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | writer.WriteEndObject ();
|
---|
86 |
|
---|
87 |
|
---|
88 | writer.WriteEndObject ();
|
---|
89 | }
|
---|
90 |
|
---|
91 | writer.WriteEndArray ();
|
---|
92 |
|
---|
93 | writer.WriteEndObject ();
|
---|
94 |
|
---|
95 | WebUtils.SendEnvelopedResult (_context, ref writer);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|