Roles.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #if TARGET_J2EE
  37. const string Roles_cookie_protection = "Roles.cookie_protection";
  38. private static CookieProtection cookie_protection {
  39. get {
  40. object o = AppDomain.CurrentDomain.GetData (Roles_cookie_protection);
  41. if (o == null) {
  42. lock (AppDomain.CurrentDomain) {
  43. o = AppDomain.CurrentDomain.GetData (Roles_cookie_protection);
  44. if (o == null) {
  45. cookie_protection = CookieProtection.All;
  46. return cookie_protection;
  47. }
  48. }
  49. }
  50. return (CookieProtection) o;
  51. }
  52. set {
  53. AppDomain.CurrentDomain.SetData (Roles_cookie_protection, value);
  54. }
  55. }
  56. private static RoleManagerSection config {
  57. get {
  58. return (RoleManagerSection) WebConfigurationManager.GetSection ("system.web/roleManager");
  59. }
  60. }
  61. const string Roles_providersCollection = "Roles.providersCollection";
  62. static RoleProviderCollection providersCollection {
  63. get {
  64. return (RoleProviderCollection)AppDomain.CurrentDomain.GetData (Roles_providersCollection);
  65. }
  66. set {
  67. AppDomain.CurrentDomain.SetData (Roles_providersCollection, value);
  68. }
  69. }
  70. #else
  71. private static CookieProtection cookie_protection;
  72. private static RoleManagerSection config;
  73. static RoleProviderCollection providersCollection;
  74. static Roles ()
  75. {
  76. // default values (when not supplied in web.config)
  77. cookie_protection = CookieProtection.All;
  78. config = (RoleManagerSection)WebConfigurationManager.GetSection ("system.web/roleManager");
  79. }
  80. #endif
  81. public static void AddUsersToRole (string [] usernames, string rolename)
  82. {
  83. Provider.AddUsersToRoles (usernames, new string[] {rolename});
  84. }
  85. public static void AddUsersToRoles (string [] usernames, string [] rolenames)
  86. {
  87. Provider.AddUsersToRoles (usernames, rolenames);
  88. }
  89. public static void AddUserToRole (string username, string rolename)
  90. {
  91. Provider.AddUsersToRoles (new string[] {username}, new string[] {rolename});
  92. }
  93. public static void AddUserToRoles (string username, string [] rolenames)
  94. {
  95. Provider.AddUsersToRoles (new string[] {username}, rolenames);
  96. }
  97. public static void CreateRole (string rolename)
  98. {
  99. Provider.CreateRole (rolename);
  100. }
  101. [MonoTODO ("Not implemented")]
  102. public static void DeleteCookie ()
  103. {
  104. throw new NotImplementedException ();
  105. }
  106. public static bool DeleteRole (string rolename)
  107. {
  108. return Provider.DeleteRole (rolename, true);
  109. }
  110. public static bool DeleteRole (string rolename, bool throwOnPopulatedRole)
  111. {
  112. return Provider.DeleteRole (rolename, throwOnPopulatedRole);
  113. }
  114. public static string [] GetAllRoles ()
  115. {
  116. return Provider.GetAllRoles ();
  117. }
  118. public static string [] GetRolesForUser ()
  119. {
  120. return Provider.GetRolesForUser (CurrentUser);
  121. }
  122. static string CurrentUser {
  123. get {
  124. if (HttpContext.Current != null && HttpContext.Current.User != null)
  125. return HttpContext.Current.User.Identity.Name;
  126. else
  127. return System.Threading.Thread.CurrentPrincipal.Identity.Name;
  128. }
  129. }
  130. public static string [] GetRolesForUser (string username)
  131. {
  132. return Provider.GetRolesForUser (username);
  133. }
  134. public static string [] GetUsersInRole (string rolename)
  135. {
  136. return Provider.GetUsersInRole (rolename);
  137. }
  138. public static bool IsUserInRole (string rolename)
  139. {
  140. return Provider.IsUserInRole (CurrentUser, rolename);
  141. }
  142. public static bool IsUserInRole (string username, string rolename)
  143. {
  144. return Provider.IsUserInRole (username, rolename);
  145. }
  146. public static void RemoveUserFromRole (string username, string rolename)
  147. {
  148. Provider.RemoveUsersFromRoles (new string[] {username}, new string[] {rolename});
  149. }
  150. public static void RemoveUserFromRoles (string username, string [] rolenames)
  151. {
  152. Provider.RemoveUsersFromRoles (new string[] {username}, rolenames);
  153. }
  154. public static void RemoveUsersFromRole (string [] usernames, string rolename)
  155. {
  156. Provider.RemoveUsersFromRoles (usernames, new string[] {rolename});
  157. }
  158. public static void RemoveUsersFromRoles (string [] usernames, string [] rolenames)
  159. {
  160. Provider.RemoveUsersFromRoles (usernames, rolenames);
  161. }
  162. public static bool RoleExists (string rolename)
  163. {
  164. return Provider.RoleExists (rolename);
  165. }
  166. public static string[] FindUsersInRole (string rolename, string usernameToMatch)
  167. {
  168. return Provider.FindUsersInRole (rolename, usernameToMatch);
  169. }
  170. public static string ApplicationName {
  171. get { return Provider.ApplicationName; }
  172. set { Provider.ApplicationName = value; }
  173. }
  174. public static bool CacheRolesInCookie {
  175. get { return config.CacheRolesInCookie; }
  176. }
  177. public static string CookieName {
  178. get { return config.CookieName; }
  179. }
  180. public static string CookiePath {
  181. get { return config.CookiePath; }
  182. }
  183. [MonoTODO ("read infos from web.config")]
  184. public static CookieProtection CookieProtectionValue {
  185. get { return cookie_protection; }
  186. }
  187. public static bool CookieRequireSSL {
  188. get { return config.CookieRequireSSL; }
  189. }
  190. public static bool CookieSlidingExpiration {
  191. get { return config.CookieSlidingExpiration; }
  192. }
  193. public static int CookieTimeout {
  194. get { return (int)config.CookieTimeout.TotalMinutes; }
  195. }
  196. public static bool CreatePersistentCookie {
  197. get { return config.CreatePersistentCookie; }
  198. }
  199. public static string Domain {
  200. get { return config.Domain; }
  201. }
  202. public static bool Enabled {
  203. get { return config.Enabled; }
  204. }
  205. public static int MaxCachedResults {
  206. get { return config.MaxCachedResults; }
  207. }
  208. public static RoleProvider Provider {
  209. get { return Providers[config.DefaultProvider]; }
  210. }
  211. public static RoleProviderCollection Providers {
  212. get {
  213. CheckEnabled ();
  214. if (providersCollection == null) {
  215. providersCollection = new RoleProviderCollection ();
  216. ProvidersHelper.InstantiateProviders (config.Providers, providersCollection, typeof (RoleProvider));
  217. }
  218. return providersCollection;
  219. }
  220. }
  221. // private stuff
  222. private static void CheckEnabled ()
  223. {
  224. if (!Enabled)
  225. throw new ProviderException ("This feature is not enabled. To enable it, add <roleManager enabled=\"true\"> to your configuration file.");
  226. }
  227. }
  228. }
  229. #endif