SettingsHelper.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Copyright (C) 2014-2018 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Windows.Forms;
  11. using System.Xml;
  12. namespace SMBServer
  13. {
  14. public class SettingsHelper
  15. {
  16. public const string SettingsFileName = "Settings.xml";
  17. public static XmlDocument ReadXmlDocument(string path)
  18. {
  19. XmlDocument doc = new XmlDocument();
  20. doc.Load(path);
  21. return doc;
  22. }
  23. public static XmlDocument ReadSettingsXML()
  24. {
  25. string executableDirectory = Path.GetDirectoryName(Application.ExecutablePath) + "\\";
  26. XmlDocument document = ReadXmlDocument(executableDirectory + SettingsFileName);
  27. return document;
  28. }
  29. public static UserCollection ReadUserSettings()
  30. {
  31. UserCollection users = new UserCollection();
  32. XmlDocument document = ReadSettingsXML();
  33. XmlNode usersNode = document.SelectSingleNode("Settings/Users");
  34. foreach (XmlNode userNode in usersNode.ChildNodes)
  35. {
  36. string accountName = userNode.Attributes["AccountName"].Value;
  37. string password = userNode.Attributes["Password"].Value;
  38. users.Add(accountName, password);
  39. }
  40. return users;
  41. }
  42. public static List<ShareSettings> ReadSharesSettings()
  43. {
  44. List<ShareSettings> shares = new List<ShareSettings>();
  45. XmlDocument document = ReadSettingsXML();
  46. XmlNode sharesNode = document.SelectSingleNode("Settings/Shares");
  47. foreach (XmlNode shareNode in sharesNode.ChildNodes)
  48. {
  49. string shareName = shareNode.Attributes["Name"].Value;
  50. string sharePath = shareNode.Attributes["Path"].Value;
  51. XmlNode readAccessNode = shareNode.SelectSingleNode("ReadAccess");
  52. List<string> readAccess = ReadAccessList(readAccessNode);
  53. XmlNode writeAccessNode = shareNode.SelectSingleNode("WriteAccess");
  54. List<string> writeAccess = ReadAccessList(writeAccessNode);
  55. ShareSettings share = new ShareSettings(shareName, sharePath, readAccess, writeAccess);
  56. shares.Add(share);
  57. }
  58. return shares;
  59. }
  60. private static List<string> ReadAccessList(XmlNode node)
  61. {
  62. List<string> result = new List<string>();
  63. if (node != null)
  64. {
  65. string accounts = node.Attributes["Accounts"].Value;
  66. if (accounts == "*")
  67. {
  68. result.Add("Users");
  69. }
  70. else
  71. {
  72. string[] splitted = accounts.Split(',');
  73. result.AddRange(splitted);
  74. }
  75. }
  76. return result;
  77. }
  78. }
  79. }