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