1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Net;
|
---|
5 | using System.Threading;
|
---|
6 | using System.Text;
|
---|
7 | using AllocsFixes.JSON;
|
---|
8 |
|
---|
9 | namespace AllocsFixes.NetConnections.Servers.Web.Handlers
|
---|
10 | {
|
---|
11 | public class UserStatusHandler : PathHandler {
|
---|
12 | public UserStatusHandler (string moduleName = null) : base(moduleName) {
|
---|
13 | }
|
---|
14 |
|
---|
15 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
|
---|
16 | JSONObject result = new JSONObject ();
|
---|
17 |
|
---|
18 | result.Add ("loggedin", new JSONBoolean (user != null));
|
---|
19 | result.Add ("username", new JSONString (user != null ? user.SteamID.ToString () : string.Empty));
|
---|
20 |
|
---|
21 | JSONArray perms = new JSONArray ();
|
---|
22 | foreach (WebPermissions.WebModulePermission perm in WebPermissions.Instance.GetModules ()) {
|
---|
23 | JSONObject permObj = new JSONObject ();
|
---|
24 | permObj.Add ("module", new JSONString (perm.module));
|
---|
25 | permObj.Add ("allowed", new JSONBoolean (WebPermissions.Instance.ModuleAllowedWithLevel (perm.module, permissionLevel)));
|
---|
26 | perms.Add (permObj);
|
---|
27 | }
|
---|
28 | result.Add ("permissions", perms);
|
---|
29 |
|
---|
30 | WriteJSON (resp, result);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void WriteJSON (HttpListenerResponse resp, JSONNode root) {
|
---|
34 | byte[] buf = Encoding.UTF8.GetBytes (root.ToString ());
|
---|
35 | resp.ContentLength64 = buf.Length;
|
---|
36 | resp.ContentType = "application/json";
|
---|
37 | resp.ContentEncoding = Encoding.UTF8;
|
---|
38 | resp.OutputStream.Write (buf, 0, buf.Length);
|
---|
39 | }
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | }
|
---|
44 |
|
---|