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.
33 using System
.Configuration
.Provider
;
34 using System
.Web
.Configuration
;
35 using System
.Configuration
;
37 namespace System
.Web
.Security
{
39 public static class Roles
{
40 static RoleManagerSection config
;
41 static RoleProviderCollection providersCollection
;
45 config
= (RoleManagerSection
)WebConfigurationManager
.GetSection ("system.web/roleManager");
49 public static void AddUsersToRole (string [] usernames
, string roleName
)
51 Provider
.AddUsersToRoles (usernames
, new string[] {roleName}
);
54 public static void AddUsersToRoles (string [] usernames
, string [] roleNames
)
56 Provider
.AddUsersToRoles (usernames
, roleNames
);
59 public static void AddUserToRole (string username
, string roleName
)
61 Provider
.AddUsersToRoles (new string[] {username}
, new string[] {roleName}
);
64 public static void AddUserToRoles (string username
, string [] roleNames
)
66 Provider
.AddUsersToRoles (new string[] {username}
, roleNames
);
69 public static void CreateRole (string roleName
)
71 Provider
.CreateRole (roleName
);
74 public static void DeleteCookie ()
76 if (CacheRolesInCookie
) {
77 HttpContext context
= HttpContext
.Current
;
79 throw new HttpException ("Context is null.");
81 HttpResponse response
= context
.Response
;
83 throw new HttpException ("Response is null.");
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
);
94 public static bool DeleteRole (string roleName
)
96 return Provider
.DeleteRole (roleName
, true);
99 public static bool DeleteRole (string roleName
, bool throwOnPopulatedRole
)
101 return Provider
.DeleteRole (roleName
, throwOnPopulatedRole
);
104 public static string [] GetAllRoles ()
106 return Provider
.GetAllRoles ();
109 public static string [] GetRolesForUser ()
111 return Provider
.GetRolesForUser (CurrentUser
);
114 static string CurrentUser
{
116 if (HttpContext
.Current
!= null && HttpContext
.Current
.User
!= null)
117 return HttpContext
.Current
.User
.Identity
.Name
;
119 return System
.Threading
.Thread
.CurrentPrincipal
.Identity
.Name
;
123 public static string [] GetRolesForUser (string username
)
125 return Provider
.GetRolesForUser (username
);
128 public static string [] GetUsersInRole (string roleName
)
130 return Provider
.GetUsersInRole (roleName
);
133 public static bool IsUserInRole (string roleName
)
135 return IsUserInRole (CurrentUser
, roleName
);
138 public static bool IsUserInRole (string username
, string roleName
)
140 if (String
.IsNullOrEmpty (username
))
142 return Provider
.IsUserInRole (username
, roleName
);
145 public static void RemoveUserFromRole (string username
, string roleName
)
147 Provider
.RemoveUsersFromRoles (new string[] {username}
, new string[] {roleName}
);
150 public static void RemoveUserFromRoles (string username
, string [] roleNames
)
152 Provider
.RemoveUsersFromRoles (new string[] {username}
, roleNames
);
155 public static void RemoveUsersFromRole (string [] usernames
, string roleName
)
157 Provider
.RemoveUsersFromRoles (usernames
, new string[] {roleName}
);
160 public static void RemoveUsersFromRoles (string [] usernames
, string [] roleNames
)
162 Provider
.RemoveUsersFromRoles (usernames
, roleNames
);
165 public static bool RoleExists (string roleName
)
167 return Provider
.RoleExists (roleName
);
170 public static string[] FindUsersInRole (string roleName
, string usernameToMatch
)
172 return Provider
.FindUsersInRole (roleName
, usernameToMatch
);
175 public static string ApplicationName
{
176 get { return Provider.ApplicationName; }
177 set { Provider.ApplicationName = value; }
180 public static bool CacheRolesInCookie
{
181 get { return config.CacheRolesInCookie; }
184 public static string CookieName
{
185 get { return config.CookieName; }
188 public static string CookiePath
{
189 get { return config.CookiePath; }
192 public static CookieProtection CookieProtectionValue
{
193 get { return config.CookieProtection; }
196 public static bool CookieRequireSSL
{
197 get { return config.CookieRequireSSL; }
200 public static bool CookieSlidingExpiration
{
201 get { return config.CookieSlidingExpiration; }
204 public static int CookieTimeout
{
205 get { return (int)config.CookieTimeout.TotalMinutes; }
208 public static bool CreatePersistentCookie
{
209 get { return config.CreatePersistentCookie; }
212 public static string Domain
{
213 get { return config.Domain; }
216 public static bool Enabled
{
217 get { return config.Enabled; }
218 set { config.Enabled = value; }
221 public static int MaxCachedResults
{
222 get { return config.MaxCachedResults; }
225 public static RoleProvider Provider
{
227 RoleProvider p
= Providers
[config
.DefaultProvider
];
229 throw new ConfigurationErrorsException ("Default Role Provider could not be found: Cannot instantiate provider: '" + config
.DefaultProvider
+ "'.");
234 public static RoleProviderCollection Providers
{
237 if (providersCollection
== null) {
238 RoleProviderCollection providersCollectionTmp
= new RoleProviderCollection ();
239 ProvidersHelper
.InstantiateProviders (config
.Providers
, providersCollectionTmp
, typeof (RoleProvider
));
240 providersCollection
= providersCollectionTmp
;
242 return providersCollection
;
247 static void CheckEnabled ()
250 throw new ProviderException ("This feature is not enabled. To enable it, add <roleManager enabled=\"true\"> to your configuration file.");