source: TFP-WebServer/WebServer/src/UrlHandlers/UserStatusHandler.cs@ 461

Last change on this file since 461 was 426, checked in by alloc, 19 months ago

*Updated web permissions system
*Fixed webpermissions command
*Moved API "webmods" to "mods", also lists non-webmod mods

File size: 3.2 KB
Line 
1using System.Collections.Generic;
2using Utf8Json;
3using Webserver.Permissions;
4
5namespace 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}
Note: See TracBrowser for help on using the repository browser.