Changeset 325 for binary-improvements/MapRendering/Web/WebPermissions.cs
- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/WebPermissions.cs
r279 r325 1 using System;2 1 using System.Collections.Generic; 3 2 using System.IO; 4 3 using System.Xml; 5 4 6 namespace AllocsFixes.NetConnections.Servers.Web 7 { 5 namespace AllocsFixes.NetConnections.Servers.Web { 8 6 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 7 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; 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; 28 18 29 19 public WebPermissions () { … … 33 23 } 34 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 35 37 public bool ModuleAllowedWithLevel (string _module, int _level) { 36 38 WebModulePermission permInfo = GetModulePermission (_module); … … 41 43 if (IsAdmin (_name) && admintokens [_name].token == _token) { 42 44 return admintokens [_name]; 43 } else {44 return null; 45 }45 } 46 47 return null; 46 48 } 47 49 … … 50 52 return modules [_module.ToLower ()]; 51 53 } 54 52 55 return defaultModulePermission; 53 56 } … … 83 86 return result; 84 87 } 85 88 86 89 87 90 // Commands … … 99 102 if (!string.IsNullOrEmpty (_module)) { 100 103 lock (this) { 101 if (!IsKnownModule(_module)) {104 if (!IsKnownModule (_module)) { 102 105 knownModules.Add (_module, new WebModulePermission (_module, _defaultPermission)); 103 } 106 } 107 104 108 if (_defaultPermission > 0 && !modules.ContainsKey (_module.ToLower ())) { 105 109 AddModulePermission (_module, _defaultPermission); … … 114 118 return knownModules.ContainsKey (_module); 115 119 } 116 } else {117 return false; 118 }119 } 120 120 } 121 122 return false; 123 } 124 121 125 public void RemoveModulePermission (string _module, bool _save = true) { 122 126 lock (this) { … … 140 144 return result; 141 145 } 142 146 143 147 144 148 //IO Tasks … … 146 150 private void InitFileWatcher () { 147 151 fileWatcher = new FileSystemWatcher (GetFilePath (), GetFileName ()); 148 fileWatcher.Changed += new FileSystemEventHandler (OnFileChanged);149 fileWatcher.Created += new FileSystemEventHandler (OnFileChanged);150 fileWatcher.Deleted += new FileSystemEventHandler (OnFileChanged);152 fileWatcher.Changed += OnFileChanged; 153 fileWatcher.Created += OnFileChanged; 154 fileWatcher.Deleted += OnFileChanged; 151 155 fileWatcher.EnableRaisingEvents = true; 152 156 } … … 198 202 continue; 199 203 } 204 200 205 if (subChild.NodeType != XmlNodeType.Element) { 201 206 Log.Warning ("Unexpected XML node found in 'admintokens' section: " + subChild.OuterXml); … … 203 208 } 204 209 205 XmlElement lineItem = (XmlElement) subChild;210 XmlElement lineItem = (XmlElement) subChild; 206 211 207 212 if (!lineItem.HasAttribute ("name")) { 208 Log.Warning ("Ignoring admintoken-entry because of missing 'name' attribute: " + subChild.OuterXml); 213 Log.Warning ("Ignoring admintoken-entry because of missing 'name' attribute: " + 214 subChild.OuterXml); 209 215 continue; 210 216 } 211 217 212 218 if (!lineItem.HasAttribute ("token")) { 213 Log.Warning ("Ignoring admintoken-entry because of missing 'token' attribute: " + subChild.OuterXml); 219 Log.Warning ("Ignoring admintoken-entry because of missing 'token' attribute: " + 220 subChild.OuterXml); 214 221 continue; 215 222 } 216 223 217 224 if (!lineItem.HasAttribute ("permission_level")) { 218 Log.Warning ("Ignoring admintoken-entry because of missing 'permission_level' attribute: " + subChild.OuterXml); 225 Log.Warning ("Ignoring admintoken-entry because of missing 'permission_level' attribute: " + 226 subChild.OuterXml); 219 227 continue; 220 228 } … … 224 232 int permissionLevel = 2000; 225 233 if (!int.TryParse (lineItem.GetAttribute ("permission_level"), out permissionLevel)) { 226 Log.Warning ("Ignoring admintoken-entry because of invalid (non-numeric) value for 'permission_level' attribute: " + subChild.OuterXml); 227 continue; 228 } 229 230 AddAdmin (name, token, permissionLevel, false); 231 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); 232 241 } 233 242 } … … 238 247 continue; 239 248 } 249 240 250 if (subChild.NodeType != XmlNodeType.Element) { 241 251 Log.Warning ("Unexpected XML node found in 'permissions' section: " + subChild.OuterXml); … … 243 253 } 244 254 245 XmlElement lineItem = (XmlElement) subChild;255 XmlElement lineItem = (XmlElement) subChild; 246 256 247 257 if (!lineItem.HasAttribute ("module")) { 248 Log.Warning ("Ignoring permission-entry because of missing 'module' attribute: " + subChild.OuterXml); 249 continue; 250 } 251 258 Log.Warning ("Ignoring permission-entry because of missing 'module' attribute: " + 259 subChild.OuterXml); 260 continue; 261 } 262 252 263 if (!lineItem.HasAttribute ("permission_level")) { 253 Log.Warning ("Ignoring permission-entry because of missing 'permission_level' attribute: " + subChild.OuterXml); 254 continue; 255 } 256 264 Log.Warning ("Ignoring permission-entry because of missing 'permission_level' attribute: " + 265 subChild.OuterXml); 266 continue; 267 } 268 257 269 int permissionLevel = 0; 258 270 if (!int.TryParse (lineItem.GetAttribute ("permission_level"), out permissionLevel)) { 259 Log.Warning ("Ignoring permission-entry because of invalid (non-numeric) value for 'permission_level' attribute: " + subChild.OuterXml); 260 continue; 261 } 262 263 AddModulePermission (lineItem.GetAttribute ("module").ToLower (), permissionLevel, false); 264 } 265 } 266 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 } 267 280 } 268 281 … … 273 286 fileWatcher.EnableRaisingEvents = false; 274 287 275 using (StreamWriter sw = new StreamWriter(GetFullPath ())) { 276 sw.WriteLine ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 277 sw.WriteLine ("<webpermissions>"); 278 sw.WriteLine (); 279 sw.WriteLine (" <admintokens>"); 280 sw.WriteLine (" <!-- <token name=\"adminuser1\" token=\"supersecrettoken\" permission_level=\"0\" /> -->"); 281 foreach (AdminToken at in admintokens.Values) { 282 sw.WriteLine (string.Format (" <token name=\"{0}\" token=\"{1}\" permission_level=\"{2}\" />", at.name, at.token, at.permissionLevel)); 283 } 284 sw.WriteLine (" </admintokens>"); 285 sw.WriteLine (); 286 sw.WriteLine (" <permissions>"); 287 foreach (WebModulePermission wap in modules.Values) { 288 sw.WriteLine (string.Format (" <permission module=\"{0}\" permission_level=\"{1}\" />", wap.module, wap.permissionLevel)); 289 } 290 sw.WriteLine (" <!-- <permission module=\"web.map\" permission_level=\"1000\" /> -->"); 291 sw.WriteLine (); 292 sw.WriteLine (" <!-- <permission module=\"webapi.getlog\" permission_level=\"0\" /> -->"); 293 sw.WriteLine (" <!-- <permission module=\"webapi.executeconsolecommand\" permission_level=\"0\" /> -->"); 294 sw.WriteLine (); 295 sw.WriteLine (" <!-- <permission module=\"webapi.getstats\" permission_level=\"1000\" /> -->"); 296 sw.WriteLine (" <!-- <permission module=\"webapi.getplayersonline\" permission_level=\"1000\" /> -->"); 297 sw.WriteLine (); 298 sw.WriteLine (" <!-- <permission module=\"webapi.getplayerslocation\" permission_level=\"1000\" /> -->"); 299 sw.WriteLine (" <!-- <permission module=\"webapi.viewallplayers\" permission_level=\"1\" /> -->"); 300 sw.WriteLine (); 301 sw.WriteLine (" <!-- <permission module=\"webapi.getlandclaims\" permission_level=\"1000\" /> -->"); 302 sw.WriteLine (" <!-- <permission module=\"webapi.viewallclaims\" permission_level=\"1\" /> -->"); 303 sw.WriteLine (); 304 sw.WriteLine (" <!-- <permission module=\"webapi.getplayerinventory\" permission_level=\"1\" /> -->"); 305 sw.WriteLine (); 306 sw.WriteLine (" <!-- <permission module=\"webapi.gethostilelocation\" permission_level=\"1\" /> -->"); 307 sw.WriteLine (" <!-- <permission module=\"webapi.getanimalslocation\" permission_level=\"1\" /> -->"); 308 sw.WriteLine (" </permissions>"); 309 sw.WriteLine (); 310 sw.WriteLine ("</webpermissions>"); 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>"); 311 331 312 332 sw.Flush (); … … 318 338 319 339 320 321 340 public class AdminToken { 322 341 public string name; 342 public int permissionLevel; 323 343 public string token; 324 public int permissionLevel;325 344 326 345 public AdminToken (string _name, string _token, int _permissionLevel) { … … 340 359 } 341 360 } 342 343 344 361 } 345 362 } 346
Note:
See TracChangeset
for help on using the changeset viewer.