ProfileManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // System.Web.Profile.ProfileManager.cs
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. // Vladimir Krasnov ([email protected])
  7. //
  8. // (C) 2005 Novell, Inc (http://www.novell.com)
  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.Web;
  32. using System.Web.Configuration;
  33. using System.Configuration;
  34. namespace System.Web.Profile
  35. {
  36. public static class ProfileManager
  37. {
  38. #if TARGET_J2EE
  39. const string Profiles_config = "Profiles.config";
  40. const string Profiles_ProfileProviderCollection = "Profiles.ProfileProviderCollection";
  41. private static ProfileSection config
  42. {
  43. get
  44. {
  45. object o = AppDomain.CurrentDomain.GetData (Profiles_config);
  46. if (o == null) {
  47. config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile");
  48. return (ProfileSection) config;
  49. }
  50. return (ProfileSection) o;
  51. }
  52. set
  53. {
  54. AppDomain.CurrentDomain.SetData (Profiles_config, value);
  55. }
  56. }
  57. private static ProfileProviderCollection providersCollection
  58. {
  59. get
  60. {
  61. object o = AppDomain.CurrentDomain.GetData (Profiles_ProfileProviderCollection);
  62. return (ProfileProviderCollection) o;
  63. }
  64. set
  65. {
  66. AppDomain.CurrentDomain.SetData (Profiles_ProfileProviderCollection, value);
  67. }
  68. }
  69. #else
  70. private static ProfileSection config;
  71. private static ProfileProviderCollection providersCollection;
  72. static ProfileManager ()
  73. {
  74. config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile");
  75. }
  76. #endif
  77. public static int DeleteInactiveProfiles (ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
  78. {
  79. return Provider.DeleteInactiveProfiles (authenticationOption, userInactiveSinceDate);
  80. }
  81. public static bool DeleteProfile (string username)
  82. {
  83. return Provider.DeleteProfiles (new string [] { username }) > 0;
  84. }
  85. public static int DeleteProfiles (string[] usernames)
  86. {
  87. return Provider.DeleteProfiles (usernames);
  88. }
  89. public static int DeleteProfiles (ProfileInfoCollection profiles)
  90. {
  91. return Provider.DeleteProfiles (profiles);
  92. }
  93. public static ProfileInfoCollection FindInactiveProfilesByUserName (ProfileAuthenticationOption authenticationOption,
  94. string usernameToMatch, DateTime userInactiveSinceDate)
  95. {
  96. int totalRecords = 0;
  97. return Provider.FindInactiveProfilesByUserName (authenticationOption, usernameToMatch, userInactiveSinceDate, 0, int.MaxValue, out totalRecords);
  98. }
  99. public static ProfileInfoCollection FindInactiveProfilesByUserName (ProfileAuthenticationOption authenticationOption,
  100. string usernameToMatch, DateTime userInactiveSinceDate,
  101. int pageIndex, int pageSize, out int totalRecords)
  102. {
  103. return Provider.FindInactiveProfilesByUserName (authenticationOption, usernameToMatch, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
  104. }
  105. public static ProfileInfoCollection FindProfilesByUserName (ProfileAuthenticationOption authenticationOption, string usernameToMatch)
  106. {
  107. int totalRecords = 0;
  108. return Provider.FindProfilesByUserName (authenticationOption, usernameToMatch, 0, int.MaxValue, out totalRecords);
  109. }
  110. public static ProfileInfoCollection FindProfilesByUserName (ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
  111. {
  112. return Provider.FindProfilesByUserName (authenticationOption, usernameToMatch, pageIndex, pageSize, out totalRecords);
  113. }
  114. public static ProfileInfoCollection GetAllInactiveProfiles (ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
  115. {
  116. int totalRecords = 0;
  117. return Provider.GetAllInactiveProfiles (authenticationOption, userInactiveSinceDate, 0, int.MaxValue, out totalRecords);
  118. }
  119. public static ProfileInfoCollection GetAllInactiveProfiles (ProfileAuthenticationOption authenticationOption,
  120. DateTime userInactiveSinceDate, int pageIndex, int pageSize,
  121. out int totalRecords)
  122. {
  123. return Provider.GetAllInactiveProfiles (authenticationOption, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
  124. }
  125. public static ProfileInfoCollection GetAllProfiles (ProfileAuthenticationOption authenticationOption)
  126. {
  127. int totalRecords = 0;
  128. return Provider.GetAllProfiles (authenticationOption, 0, int.MaxValue, out totalRecords);
  129. }
  130. public static ProfileInfoCollection GetAllProfiles (ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
  131. {
  132. return Provider.GetAllProfiles (authenticationOption, pageIndex, pageSize, out totalRecords);
  133. }
  134. public static int GetNumberOfInactiveProfiles (ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
  135. {
  136. return Provider.GetNumberOfInactiveProfiles (authenticationOption, userInactiveSinceDate);
  137. }
  138. public static int GetNumberOfProfiles (ProfileAuthenticationOption authenticationOption)
  139. {
  140. int totalRecords = 0;
  141. Provider.GetAllProfiles (authenticationOption, 0, 1, out totalRecords);
  142. return totalRecords;
  143. }
  144. public static string ApplicationName {
  145. get {
  146. return Provider.ApplicationName;
  147. }
  148. set {
  149. Provider.ApplicationName = value;
  150. }
  151. }
  152. public static bool AutomaticSaveEnabled {
  153. get {
  154. return config.AutomaticSaveEnabled;
  155. }
  156. }
  157. public static bool Enabled {
  158. get {
  159. return config.Enabled;
  160. }
  161. }
  162. [MonoTODO ("check AspNetHostingPermissionLevel")]
  163. public static ProfileProvider Provider {
  164. get {
  165. ProfileProvider p = Providers [config.DefaultProvider];
  166. if (p == null)
  167. throw new ConfigurationErrorsException ("Provider '" + config.DefaultProvider + "' was not found");
  168. return p;
  169. }
  170. }
  171. public static ProfileProviderCollection Providers {
  172. get {
  173. CheckEnabled ();
  174. if (providersCollection == null) {
  175. ProfileProviderCollection providersCollectionTmp = new ProfileProviderCollection ();
  176. ProvidersHelper.InstantiateProviders (config.Providers, providersCollectionTmp, typeof (ProfileProvider));
  177. providersCollection = providersCollectionTmp;
  178. }
  179. return providersCollection;
  180. }
  181. }
  182. private static void CheckEnabled ()
  183. {
  184. if (!Enabled)
  185. throw new Exception ("This feature is not enabled. To enable it, add <profile enabled=\"true\"> to your configuration file.");
  186. }
  187. }
  188. }
  189. #endif