Roles.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // System.Web.Security.Roles
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. // Chris Toshok <[email protected]>
  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. #if NET_2_0
  32. using System.Configuration.Provider;
  33. using System.Web.Configuration;
  34. namespace System.Web.Security {
  35. public static class Roles {
  36. private static CookieProtection cookie_protection;
  37. private static RoleManagerSection config;
  38. static RoleProviderCollection providersCollection;
  39. static Roles ()
  40. {
  41. // default values (when not supplied in web.config)
  42. cookie_protection = CookieProtection.All;
  43. config = (RoleManagerSection)WebConfigurationManager.GetSection ("system.web/roleManager");
  44. }
  45. public static void AddUsersToRole (string [] usernames, string rolename)
  46. {
  47. Provider.AddUsersToRoles (usernames, new string[] {rolename});
  48. }
  49. public static void AddUsersToRoles (string [] usernames, string [] rolenames)
  50. {
  51. Provider.AddUsersToRoles (usernames, rolenames);
  52. }
  53. public static void AddUserToRole (string username, string rolename)
  54. {
  55. Provider.AddUsersToRoles (new string[] {username}, new string[] {rolename});
  56. }
  57. public static void AddUserToRoles (string username, string [] rolenames)
  58. {
  59. Provider.AddUsersToRoles (new string[] {username}, rolenames);
  60. }
  61. public static void CreateRole (string rolename)
  62. {
  63. Provider.CreateRole (rolename);
  64. }
  65. [MonoTODO]
  66. public static void DeleteCookie ()
  67. {
  68. throw new NotImplementedException ();
  69. }
  70. public static bool DeleteRole (string rolename)
  71. {
  72. return Provider.DeleteRole (rolename, true);
  73. }
  74. public static bool DeleteRole (string rolename, bool throwOnPopulatedRole)
  75. {
  76. return Provider.DeleteRole (rolename, throwOnPopulatedRole);
  77. }
  78. public static string [] GetAllRoles ()
  79. {
  80. return Provider.GetAllRoles ();
  81. }
  82. public static string [] GetRolesForUser ()
  83. {
  84. return Provider.GetRolesForUser (CurrentUser);
  85. }
  86. static string CurrentUser {
  87. get {
  88. if (HttpContext.Current != null && HttpContext.Current.User != null)
  89. return HttpContext.Current.User.Identity.Name;
  90. else
  91. return System.Threading.Thread.CurrentPrincipal.Identity.Name;
  92. }
  93. }
  94. public static string [] GetRolesForUser (string username)
  95. {
  96. return Provider.GetRolesForUser (username);
  97. }
  98. public static string [] GetUsersInRole (string rolename)
  99. {
  100. return Provider.GetUsersInRole (rolename);
  101. }
  102. public static bool IsUserInRole (string rolename)
  103. {
  104. return Provider.IsUserInRole (CurrentUser, rolename);
  105. }
  106. public static bool IsUserInRole (string username, string rolename)
  107. {
  108. return Provider.IsUserInRole (username, rolename);
  109. }
  110. public static void RemoveUserFromRole (string username, string rolename)
  111. {
  112. Provider.RemoveUsersFromRoles (new string[] {username}, new string[] {rolename});
  113. }
  114. public static void RemoveUserFromRoles (string username, string [] rolenames)
  115. {
  116. Provider.RemoveUsersFromRoles (new string[] {username}, rolenames);
  117. }
  118. public static void RemoveUsersFromRole (string [] usernames, string rolename)
  119. {
  120. Provider.RemoveUsersFromRoles (usernames, new string[] {rolename});
  121. }
  122. public static void RemoveUsersFromRoles (string [] usernames, string [] rolenames)
  123. {
  124. Provider.RemoveUsersFromRoles (usernames, rolenames);
  125. }
  126. public static bool RoleExists (string rolename)
  127. {
  128. return Provider.RoleExists (rolename);
  129. }
  130. public static string[] FindUsersInRole (string rolename, string usernameToMatch)
  131. {
  132. return Provider.FindUsersInRole (rolename, usernameToMatch);
  133. }
  134. public static string ApplicationName {
  135. get { return Provider.ApplicationName; }
  136. set { Provider.ApplicationName = value; }
  137. }
  138. public static bool CacheRolesInCookie {
  139. get { return config.CacheRolesInCookie; }
  140. }
  141. public static string CookieName {
  142. get { return config.CookieName; }
  143. }
  144. public static string CookiePath {
  145. get { return config.CookiePath; }
  146. }
  147. [MonoTODO ("read infos from web.config")]
  148. public static CookieProtection CookieProtectionValue {
  149. get { return cookie_protection; }
  150. }
  151. public static bool CookieRequireSSL {
  152. get { return config.CookieRequireSSL; }
  153. }
  154. public static bool CookieSlidingExpiration {
  155. get { return config.CookieSlidingExpiration; }
  156. }
  157. public static int CookieTimeout {
  158. get { return (int)config.CookieTimeout.TotalMinutes; }
  159. }
  160. public static bool CreatePersistentCookie {
  161. get { return config.CreatePersistentCookie; }
  162. }
  163. public static string Domain {
  164. get { return config.Domain; }
  165. }
  166. public static bool Enabled {
  167. get { return config.Enabled; }
  168. }
  169. public static int MaxCachedResults {
  170. get { return config.MaxCachedResults; }
  171. }
  172. public static RoleProvider Provider {
  173. get { return Providers[config.DefaultProvider]; }
  174. }
  175. public static RoleProviderCollection Providers {
  176. get {
  177. CheckEnabled ();
  178. if (providersCollection == null) {
  179. providersCollection = new RoleProviderCollection ();
  180. ProvidersHelper.InstantiateProviders (config.Providers, providersCollection, typeof (RoleProvider));
  181. }
  182. return providersCollection;
  183. }
  184. }
  185. // private stuff
  186. private static void CheckEnabled ()
  187. {
  188. if (!Enabled)
  189. throw new ProviderException ("This feature is not enabled. To enable it, add <roleManager enabled=\"true\"> to your configuration file.");
  190. }
  191. }
  192. }
  193. #endif