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