2 // System.Web.Security.Roles
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Chris Toshok <toshok@ximian.com>
10 // Copyright (c) 2005,2006 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 using System.Configuration.Provider;
35 using System.Web.Configuration;
36 using System.Configuration;
38 namespace System.Web.Security {
40 public static class Roles {
42 const string Roles_cookie_protection = "Roles.cookie_protection";
43 private static RoleManagerSection config {
45 return (RoleManagerSection) WebConfigurationManager.GetSection ("system.web/roleManager");
49 const string Roles_providersCollection = "Roles.providersCollection";
50 static RoleProviderCollection providersCollection {
52 return (RoleProviderCollection)AppDomain.CurrentDomain.GetData (Roles_providersCollection);
56 AppDomain.CurrentDomain.SetData (Roles_providersCollection, value);
60 private static RoleManagerSection config;
61 static RoleProviderCollection providersCollection;
65 config = (RoleManagerSection)WebConfigurationManager.GetSection ("system.web/roleManager");
70 public static void AddUsersToRole (string [] usernames, string rolename)
72 Provider.AddUsersToRoles (usernames, new string[] {rolename});
75 public static void AddUsersToRoles (string [] usernames, string [] rolenames)
77 Provider.AddUsersToRoles (usernames, rolenames);
80 public static void AddUserToRole (string username, string rolename)
82 Provider.AddUsersToRoles (new string[] {username}, new string[] {rolename});
85 public static void AddUserToRoles (string username, string [] rolenames)
87 Provider.AddUsersToRoles (new string[] {username}, rolenames);
90 public static void CreateRole (string rolename)
92 Provider.CreateRole (rolename);
95 public static void DeleteCookie ()
97 if (CacheRolesInCookie) {
98 HttpContext context = HttpContext.Current;
100 throw new HttpException ("Context is null.");
102 HttpResponse response = context.Response;
103 if (response == null)
104 throw new HttpException ("Response is null.");
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);
115 public static bool DeleteRole (string rolename)
117 return Provider.DeleteRole (rolename, true);
120 public static bool DeleteRole (string rolename, bool throwOnPopulatedRole)
122 return Provider.DeleteRole (rolename, throwOnPopulatedRole);
125 public static string [] GetAllRoles ()
127 return Provider.GetAllRoles ();
130 public static string [] GetRolesForUser ()
132 return Provider.GetRolesForUser (CurrentUser);
135 static string CurrentUser {
137 if (HttpContext.Current != null && HttpContext.Current.User != null)
138 return HttpContext.Current.User.Identity.Name;
140 return System.Threading.Thread.CurrentPrincipal.Identity.Name;
144 public static string [] GetRolesForUser (string username)
146 return Provider.GetRolesForUser (username);
149 public static string [] GetUsersInRole (string rolename)
151 return Provider.GetUsersInRole (rolename);
154 public static bool IsUserInRole (string rolename)
156 return Provider.IsUserInRole (CurrentUser, rolename);
159 public static bool IsUserInRole (string username, string rolename)
161 return Provider.IsUserInRole (username, rolename);
164 public static void RemoveUserFromRole (string username, string rolename)
166 Provider.RemoveUsersFromRoles (new string[] {username}, new string[] {rolename});
169 public static void RemoveUserFromRoles (string username, string [] rolenames)
171 Provider.RemoveUsersFromRoles (new string[] {username}, rolenames);
174 public static void RemoveUsersFromRole (string [] usernames, string rolename)
176 Provider.RemoveUsersFromRoles (usernames, new string[] {rolename});
179 public static void RemoveUsersFromRoles (string [] usernames, string [] rolenames)
181 Provider.RemoveUsersFromRoles (usernames, rolenames);
184 public static bool RoleExists (string rolename)
186 return Provider.RoleExists (rolename);
189 public static string[] FindUsersInRole (string rolename, string usernameToMatch)
191 return Provider.FindUsersInRole (rolename, usernameToMatch);
194 public static string ApplicationName {
195 get { return Provider.ApplicationName; }
196 set { Provider.ApplicationName = value; }
199 public static bool CacheRolesInCookie {
200 get { return config.CacheRolesInCookie; }
203 public static string CookieName {
204 get { return config.CookieName; }
207 public static string CookiePath {
208 get { return config.CookiePath; }
211 public static CookieProtection CookieProtectionValue {
212 get { return config.CookieProtection; }
215 public static bool CookieRequireSSL {
216 get { return config.CookieRequireSSL; }
219 public static bool CookieSlidingExpiration {
220 get { return config.CookieSlidingExpiration; }
223 public static int CookieTimeout {
224 get { return (int)config.CookieTimeout.TotalMinutes; }
227 public static bool CreatePersistentCookie {
228 get { return config.CreatePersistentCookie; }
231 public static string Domain {
232 get { return config.Domain; }
235 public static bool Enabled {
236 get { return config.Enabled; }
239 public static int MaxCachedResults {
240 get { return config.MaxCachedResults; }
243 public static RoleProvider Provider {
245 RoleProvider p = Providers [config.DefaultProvider];
247 throw new ConfigurationErrorsException ("Default Role Provider could not be found: Cannot instantiate provider: '" + config.DefaultProvider + "'.");
252 public static RoleProviderCollection Providers {
255 if (providersCollection == null) {
256 RoleProviderCollection providersCollectionTmp = new RoleProviderCollection ();
257 ProvidersHelper.InstantiateProviders (config.Providers, providersCollectionTmp, typeof (RoleProvider));
258 providersCollection = providersCollectionTmp;
260 return providersCollection;
265 private static void CheckEnabled ()
268 throw new ProviderException ("This feature is not enabled. To enable it, add <roleManager enabled=\"true\"> to your configuration file.");