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