ProfileManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. using System;
  30. using System.Web;
  31. using System.Web.Configuration;
  32. using System.Configuration;
  33. namespace System.Web.Profile
  34. {
  35. public static class ProfileManager
  36. {
  37. static ProfileSection config;
  38. static ProfileProviderCollection providersCollection;
  39. static ProfileManager ()
  40. {
  41. config = (ProfileSection) WebConfigurationManager.GetSection ("system.web/profile");
  42. }
  43. public static int DeleteInactiveProfiles (ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
  44. {
  45. return Provider.DeleteInactiveProfiles (authenticationOption, userInactiveSinceDate);
  46. }
  47. public static bool DeleteProfile (string username)
  48. {
  49. return Provider.DeleteProfiles (new string [] { username }) > 0;
  50. }
  51. public static int DeleteProfiles (string[] usernames)
  52. {
  53. return Provider.DeleteProfiles (usernames);
  54. }
  55. public static int DeleteProfiles (ProfileInfoCollection profiles)
  56. {
  57. return Provider.DeleteProfiles (profiles);
  58. }
  59. public static ProfileInfoCollection FindInactiveProfilesByUserName (ProfileAuthenticationOption authenticationOption,
  60. string usernameToMatch, DateTime userInactiveSinceDate)
  61. {
  62. int totalRecords = 0;
  63. return Provider.FindInactiveProfilesByUserName (authenticationOption, usernameToMatch, userInactiveSinceDate, 0, int.MaxValue, out totalRecords);
  64. }
  65. public static ProfileInfoCollection FindInactiveProfilesByUserName (ProfileAuthenticationOption authenticationOption,
  66. string usernameToMatch, DateTime userInactiveSinceDate,
  67. int pageIndex, int pageSize, out int totalRecords)
  68. {
  69. return Provider.FindInactiveProfilesByUserName (authenticationOption, usernameToMatch, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
  70. }
  71. public static ProfileInfoCollection FindProfilesByUserName (ProfileAuthenticationOption authenticationOption, string usernameToMatch)
  72. {
  73. int totalRecords = 0;
  74. return Provider.FindProfilesByUserName (authenticationOption, usernameToMatch, 0, int.MaxValue, out totalRecords);
  75. }
  76. public static ProfileInfoCollection FindProfilesByUserName (ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
  77. {
  78. return Provider.FindProfilesByUserName (authenticationOption, usernameToMatch, pageIndex, pageSize, out totalRecords);
  79. }
  80. public static ProfileInfoCollection GetAllInactiveProfiles (ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
  81. {
  82. int totalRecords = 0;
  83. return Provider.GetAllInactiveProfiles (authenticationOption, userInactiveSinceDate, 0, int.MaxValue, out totalRecords);
  84. }
  85. public static ProfileInfoCollection GetAllInactiveProfiles (ProfileAuthenticationOption authenticationOption,
  86. DateTime userInactiveSinceDate, int pageIndex, int pageSize,
  87. out int totalRecords)
  88. {
  89. return Provider.GetAllInactiveProfiles (authenticationOption, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
  90. }
  91. public static ProfileInfoCollection GetAllProfiles (ProfileAuthenticationOption authenticationOption)
  92. {
  93. int totalRecords = 0;
  94. return Provider.GetAllProfiles (authenticationOption, 0, int.MaxValue, out totalRecords);
  95. }
  96. public static ProfileInfoCollection GetAllProfiles (ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
  97. {
  98. return Provider.GetAllProfiles (authenticationOption, pageIndex, pageSize, out totalRecords);
  99. }
  100. public static int GetNumberOfInactiveProfiles (ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
  101. {
  102. return Provider.GetNumberOfInactiveProfiles (authenticationOption, userInactiveSinceDate);
  103. }
  104. public static int GetNumberOfProfiles (ProfileAuthenticationOption authenticationOption)
  105. {
  106. int totalRecords = 0;
  107. Provider.GetAllProfiles (authenticationOption, 0, 1, out totalRecords);
  108. return totalRecords;
  109. }
  110. public static string ApplicationName {
  111. get {
  112. return Provider.ApplicationName;
  113. }
  114. set {
  115. Provider.ApplicationName = value;
  116. }
  117. }
  118. public static bool AutomaticSaveEnabled {
  119. get {
  120. return config.AutomaticSaveEnabled;
  121. }
  122. }
  123. public static bool Enabled {
  124. get {
  125. return config.Enabled;
  126. }
  127. }
  128. [MonoTODO ("check AspNetHostingPermissionLevel")]
  129. public static ProfileProvider Provider {
  130. get {
  131. ProfileProvider p = Providers [config.DefaultProvider];
  132. if (p == null)
  133. throw new ConfigurationErrorsException ("Provider '" + config.DefaultProvider + "' was not found");
  134. return p;
  135. }
  136. }
  137. public static ProfileProviderCollection Providers {
  138. get {
  139. CheckEnabled ();
  140. if (providersCollection == null) {
  141. ProfileProviderCollection providersCollectionTmp = new ProfileProviderCollection ();
  142. ProvidersHelper.InstantiateProviders (config.Providers, providersCollectionTmp, typeof (ProfileProvider));
  143. providersCollection = providersCollectionTmp;
  144. }
  145. return providersCollection;
  146. }
  147. }
  148. static void CheckEnabled ()
  149. {
  150. if (!Enabled)
  151. throw new Exception ("This feature is not enabled. To enable it, add <profile enabled=\"true\"> to your configuration file.");
  152. }
  153. }
  154. }