RoleManagerSectionMapper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // Mono.Web.Util.RoleManagerSectionMapper
  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. using System;
  30. using System.Collections.Generic;
  31. using System.Configuration;
  32. using System.Web.Configuration;
  33. namespace Mono.Web.Util
  34. {
  35. internal class RoleManagerSectionMapper : ISectionSettingsMapper
  36. {
  37. public object MapSection (object _section, List <SettingsMappingWhat> whats)
  38. {
  39. RoleManagerSection section = _section as RoleManagerSection;
  40. if (section == null)
  41. return _section;
  42. List <SettingsMappingWhatContents> contents;
  43. foreach (SettingsMappingWhat what in whats) {
  44. contents = what.Contents;
  45. if (contents == null || contents.Count == 0)
  46. continue;
  47. foreach (SettingsMappingWhatContents item in contents) {
  48. switch (item.Operation) {
  49. case SettingsMappingWhatOperation.Add:
  50. ProcessAdd (section, item);
  51. break;
  52. case SettingsMappingWhatOperation.Clear:
  53. ProcessClear (section, item);
  54. break;
  55. case SettingsMappingWhatOperation.Replace:
  56. ProcessReplace (section, item);
  57. break;
  58. case SettingsMappingWhatOperation.Remove:
  59. ProcessRemove (section, item);
  60. break;
  61. }
  62. }
  63. }
  64. return section;
  65. }
  66. bool GetCommonAttributes (SettingsMappingWhatContents how, out string name, out string type)
  67. {
  68. name = type = null;
  69. Dictionary <string, string> attrs = how.Attributes;
  70. if (attrs == null || attrs.Count == 0)
  71. return false;
  72. if (!attrs.TryGetValue ("name", out name))
  73. return false;
  74. if (String.IsNullOrEmpty (name))
  75. return false;
  76. attrs.TryGetValue ("type", out type);
  77. return true;
  78. }
  79. void SetProviderProperties (SettingsMappingWhatContents how, ProviderSettings prov)
  80. {
  81. Dictionary <string, string> attrs = how.Attributes;
  82. if (attrs == null || attrs.Count == 0)
  83. return;
  84. string key;
  85. foreach (KeyValuePair <string, string> kvp in attrs) {
  86. key = kvp.Key;
  87. if (key == "name")
  88. continue;
  89. if (key == "type") {
  90. prov.Type = kvp.Value;
  91. continue;
  92. }
  93. prov.Parameters [key] = kvp.Value;
  94. }
  95. }
  96. void ProcessAdd (RoleManagerSection section, SettingsMappingWhatContents how)
  97. {
  98. string name, type;
  99. if (!GetCommonAttributes (how, out name, out type))
  100. return;
  101. ProviderSettingsCollection providers = section.Providers;
  102. ProviderSettings provider = providers [name];
  103. if (provider != null)
  104. return;
  105. ProviderSettings prov = new ProviderSettings (name, type);
  106. SetProviderProperties (how, prov);
  107. providers.Add (prov);
  108. }
  109. void ProcessRemove (RoleManagerSection section, SettingsMappingWhatContents how)
  110. {
  111. string name, type;
  112. if (!GetCommonAttributes (how, out name, out type))
  113. return;
  114. ProviderSettingsCollection providers = section.Providers;
  115. ProviderSettings provider = providers [name];
  116. if (provider != null) {
  117. if (provider.Type != type)
  118. return;
  119. providers.Remove (name);
  120. }
  121. }
  122. void ProcessClear (RoleManagerSection section, SettingsMappingWhatContents how)
  123. {
  124. section.Providers.Clear ();
  125. }
  126. void ProcessReplace (RoleManagerSection section, SettingsMappingWhatContents how)
  127. {
  128. string name, type;
  129. if (!GetCommonAttributes (how, out name, out type))
  130. return;
  131. ProviderSettings provider = section.Providers [name];
  132. if (provider != null)
  133. SetProviderProperties (how, provider);
  134. }
  135. }
  136. }