[404] | 1 | using System.Collections.Generic;
|
---|
[402] | 2 | using Utf8Json;
|
---|
[404] | 3 | using Webserver.Permissions;
|
---|
[391] | 4 |
|
---|
| 5 | namespace 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 |
|
---|
[426] | 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 |
|
---|
[391] | 28 | public override void HandleRequest (RequestContext _context) {
|
---|
[402] | 29 | WebUtils.PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 30 |
|
---|
| 31 | writer.WriteRaw (jsonLoggedInKey);
|
---|
| 32 | writer.WriteBoolean (_context.Connection != null);
|
---|
| 33 |
|
---|
| 34 | writer.WriteRaw (jsonUsernameKey);
|
---|
[404] | 35 | writer.WriteString (_context.Connection != null ? _context.Connection.Username : string.Empty);
|
---|
[402] | 36 |
|
---|
[404] | 37 | writer.WriteRaw (jsonPermissionLevelKey);
|
---|
| 38 | writer.WriteInt32 (_context.PermissionLevel);
|
---|
| 39 |
|
---|
[402] | 40 | writer.WriteRaw (jsonPermissionsKey);
|
---|
| 41 | writer.WriteBeginArray ();
|
---|
[391] | 42 |
|
---|
[404] | 43 | List<AdminWebModules.WebModule> list = AdminWebModules.Instance.GetModules ();
|
---|
[426] | 44 | for (int iModule = 0; iModule < list.Count; iModule++) {
|
---|
| 45 | AdminWebModules.WebModule perm = list [iModule];
|
---|
[404] | 46 |
|
---|
[426] | 47 | if (iModule > 0) {
|
---|
[402] | 48 | writer.WriteValueSeparator ();
|
---|
| 49 | }
|
---|
[391] | 50 |
|
---|
[402] | 51 | writer.WriteRaw (jsonModuleKey);
|
---|
[404] | 52 | writer.WriteString (perm.Name);
|
---|
| 53 |
|
---|
[402] | 54 | writer.WriteRaw (jsonAllowedKey);
|
---|
[426] | 55 |
|
---|
| 56 | writer.WriteBeginObject ();
|
---|
[404] | 57 |
|
---|
[426] | 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 |
|
---|
[402] | 85 | writer.WriteEndObject ();
|
---|
[426] | 86 |
|
---|
| 87 |
|
---|
| 88 | writer.WriteEndObject ();
|
---|
[391] | 89 | }
|
---|
| 90 |
|
---|
[402] | 91 | writer.WriteEndArray ();
|
---|
| 92 |
|
---|
| 93 | writer.WriteEndObject ();
|
---|
| 94 |
|
---|
| 95 | WebUtils.SendEnvelopedResult (_context, ref writer);
|
---|
[391] | 96 | }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|