Roles.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //
  2. // System.Web.Security.Roles
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Ben Maurer
  9. // Copyright (c) 2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System.Configuration.Provider;
  32. namespace System.Web.Security {
  33. [MonoTODO ("read infos from web.config")]
  34. public static class Roles {
  35. private static RoleProvider provider;
  36. private static bool cookie_cache_roles;
  37. private static string cookie_name;
  38. private static string cookie_path;
  39. private static CookieProtection cookie_protection;
  40. private static bool cookie_ssl;
  41. private static bool cookie_sliding;
  42. private static int cookie_timeout;
  43. private static bool cookie_persistent;
  44. private static string domain;
  45. private static int max_cached_result;
  46. static Roles ()
  47. {
  48. // default values (when not supplied in web.config)
  49. cookie_name = ".ASPXROLES";
  50. cookie_path = "/";
  51. cookie_protection = CookieProtection.All;
  52. cookie_sliding = true;
  53. cookie_timeout = 30;
  54. max_cached_result = 25;
  55. }
  56. public static void AddUsersToRole (string [] usernames, string rolename)
  57. {
  58. Provider.AddUsersToRoles (usernames, new string[] {rolename});
  59. }
  60. public static void AddUsersToRoles (string [] usernames, string [] rolenames)
  61. {
  62. Provider.AddUsersToRoles (usernames, rolenames);
  63. }
  64. public static void AddUserToRole (string username, string rolename)
  65. {
  66. Provider.AddUsersToRoles (new string[] {username}, new string[] {rolename});
  67. }
  68. public static void AddUserToRoles (string username, string [] rolenames)
  69. {
  70. Provider.AddUsersToRoles (new string[] {username}, rolenames);
  71. }
  72. public static void CreateRole (string rolename)
  73. {
  74. Provider.CreateRole (rolename);
  75. }
  76. [MonoTODO]
  77. public static void DeleteCookie ()
  78. {
  79. throw new NotImplementedException ();
  80. }
  81. public static bool DeleteRole (string rolename)
  82. {
  83. return Provider.DeleteRole (rolename, true);
  84. }
  85. public static bool DeleteRole (string rolename, bool throwOnPopulatedRole)
  86. {
  87. return Provider.DeleteRole (rolename, throwOnPopulatedRole);
  88. }
  89. public static string [] GetAllRoles ()
  90. {
  91. return Provider.GetAllRoles ();
  92. }
  93. public static string [] GetRolesForUser ()
  94. {
  95. return Provider.GetRolesForUser (CurrentUser);
  96. }
  97. static string CurrentUser {
  98. get {
  99. if (HttpContext.Current != null && HttpContext.Current.User != null)
  100. return HttpContext.Current.User.Identity.Name;
  101. else
  102. return System.Threading.Thread.CurrentPrincipal.Identity.Name;
  103. }
  104. }
  105. public static string [] GetRolesForUser (string username)
  106. {
  107. return Provider.GetRolesForUser (username);
  108. }
  109. public static string [] GetUsersInRole (string rolename)
  110. {
  111. return Provider.GetUsersInRole (rolename);
  112. }
  113. public static bool IsUserInRole (string rolename)
  114. {
  115. return Provider.IsUserInRole (CurrentUser, rolename);
  116. }
  117. public static bool IsUserInRole (string username, string rolename)
  118. {
  119. return Provider.IsUserInRole (username, rolename);
  120. }
  121. public static void RemoveUserFromRole (string username, string rolename)
  122. {
  123. Provider.RemoveUsersFromRoles (new string[] {username}, new string[] {rolename});
  124. }
  125. public static void RemoveUserFromRoles (string username, string [] rolenames)
  126. {
  127. Provider.RemoveUsersFromRoles (new string[] {username}, rolenames);
  128. }
  129. public static void RemoveUsersFromRole (string [] usernames, string rolename)
  130. {
  131. Provider.RemoveUsersFromRoles (usernames, new string[] {rolename});
  132. }
  133. public static void RemoveUsersFromRoles (string [] usernames, string [] rolenames)
  134. {
  135. Provider.RemoveUsersFromRoles (usernames, rolenames);
  136. }
  137. public static bool RoleExists (string rolename)
  138. {
  139. return Provider.RoleExists (rolename);
  140. }
  141. public static string[] FindUsersInRole (string rolename, string usernameToMatch)
  142. {
  143. return Provider.FindUsersInRole (rolename, usernameToMatch);
  144. }
  145. public static string ApplicationName {
  146. get { return Provider.ApplicationName; }
  147. set { Provider.ApplicationName = value; }
  148. }
  149. public static bool CacheRolesInCookie {
  150. get { return cookie_cache_roles; }
  151. }
  152. public static string CookieName {
  153. get { return cookie_name; }
  154. }
  155. public static string CookiePath {
  156. get { return cookie_path; }
  157. }
  158. public static CookieProtection CookieProtectionValue {
  159. get { return cookie_protection; }
  160. }
  161. public static bool CookieRequireSSL {
  162. get { return cookie_ssl; }
  163. }
  164. public static bool CookieSlidingExpiration {
  165. get { return cookie_sliding; }
  166. }
  167. public static int CookieTimeout {
  168. get { return cookie_timeout; }
  169. }
  170. public static bool CreatePersistentCookie {
  171. get { return cookie_persistent; }
  172. }
  173. public static string Domain {
  174. get { return domain; }
  175. }
  176. public static bool Enabled {
  177. get { return (provider != null); }
  178. }
  179. public static int MaxCachedResults {
  180. get { return max_cached_result; }
  181. }
  182. [MonoTODO]
  183. public static RoleProvider Provider {
  184. get {
  185. CheckProvider ();
  186. throw new NotImplementedException ();
  187. }
  188. }
  189. [MonoTODO]
  190. public static RoleProviderCollection Providers {
  191. get {
  192. CheckProvider ();
  193. throw new NotImplementedException ();
  194. }
  195. }
  196. // private stuff
  197. private static void CheckProvider ()
  198. {
  199. if (!Enabled)
  200. throw new ProviderException ();
  201. }
  202. }
  203. }
  204. #endif