RolePrincipal.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // System.Web.Security.RolePrincipal
  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.Collections.Specialized;
  32. using System.Security.Permissions;
  33. using System.Security.Principal;
  34. namespace System.Web.Security {
  35. [Serializable]
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. public sealed class RolePrincipal : IPrincipal {
  38. private IIdentity identity;
  39. private bool listChanged;
  40. string[] cachedArray;
  41. private HybridDictionary cachedRoles;
  42. private RoleProvider provider;
  43. public RolePrincipal (IIdentity identity)
  44. {
  45. if (identity == null)
  46. throw new ArgumentNullException ("identity");
  47. this.identity = identity;
  48. this.provider = Roles.Provider;
  49. }
  50. [MonoTODO]
  51. public RolePrincipal (IIdentity identity, string encryptedTicket)
  52. : this (identity)
  53. {
  54. throw new NotImplementedException ();
  55. }
  56. public RolePrincipal (string providerName, IIdentity identity)
  57. : this (identity)
  58. {
  59. if (providerName == null)
  60. throw new ArgumentNullException ("providerName");
  61. this.provider = Roles.Providers[providerName];
  62. }
  63. [MonoTODO]
  64. public RolePrincipal (string providerName, IIdentity identity, string encryptedTicket)
  65. : this (identity)
  66. {
  67. if (providerName == null)
  68. throw new ArgumentNullException ("providerName");
  69. this.provider = Roles.Providers[providerName];
  70. throw new NotImplementedException ();
  71. }
  72. public string [] GetRoles ()
  73. {
  74. if (!identity.IsAuthenticated)
  75. return new string[0];
  76. if (cachedRoles == null) {
  77. cachedArray = provider.GetRolesForUser (identity.Name);
  78. cachedRoles = new HybridDictionary (true);
  79. foreach (string r in cachedArray)
  80. cachedRoles.Add(r, r);
  81. }
  82. return cachedArray;
  83. }
  84. public bool IsInRole (string role)
  85. {
  86. if (!identity.IsAuthenticated)
  87. return false;
  88. GetRoles ();
  89. return cachedRoles[role] != null;
  90. }
  91. [MonoTODO]
  92. public string ToEncryptedTicket ()
  93. {
  94. throw new NotImplementedException ();
  95. }
  96. public bool CachedListChanged {
  97. get { return listChanged; }
  98. }
  99. [MonoTODO]
  100. public string CookiePath {
  101. get { throw new NotImplementedException (); }
  102. }
  103. [MonoTODO]
  104. public bool Expired {
  105. get { throw new NotImplementedException (); }
  106. }
  107. [MonoTODO]
  108. public DateTime ExpireDate {
  109. get { throw new NotImplementedException (); }
  110. }
  111. public IIdentity Identity {
  112. get { return identity; }
  113. }
  114. public bool IsRoleListCached {
  115. get { return cachedRoles != null; }
  116. }
  117. [MonoTODO]
  118. public DateTime IssueDate {
  119. get { throw new NotImplementedException (); }
  120. }
  121. public string ProviderName {
  122. get { return provider.Name; }
  123. }
  124. public int Version {
  125. get { return 1; }
  126. }
  127. public void SetDirty ()
  128. {
  129. listChanged = true;
  130. cachedRoles = null;
  131. cachedArray = null;
  132. }
  133. }
  134. }
  135. #endif