source: binary-improvements/MapRendering/Web/Handlers/UserStatusHandler.cs@ 304

Last change on this file since 304 was 244, checked in by alloc, 9 years ago

Fixes intermediate state

File size: 1.4 KB
RevLine 
[244]1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Net;
5using System.Threading;
6using System.Text;
7using AllocsFixes.JSON;
8
9namespace 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
Note: See TracBrowser for help on using the repository browser.