2008-11-18 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Security / Roles.cs
1 //
2 // System.Web.Security.Roles
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //      Chris Toshok  <toshok@ximian.com>
8 //
9 // (C) 2003 Ben Maurer
10 // Copyright (c) 2005,2006 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if NET_2_0
33
34 using System.Configuration.Provider;
35 using System.Web.Configuration;
36 using System.Configuration;
37
38 namespace System.Web.Security {
39
40         public static class Roles {
41 #if TARGET_J2EE
42                 const string Roles_cookie_protection = "Roles.cookie_protection";
43                 static RoleManagerSection config {
44                         get {
45                                 return (RoleManagerSection) WebConfigurationManager.GetSection ("system.web/roleManager");
46                         }
47                 }
48
49                 const string Roles_providersCollection = "Roles.providersCollection";
50                 static RoleProviderCollection providersCollection {
51                         get {
52                                 return (RoleProviderCollection)AppDomain.CurrentDomain.GetData (Roles_providersCollection);
53                         }
54
55                         set {
56                                 AppDomain.CurrentDomain.SetData (Roles_providersCollection, value);
57                         }
58                 }
59 #else
60                 static RoleManagerSection config;
61                 static RoleProviderCollection providersCollection;
62
63                 static Roles ()
64                 {
65                         config = (RoleManagerSection)WebConfigurationManager.GetSection ("system.web/roleManager");
66                 }
67 #endif
68
69
70                 public static void AddUsersToRole (string [] usernames, string rolename)
71                 {
72                         Provider.AddUsersToRoles (usernames, new string[] {rolename});
73                 }
74                 
75                 public static void AddUsersToRoles (string [] usernames, string [] rolenames)
76                 {
77                         Provider.AddUsersToRoles (usernames, rolenames);
78                 }
79                 
80                 public static void AddUserToRole (string username, string rolename)
81                 {
82                         Provider.AddUsersToRoles (new string[] {username}, new string[] {rolename});
83                 }
84                 
85                 public static void AddUserToRoles (string username, string [] rolenames)
86                 {
87                         Provider.AddUsersToRoles (new string[] {username}, rolenames);
88                 }
89                 
90                 public static void CreateRole (string rolename)
91                 {
92                         Provider.CreateRole (rolename);
93                 }
94                 
95                 public static void DeleteCookie ()
96                 {
97                         if (CacheRolesInCookie) {
98                                 HttpContext context = HttpContext.Current;
99                                 if (context == null)
100                                         throw new HttpException ("Context is null.");
101
102                                 HttpResponse response = context.Response;
103                                 if (response == null)
104                                         throw new HttpException ("Response is null.");
105
106                                 HttpCookieCollection cc = response.Cookies;
107                                 cc.Remove (CookieName);
108                                 HttpCookie expiration_cookie = new HttpCookie (CookieName, "");
109                                 expiration_cookie.Expires = new DateTime (1999, 10, 12);
110                                 expiration_cookie.Path = CookiePath;
111                                 cc.Add (expiration_cookie);
112                         }
113                 }
114                 
115                 public static bool DeleteRole (string rolename)
116                 {
117                         return Provider.DeleteRole (rolename, true);
118                 }
119                 
120                 public static bool DeleteRole (string rolename, bool throwOnPopulatedRole)
121                 {
122                         return Provider.DeleteRole (rolename, throwOnPopulatedRole);
123                 }
124                 
125                 public static string [] GetAllRoles ()
126                 {
127                         return Provider.GetAllRoles ();
128                 }
129                 
130                 public static string [] GetRolesForUser ()
131                 {
132                         return Provider.GetRolesForUser (CurrentUser);
133                 }
134                 
135                 static string CurrentUser {
136                         get {
137                                 if (HttpContext.Current != null && HttpContext.Current.User != null)
138                                         return HttpContext.Current.User.Identity.Name;
139                                 else
140                                         return System.Threading.Thread.CurrentPrincipal.Identity.Name;
141                         }
142                 }
143                 
144                 public static string [] GetRolesForUser (string username)
145                 {
146                         return Provider.GetRolesForUser (username);
147                 }
148                 
149                 public static string [] GetUsersInRole (string rolename)
150                 {
151                         return Provider.GetUsersInRole (rolename);
152                 }
153                 
154                 public static bool IsUserInRole (string rolename)
155                 {
156                         return Provider.IsUserInRole (CurrentUser, rolename);
157                 }
158                 
159                 public static bool IsUserInRole (string username, string rolename)
160                 {
161                         return Provider.IsUserInRole (username, rolename);
162                 }
163                 
164                 public static void RemoveUserFromRole (string username, string rolename)
165                 {
166                         Provider.RemoveUsersFromRoles (new string[] {username}, new string[] {rolename});
167                 }
168                 
169                 public static void RemoveUserFromRoles (string username, string [] rolenames)
170                 {
171                         Provider.RemoveUsersFromRoles (new string[] {username}, rolenames);
172                 }
173                 
174                 public static void RemoveUsersFromRole (string [] usernames, string rolename)
175                 {
176                         Provider.RemoveUsersFromRoles (usernames, new string[] {rolename});
177                 }
178                 
179                 public static void RemoveUsersFromRoles (string [] usernames, string [] rolenames)
180                 {
181                         Provider.RemoveUsersFromRoles (usernames, rolenames);
182                 }
183                 
184                 public static bool RoleExists (string rolename)
185                 {
186                         return Provider.RoleExists (rolename);
187                 }
188                 
189                 public static string[] FindUsersInRole (string rolename, string usernameToMatch)
190                 {
191                         return Provider.FindUsersInRole (rolename, usernameToMatch);
192                 }
193                 
194                 public static string ApplicationName {
195                         get { return Provider.ApplicationName; }
196                         set { Provider.ApplicationName = value; }
197                 }
198                 
199                 public static bool CacheRolesInCookie {
200                         get { return config.CacheRolesInCookie; }
201                 }
202                 
203                 public static string CookieName {
204                         get { return config.CookieName; }
205                 }
206                 
207                 public static string CookiePath {
208                         get { return config.CookiePath; }
209                 }
210                 
211                 public static CookieProtection CookieProtectionValue {
212                         get { return config.CookieProtection; }
213                 }
214                 
215                 public static bool CookieRequireSSL {
216                         get { return config.CookieRequireSSL; }
217                 }
218                 
219                 public static bool CookieSlidingExpiration {
220                         get { return config.CookieSlidingExpiration; }
221                 }
222                 
223                 public static int CookieTimeout {
224                         get { return (int)config.CookieTimeout.TotalMinutes; }
225                 }
226
227                 public static bool CreatePersistentCookie {
228                         get { return config.CreatePersistentCookie; }
229                 }
230
231                 public static string Domain {
232                         get { return config.Domain; }
233                 }
234
235                 public static bool Enabled {
236                         get { return config.Enabled; }
237                 }
238
239                 public static int MaxCachedResults {
240                         get { return config.MaxCachedResults; }
241                 }
242                 
243                 public static RoleProvider Provider {
244                         get {
245                                 RoleProvider p = Providers [config.DefaultProvider];
246                                 if (p == null)
247                                         throw new ConfigurationErrorsException ("Default Role Provider could not be found: Cannot instantiate provider: '" + config.DefaultProvider + "'.");
248                                 return p;
249                         }
250                 }
251                 
252                 public static RoleProviderCollection Providers {
253                         get {
254                                 CheckEnabled ();
255                                 if (providersCollection == null) {
256                                         RoleProviderCollection providersCollectionTmp = new RoleProviderCollection ();
257                                         ProvidersHelper.InstantiateProviders (config.Providers, providersCollectionTmp, typeof (RoleProvider));
258                                         providersCollection = providersCollectionTmp;
259                                 }
260                                 return providersCollection;
261                         }
262                 }
263
264                 // private stuff
265                 static void CheckEnabled ()
266                 {
267                         if (!Enabled)
268                                 throw new ProviderException ("This feature is not enabled.  To enable it, add <roleManager enabled=\"true\"> to your configuration file.");
269                 }
270         }
271 }
272
273 #endif