ProfileManager.cs 7.1 KB

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