MembershipSectionMapper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // System.Web.Util.MembershipSectionMapper
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2007 Novell, Inc
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Configuration;
  33. using System.Web.Configuration;
  34. namespace System.Web.Util
  35. {
  36. internal class MembershipSectionMapper : ISectionSettingsMapper
  37. {
  38. public object MapSection (object _section, List <SettingsMappingWhat> whats)
  39. {
  40. MembershipSection section = _section as MembershipSection;
  41. if (section == null)
  42. return _section;
  43. List <SettingsMappingWhatContents> contents;
  44. foreach (SettingsMappingWhat what in whats) {
  45. contents = what.Contents;
  46. if (contents == null || contents.Count == 0)
  47. continue;
  48. foreach (SettingsMappingWhatContents item in contents) {
  49. switch (item.Operation) {
  50. case SettingsMappingWhatOperation.Add:
  51. ProcessAdd (section, item);
  52. break;
  53. case SettingsMappingWhatOperation.Clear:
  54. ProcessClear (section, item);
  55. break;
  56. case SettingsMappingWhatOperation.Replace:
  57. ProcessReplace (section, item);
  58. break;
  59. case SettingsMappingWhatOperation.Remove:
  60. ProcessRemove (section, item);
  61. break;
  62. }
  63. }
  64. }
  65. return section;
  66. }
  67. bool GetCommonAttributes (SettingsMappingWhatContents how, out string name, out string type)
  68. {
  69. name = type = null;
  70. Dictionary <string, string> attrs = how.Attributes;
  71. if (attrs == null || attrs.Count == 0)
  72. return false;
  73. if (!attrs.TryGetValue ("name", out name))
  74. return false;
  75. if (String.IsNullOrEmpty (name))
  76. return false;
  77. attrs.TryGetValue ("type", out type);
  78. return true;
  79. }
  80. void SetProviderProperties (SettingsMappingWhatContents how, ProviderSettings prov)
  81. {
  82. Dictionary <string, string> attrs = how.Attributes;
  83. if (attrs == null || attrs.Count == 0)
  84. return;
  85. string key;
  86. foreach (KeyValuePair <string, string> kvp in attrs) {
  87. key = kvp.Key;
  88. if (key == "name")
  89. continue;
  90. if (key == "type") {
  91. prov.Type = kvp.Value;
  92. continue;
  93. }
  94. prov.Parameters [key] = kvp.Value;
  95. }
  96. }
  97. void ProcessAdd (MembershipSection section, SettingsMappingWhatContents how)
  98. {
  99. string name, type;
  100. if (!GetCommonAttributes (how, out name, out type))
  101. return;
  102. ProviderSettingsCollection providers = section.Providers;
  103. ProviderSettings provider = providers [name];
  104. if (provider != null)
  105. return;
  106. ProviderSettings prov = new ProviderSettings (name, type);
  107. SetProviderProperties (how, prov);
  108. providers.Add (prov);
  109. }
  110. void ProcessRemove (MembershipSection section, SettingsMappingWhatContents how)
  111. {
  112. string name, type;
  113. if (!GetCommonAttributes (how, out name, out type))
  114. return;
  115. ProviderSettingsCollection providers = section.Providers;
  116. ProviderSettings provider = providers [name];
  117. if (provider != null) {
  118. if (provider.Type != type)
  119. return;
  120. providers.Remove (name);
  121. }
  122. }
  123. void ProcessClear (MembershipSection section, SettingsMappingWhatContents how)
  124. {
  125. section.Providers.Clear ();
  126. }
  127. void ProcessReplace (MembershipSection section, SettingsMappingWhatContents how)
  128. {
  129. string name, type;
  130. if (!GetCommonAttributes (how, out name, out type))
  131. return;
  132. ProviderSettings provider = section.Providers [name];
  133. if (provider != null)
  134. SetProviderProperties (how, provider);
  135. }
  136. }
  137. }
  138. #endif