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