RolePrincipal.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. readonly string providerName;
  43. public RolePrincipal (IIdentity identity)
  44. {
  45. if (identity == null)
  46. throw new ArgumentNullException ("identity");
  47. this.identity = identity;
  48. }
  49. [MonoTODO]
  50. public RolePrincipal (IIdentity identity, string encryptedTicket)
  51. : this (identity)
  52. {
  53. throw new NotImplementedException ();
  54. }
  55. public RolePrincipal (string providerName, IIdentity identity)
  56. : this (identity)
  57. {
  58. if (providerName == null)
  59. throw new ArgumentNullException ("providerName");
  60. this.providerName = providerName;
  61. }
  62. [MonoTODO]
  63. public RolePrincipal (string providerName, IIdentity identity, string encryptedTicket)
  64. : this (identity)
  65. {
  66. if (providerName == null)
  67. throw new ArgumentNullException ("providerName");
  68. this.providerName = providerName;
  69. throw new NotImplementedException ();
  70. }
  71. public string [] GetRoles ()
  72. {
  73. if (!identity.IsAuthenticated)
  74. return new string[0];
  75. if (cachedRoles == null) {
  76. cachedArray = Provider.GetRolesForUser (identity.Name);
  77. cachedRoles = new HybridDictionary (true);
  78. foreach (string r in cachedArray)
  79. cachedRoles.Add(r, r);
  80. }
  81. return cachedArray;
  82. }
  83. public bool IsInRole (string role)
  84. {
  85. if (!identity.IsAuthenticated)
  86. return false;
  87. GetRoles ();
  88. return cachedRoles[role] != null;
  89. }
  90. [MonoTODO]
  91. public string ToEncryptedTicket ()
  92. {
  93. throw new NotImplementedException ();
  94. }
  95. public bool CachedListChanged {
  96. get { return listChanged; }
  97. }
  98. [MonoTODO]
  99. public string CookiePath {
  100. get { throw new NotImplementedException (); }
  101. }
  102. [MonoTODO]
  103. public bool Expired {
  104. get { throw new NotImplementedException (); }
  105. }
  106. [MonoTODO]
  107. public DateTime ExpireDate {
  108. get { throw new NotImplementedException (); }
  109. }
  110. public IIdentity Identity {
  111. get { return identity; }
  112. }
  113. public bool IsRoleListCached {
  114. get { return cachedRoles != null; }
  115. }
  116. [MonoTODO]
  117. public DateTime IssueDate {
  118. get { throw new NotImplementedException (); }
  119. }
  120. public string ProviderName {
  121. get { return String.IsNullOrEmpty(providerName) ? Provider.Name : providerName; }
  122. }
  123. public int Version {
  124. get { return 1; }
  125. }
  126. RoleProvider Provider {
  127. get {
  128. if (String.IsNullOrEmpty(providerName))
  129. return Roles.Provider;
  130. return Roles.Providers [providerName];
  131. }
  132. }
  133. public void SetDirty ()
  134. {
  135. listChanged = true;
  136. cachedRoles = null;
  137. cachedArray = null;
  138. }
  139. }
  140. }
  141. #endif