Merge pull request #4453 from lambdageek/bug-49721
[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
33 using System.Configuration.Provider;
34 using System.Web.Configuration;
35 using System.Configuration;
36
37 namespace System.Web.Security {
38
39         public static class Roles {
40                 static RoleManagerSection config;
41                 static RoleProviderCollection providersCollection;
42
43                 static Roles ()
44                 {
45                         config = (RoleManagerSection)WebConfigurationManager.GetSection ("system.web/roleManager");
46                 }
47
48
49                 public static void AddUsersToRole (string [] usernames, string roleName)
50                 {
51                         Provider.AddUsersToRoles (usernames, new string[] {roleName});
52                 }
53                 
54                 public static void AddUsersToRoles (string [] usernames, string [] roleNames)
55                 {
56                         Provider.AddUsersToRoles (usernames, roleNames);
57                 }
58                 
59                 public static void AddUserToRole (string username, string roleName)
60                 {
61                         Provider.AddUsersToRoles (new string[] {username}, new string[] {roleName});
62                 }
63                 
64                 public static void AddUserToRoles (string username, string [] roleNames)
65                 {
66                         Provider.AddUsersToRoles (new string[] {username}, roleNames);
67                 }
68                 
69                 public static void CreateRole (string roleName)
70                 {
71                         Provider.CreateRole (roleName);
72                 }
73                 
74                 public static void DeleteCookie ()
75                 {
76                         if (CacheRolesInCookie) {
77                                 HttpContext context = HttpContext.Current;
78                                 if (context == null)
79                                         throw new HttpException ("Context is null.");
80
81                                 HttpResponse response = context.Response;
82                                 if (response == null)
83                                         throw new HttpException ("Response is null.");
84
85                                 HttpCookieCollection cc = response.Cookies;
86                                 cc.Remove (CookieName);
87                                 HttpCookie expiration_cookie = new HttpCookie (CookieName, "");
88                                 expiration_cookie.Expires = new DateTime (1999, 10, 12);
89                                 expiration_cookie.Path = CookiePath;
90                                 cc.Add (expiration_cookie);
91                         }
92                 }
93                 
94                 public static bool DeleteRole (string roleName)
95                 {
96                         return Provider.DeleteRole (roleName, true);
97                 }
98                 
99                 public static bool DeleteRole (string roleName, bool throwOnPopulatedRole)
100                 {
101                         return Provider.DeleteRole (roleName, throwOnPopulatedRole);
102                 }
103                 
104                 public static string [] GetAllRoles ()
105                 {
106                         return Provider.GetAllRoles ();
107                 }
108                 
109                 public static string [] GetRolesForUser ()
110                 {
111                         return Provider.GetRolesForUser (CurrentUser);
112                 }
113                 
114                 static string CurrentUser {
115                         get {
116                                 if (HttpContext.Current != null && HttpContext.Current.User != null)
117                                         return HttpContext.Current.User.Identity.Name;
118                                 else
119                                         return System.Threading.Thread.CurrentPrincipal.Identity.Name;
120                         }
121                 }
122                 
123                 public static string [] GetRolesForUser (string username)
124                 {
125                         return Provider.GetRolesForUser (username);
126                 }
127                 
128                 public static string [] GetUsersInRole (string roleName)
129                 {
130                         return Provider.GetUsersInRole (roleName);
131                 }
132                 
133                 public static bool IsUserInRole (string roleName)
134                 {
135                         return IsUserInRole (CurrentUser, roleName);
136                 }
137                 
138                 public static bool IsUserInRole (string username, string roleName)
139                 {
140                         if (String.IsNullOrEmpty (username))
141                                 return false;
142                         return Provider.IsUserInRole (username, roleName);
143                 }
144                 
145                 public static void RemoveUserFromRole (string username, string roleName)
146                 {
147                         Provider.RemoveUsersFromRoles (new string[] {username}, new string[] {roleName});
148                 }
149                 
150                 public static void RemoveUserFromRoles (string username, string [] roleNames)
151                 {
152                         Provider.RemoveUsersFromRoles (new string[] {username}, roleNames);
153                 }
154                 
155                 public static void RemoveUsersFromRole (string [] usernames, string roleName)
156                 {
157                         Provider.RemoveUsersFromRoles (usernames, new string[] {roleName});
158                 }
159                 
160                 public static void RemoveUsersFromRoles (string [] usernames, string [] roleNames)
161                 {
162                         Provider.RemoveUsersFromRoles (usernames, roleNames);
163                 }
164                 
165                 public static bool RoleExists (string roleName)
166                 {
167                         return Provider.RoleExists (roleName);
168                 }
169                 
170                 public static string[] FindUsersInRole (string roleName, string usernameToMatch)
171                 {
172                         return Provider.FindUsersInRole (roleName, usernameToMatch);
173                 }
174                 
175                 public static string ApplicationName {
176                         get { return Provider.ApplicationName; }
177                         set { Provider.ApplicationName = value; }
178                 }
179                 
180                 public static bool CacheRolesInCookie {
181                         get { return config.CacheRolesInCookie; }
182                 }
183                 
184                 public static string CookieName {
185                         get { return config.CookieName; }
186                 }
187                 
188                 public static string CookiePath {
189                         get { return config.CookiePath; }
190                 }
191                 
192                 public static CookieProtection CookieProtectionValue {
193                         get { return config.CookieProtection; }
194                 }
195                 
196                 public static bool CookieRequireSSL {
197                         get { return config.CookieRequireSSL; }
198                 }
199                 
200                 public static bool CookieSlidingExpiration {
201                         get { return config.CookieSlidingExpiration; }
202                 }
203                 
204                 public static int CookieTimeout {
205                         get { return (int)config.CookieTimeout.TotalMinutes; }
206                 }
207
208                 public static bool CreatePersistentCookie {
209                         get { return config.CreatePersistentCookie; }
210                 }
211
212                 public static string Domain {
213                         get { return config.Domain; }
214                 }
215
216                 public static bool Enabled {
217                         get { return config.Enabled; }
218                         set { config.Enabled = value; }
219                 }
220
221                 public static int MaxCachedResults {
222                         get { return config.MaxCachedResults; }
223                 }
224                 
225                 public static RoleProvider Provider {
226                         get {
227                                 RoleProvider p = Providers [config.DefaultProvider];
228                                 if (p == null)
229                                         throw new ConfigurationErrorsException ("Default Role Provider could not be found: Cannot instantiate provider: '" + config.DefaultProvider + "'.");
230                                 return p;
231                         }
232                 }
233                 
234                 public static RoleProviderCollection Providers {
235                         get {
236                                 CheckEnabled ();
237                                 if (providersCollection == null) {
238                                         RoleProviderCollection providersCollectionTmp = new RoleProviderCollection ();
239                                         ProvidersHelper.InstantiateProviders (config.Providers, providersCollectionTmp, typeof (RoleProvider));
240                                         providersCollection = providersCollectionTmp;
241                                 }
242                                 return providersCollection;
243                         }
244                 }
245
246                 // private stuff
247                 static void CheckEnabled ()
248                 {
249                         if (!Enabled)
250                                 throw new ProviderException ("This feature is not enabled.  To enable it, add <roleManager enabled=\"true\"> to your configuration file.");
251                 }
252         }
253 }
254