source: binary-improvements/MapRendering/Web/WebPermissions.cs@ 259

Last change on this file since 259 was 253, checked in by alloc, 9 years ago

Fixes 6_8_10

File size: 10.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Xml;
5
6namespace AllocsFixes.NetConnections.Servers.Web
7{
8 public class WebPermissions {
9 private static WebPermissions instance = null;
10
11 public static WebPermissions Instance {
12 get {
13 lock (typeof(WebPermissions)) {
14 if (instance == null) {
15 instance = new WebPermissions ();
16 }
17 return instance;
18 }
19 }
20 }
21
22 private const string PERMISSIONS_FILE = "webpermissions.xml";
23 Dictionary<string, AdminToken> admintokens;
24 Dictionary<string, WebModulePermission> modules;
25 Dictionary<string, WebModulePermission> knownModules = new Dictionary<string, WebModulePermission> ();
26 WebModulePermission defaultModulePermission = new WebModulePermission ("", 0);
27 FileSystemWatcher fileWatcher;
28
29 public WebPermissions () {
30 Directory.CreateDirectory (GetFilePath ());
31 InitFileWatcher ();
32 Load ();
33 }
34
35 public bool ModuleAllowedWithLevel (string _module, int _level) {
36 WebModulePermission permInfo = GetModulePermission (_module);
37 return permInfo.permissionLevel >= _level;
38 }
39
40 public AdminToken GetWebAdmin (string _name, string _token) {
41 if (IsAdmin (_name) && admintokens [_name].token == _token) {
42 return admintokens [_name];
43 } else {
44 return null;
45 }
46 }
47
48 public WebModulePermission GetModulePermission (string _module) {
49 if (modules.ContainsKey (_module.ToLower ())) {
50 return modules [_module.ToLower ()];
51 }
52 return defaultModulePermission;
53 }
54
55
56 // Admins
57 public void AddAdmin (string _name, string _token, int _permissionLevel, bool _save = true) {
58 AdminToken c = new AdminToken (_name, _token, _permissionLevel);
59 lock (this) {
60 admintokens [_name] = c;
61 if (_save) {
62 Save ();
63 }
64 }
65 }
66
67 public void RemoveAdmin (string _name, bool _save = true) {
68 lock (this) {
69 admintokens.Remove (_name);
70 if (_save) {
71 Save ();
72 }
73 }
74 }
75
76 public bool IsAdmin (string _name) {
77 return admintokens.ContainsKey (_name);
78 }
79
80 public AdminToken[] GetAdmins () {
81 AdminToken[] result = new AdminToken[admintokens.Count];
82 admintokens.Values.CopyTo (result, 0);
83 return result;
84 }
85
86
87 // Commands
88 public void AddModulePermission (string _module, int _permissionLevel, bool _save = true) {
89 WebModulePermission p = new WebModulePermission (_module.ToLower (), _permissionLevel);
90 lock (this) {
91 modules [_module] = p;
92 if (_save) {
93 Save ();
94 }
95 }
96 }
97
98 public void AddKnownModule (string _module) {
99 if (!string.IsNullOrEmpty (_module)) {
100 lock (this) {
101 if (!IsKnownModule( _module)) {
102 knownModules.Add (_module, new WebModulePermission (_module, 0));
103 }
104 }
105 }
106 }
107
108 public bool IsKnownModule (string _module) {
109 if (!string.IsNullOrEmpty (_module)) {
110 lock (this) {
111 return knownModules.ContainsKey (_module);
112 }
113 } else {
114 return false;
115 }
116 }
117
118 public void RemoveModulePermission (string _module, bool _save = true) {
119 lock (this) {
120 modules.Remove (_module.ToLower ());
121 if (_save) {
122 Save ();
123 }
124 }
125 }
126
127 public List<WebModulePermission> GetModules () {
128 List<WebModulePermission> result = new List<WebModulePermission> ();
129 foreach (string module in knownModules.Keys) {
130 if (modules.ContainsKey (module)) {
131 result.Add (modules [module]);
132 } else {
133 result.Add (knownModules [module]);
134 }
135 }
136
137 return result;
138 }
139
140
141 //IO Tasks
142
143 private void InitFileWatcher () {
144 fileWatcher = new FileSystemWatcher (GetFilePath (), GetFileName ());
145 fileWatcher.Changed += new FileSystemEventHandler (OnFileChanged);
146 fileWatcher.Created += new FileSystemEventHandler (OnFileChanged);
147 fileWatcher.Deleted += new FileSystemEventHandler (OnFileChanged);
148 fileWatcher.EnableRaisingEvents = true;
149 }
150
151 private void OnFileChanged (object source, FileSystemEventArgs e) {
152 Log.Out ("Reloading " + PERMISSIONS_FILE);
153 Load ();
154 }
155
156 private string GetFilePath () {
157 return GamePrefs.GetString (EnumGamePrefs.SaveGameFolder);
158 }
159
160 private string GetFileName () {
161 return PERMISSIONS_FILE;
162 }
163
164 private string GetFullPath () {
165 return GetFilePath () + "/" + GetFileName ();
166 }
167
168 public void Load () {
169 admintokens = new Dictionary<string, AdminToken> ();
170 modules = new Dictionary<string, WebModulePermission> ();
171
172 if (!Utils.FileExists (GetFullPath ())) {
173 Log.Out (string.Format ("Permissions file '{0}' not found, creating.", GetFileName ()));
174 Save ();
175 return;
176 }
177
178 Log.Out (string.Format ("Loading permissions file at '{0}'", GetFullPath ()));
179
180 XmlDocument xmlDoc = new XmlDocument ();
181
182 try {
183 xmlDoc.Load (GetFullPath ());
184 } catch (XmlException e) {
185 Log.Error (string.Format ("Failed loading permissions file: {0}", e.Message));
186 return;
187 }
188
189 XmlNode adminToolsNode = xmlDoc.DocumentElement;
190
191 foreach (XmlNode childNode in adminToolsNode.ChildNodes) {
192 if (childNode.Name == "admintokens") {
193 foreach (XmlNode subChild in childNode.ChildNodes) {
194 if (subChild.NodeType == XmlNodeType.Comment) {
195 continue;
196 }
197 if (subChild.NodeType != XmlNodeType.Element) {
198 Log.Warning ("Unexpected XML node found in 'admintokens' section: " + subChild.OuterXml);
199 continue;
200 }
201
202 XmlElement lineItem = (XmlElement)subChild;
203
204 if (!lineItem.HasAttribute ("name")) {
205 Log.Warning ("Ignoring admintoken-entry because of missing 'name' attribute: " + subChild.OuterXml);
206 continue;
207 }
208
209 if (!lineItem.HasAttribute ("token")) {
210 Log.Warning ("Ignoring admintoken-entry because of missing 'token' attribute: " + subChild.OuterXml);
211 continue;
212 }
213
214 if (!lineItem.HasAttribute ("permission_level")) {
215 Log.Warning ("Ignoring admintoken-entry because of missing 'permission_level' attribute: " + subChild.OuterXml);
216 continue;
217 }
218
219 string name = lineItem.GetAttribute ("name");
220 string token = lineItem.GetAttribute ("token");
221 int permissionLevel = 2000;
222 if (!int.TryParse (lineItem.GetAttribute ("permission_level"), out permissionLevel)) {
223 Log.Warning ("Ignoring admintoken-entry because of invalid (non-numeric) value for 'permission_level' attribute: " + subChild.OuterXml);
224 continue;
225 }
226
227 AddAdmin (name, token, permissionLevel, false);
228
229 }
230 }
231
232 if (childNode.Name == "permissions") {
233 foreach (XmlNode subChild in childNode.ChildNodes) {
234 if (subChild.NodeType == XmlNodeType.Comment) {
235 continue;
236 }
237 if (subChild.NodeType != XmlNodeType.Element) {
238 Log.Warning ("Unexpected XML node found in 'permissions' section: " + subChild.OuterXml);
239 continue;
240 }
241
242 XmlElement lineItem = (XmlElement)subChild;
243
244 if (!lineItem.HasAttribute ("module")) {
245 Log.Warning ("Ignoring permission-entry because of missing 'module' attribute: " + subChild.OuterXml);
246 continue;
247 }
248
249 if (!lineItem.HasAttribute ("permission_level")) {
250 Log.Warning ("Ignoring permission-entry because of missing 'permission_level' attribute: " + subChild.OuterXml);
251 continue;
252 }
253
254 int permissionLevel = 0;
255 if (!int.TryParse (lineItem.GetAttribute ("permission_level"), out permissionLevel)) {
256 Log.Warning ("Ignoring permission-entry because of invalid (non-numeric) value for 'permission_level' attribute: " + subChild.OuterXml);
257 continue;
258 }
259
260 AddModulePermission (lineItem.GetAttribute ("module").ToLower (), permissionLevel, false);
261 }
262 }
263
264 }
265
266 Log.Out ("Loading permissions file done.");
267 }
268
269 public void Save () {
270 fileWatcher.EnableRaisingEvents = false;
271
272 using (StreamWriter sw = new StreamWriter(GetFullPath ())) {
273 sw.WriteLine ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
274 sw.WriteLine ("<webpermissions>");
275 sw.WriteLine ();
276 sw.WriteLine (" <admintokens>");
277 sw.WriteLine (" <!-- <token name=\"adminuser1\" token=\"supersecrettoken\" permission_level=\"0\" /> -->");
278 foreach (AdminToken at in admintokens.Values) {
279 sw.WriteLine (string.Format (" <token name=\"{0}\" token=\"{1}\" permission_level=\"{2}\" />", at.name, at.token, at.permissionLevel));
280 }
281 sw.WriteLine (" </admintokens>");
282 sw.WriteLine ();
283 sw.WriteLine (" <permissions>");
284 foreach (WebModulePermission wap in modules.Values) {
285 sw.WriteLine (string.Format (" <permission module=\"{0}\" permission_level=\"{1}\" />", wap.module, wap.permissionLevel));
286 }
287 sw.WriteLine (" <!-- <permission module=\"web.map\" permission_level=\"1000\" /> -->");
288 sw.WriteLine ();
289 sw.WriteLine (" <!-- <permission module=\"webapi.getlog\" permission_level=\"0\" /> -->");
290 sw.WriteLine (" <!-- <permission module=\"webapi.executeconsolecommand\" permission_level=\"0\" /> -->");
291 sw.WriteLine ();
292 sw.WriteLine (" <!-- <permission module=\"webapi.getstats\" permission_level=\"1000\" /> -->");
293 sw.WriteLine (" <!-- <permission module=\"webapi.getplayersonline\" permission_level=\"1000\" /> -->");
294 sw.WriteLine ();
295 sw.WriteLine (" <!-- <permission module=\"webapi.getplayerslocation\" permission_level=\"1000\" /> -->");
296 sw.WriteLine (" <!-- <permission module=\"webapi.viewallplayers\" permission_level=\"1\" /> -->");
297 sw.WriteLine ();
298 sw.WriteLine (" <!-- <permission module=\"webapi.getlandclaims\" permission_level=\"1000\" /> -->");
299 sw.WriteLine (" <!-- <permission module=\"webapi.viewallclaims\" permission_level=\"1\" /> -->");
300 sw.WriteLine ();
301 sw.WriteLine (" <!-- <permission module=\"webapi.getplayerinventory\" permission_level=\"1\" /> -->");
302 sw.WriteLine ();
303 sw.WriteLine (" <!-- <permission module=\"webapi.gethostilelocation\" permission_level=\"1\" /> -->");
304 sw.WriteLine (" <!-- <permission module=\"webapi.getanimalslocation\" permission_level=\"1\" /> -->");
305 sw.WriteLine (" </permissions>");
306 sw.WriteLine ();
307 sw.WriteLine ("</webpermissions>");
308
309 sw.Flush ();
310 sw.Close ();
311 }
312
313 fileWatcher.EnableRaisingEvents = true;
314 }
315
316
317
318 public class AdminToken {
319 public string name;
320 public string token;
321 public int permissionLevel;
322
323 public AdminToken (string _name, string _token, int _permissionLevel) {
324 name = _name;
325 token = _token;
326 permissionLevel = _permissionLevel;
327 }
328 }
329
330 public struct WebModulePermission {
331 public string module;
332 public int permissionLevel;
333
334 public WebModulePermission (string _module, int _permissionLevel) {
335 module = _module;
336 permissionLevel = _permissionLevel;
337 }
338 }
339
340
341 }
342}
343
Note: See TracBrowser for help on using the repository browser.