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

Last change on this file since 325 was 325, checked in by alloc, 6 years ago

Code style cleanup (mostly whitespace changes, enforcing braces, using cleanup)

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