DbDataPermission.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // System.Data.Common.DbDataPermission.cs
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // (C) Ximian, Inc
  10. // Copyright (C) Tim Coleman, 2002-2003
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Collections;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. namespace System.Data.Common {
  36. [Serializable]
  37. public abstract class DBDataPermission : CodeAccessPermission, IUnrestrictedPermission {
  38. #region Fields
  39. private const int version = 1;
  40. private bool allowBlankPassword;
  41. private PermissionState state;
  42. private Hashtable _connections;
  43. #endregion // Fields
  44. #region Constructors
  45. #if NET_2_0
  46. [Obsolete ("use DBDataPermission (PermissionState.None)", true)]
  47. #endif
  48. protected DBDataPermission ()
  49. : this (PermissionState.None)
  50. {
  51. }
  52. protected DBDataPermission (DBDataPermission permission)
  53. {
  54. if (permission == null)
  55. throw new ArgumentNullException ("permission");
  56. state = permission.state;
  57. if (state != PermissionState.Unrestricted) {
  58. allowBlankPassword = permission.allowBlankPassword;
  59. _connections = (Hashtable) _connections.Clone ();
  60. }
  61. }
  62. protected DBDataPermission (DBDataPermissionAttribute permissionAttribute)
  63. {
  64. if (permissionAttribute == null)
  65. throw new ArgumentNullException ("permissionAttribute");
  66. _connections = new Hashtable ();
  67. if (permissionAttribute.Unrestricted) {
  68. state = PermissionState.Unrestricted;
  69. }
  70. else {
  71. state = PermissionState.None;
  72. allowBlankPassword = permissionAttribute.AllowBlankPassword;
  73. Add (permissionAttribute.ConnectionString, permissionAttribute.KeyRestrictions,
  74. permissionAttribute.KeyRestrictionBehavior);
  75. }
  76. }
  77. #if NET_2_0
  78. [MonoTODO]
  79. protected DBDataPermission (DbConnectionOptions connectionOptions)
  80. : this (PermissionState.None)
  81. {
  82. // ignore null (i.e. no ArgumentNullException)
  83. if (connectionOptions != null) {
  84. throw new NotImplementedException ();
  85. }
  86. }
  87. #endif
  88. protected DBDataPermission (PermissionState state)
  89. {
  90. this.state = PermissionHelper.CheckPermissionState (state, true);
  91. _connections = new Hashtable ();
  92. }
  93. #if NET_2_0
  94. [Obsolete ("use DBDataPermission (PermissionState.None)", true)]
  95. #endif
  96. public DBDataPermission (PermissionState state, bool allowBlankPassword)
  97. : this (state)
  98. {
  99. this.allowBlankPassword = allowBlankPassword;
  100. }
  101. #endregion // Constructors
  102. #region Properties
  103. public bool AllowBlankPassword {
  104. get { return allowBlankPassword; }
  105. set { allowBlankPassword = value; }
  106. }
  107. #endregion // Properties
  108. #region Methods
  109. #if NET_2_0
  110. public virtual void Add (string connectionString, string restrictions, KeyRestrictionBehavior behavior)
  111. {
  112. state = PermissionState.None;
  113. AddConnectionString (connectionString, restrictions, behavior, null, false);
  114. }
  115. [MonoTODO ("synonyms and useFirstKeyValue aren't supported")]
  116. protected virtual void AddConnectionString (string connectionString, string restrictions,
  117. KeyRestrictionBehavior behavior, Hashtable synonyms, bool useFirstKeyValue)
  118. {
  119. _connections [connectionString] = new object [2] { restrictions, connectionString };
  120. }
  121. #elif NET_1_1
  122. public virtual void Add (string connectionString, string restrictions, KeyRestrictionBehavior behavior)
  123. {
  124. state = PermissionState.None;
  125. _connections [connectionString] = new object [2] { restrictions, connectionString };
  126. }
  127. #endif
  128. protected void Clear ()
  129. {
  130. _connections.Clear ();
  131. }
  132. public override IPermission Copy ()
  133. {
  134. // call protected constructor
  135. return (IPermission) Activator.CreateInstance (this.GetType (), new object [1] { this });
  136. }
  137. protected virtual DBDataPermission CreateInstance ()
  138. {
  139. return (DBDataPermission) Activator.CreateInstance (this.GetType ());
  140. }
  141. [MonoTODO]
  142. public override void FromXml (SecurityElement securityElement)
  143. {
  144. }
  145. [MonoTODO]
  146. public override IPermission Intersect (IPermission target)
  147. {
  148. throw new NotImplementedException ();
  149. }
  150. [MonoTODO]
  151. public override bool IsSubsetOf (IPermission target)
  152. {
  153. throw new NotImplementedException ();
  154. }
  155. public bool IsUnrestricted ()
  156. {
  157. return (state == PermissionState.Unrestricted);
  158. }
  159. #if NET_2_0
  160. [MonoTODO ("DO NOT IMPLEMENT - will be removed")]
  161. [Obsolete ("DO NOT IMPLEMENT - will be removed")]
  162. protected void SetConnectionString (DbConnectionOptions constr)
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. [MonoTODO ("DO NOT IMPLEMENT - will be removed")]
  167. [Obsolete ("DO NOT IMPLEMENT - will be removed")]
  168. public virtual void SetRestriction (string connectionString, string restrictions, KeyRestrictionBehavior behavior)
  169. {
  170. throw new NotImplementedException ();
  171. }
  172. #endif
  173. public override SecurityElement ToXml ()
  174. {
  175. SecurityElement se = PermissionHelper.Element (this.GetType (), version);
  176. if (IsUnrestricted ()) {
  177. se.AddAttribute ("Unrestricted", "true");
  178. }
  179. else {
  180. // attribute is present for both True and False
  181. se.AddAttribute ("AllowBlankPassword", allowBlankPassword.ToString ());
  182. foreach (DictionaryEntry de in _connections) {
  183. SecurityElement child = new SecurityElement ("add");
  184. child.AddAttribute ("ConnectionString", (string) de.Key);
  185. object[] restrictionsInfo = (object[]) de.Value;
  186. child.AddAttribute ("KeyRestrictions", (string) restrictionsInfo [0]);
  187. KeyRestrictionBehavior krb = (KeyRestrictionBehavior) restrictionsInfo [1];
  188. child.AddAttribute ("KeyRestrictionBehavior", krb.ToString ());
  189. se.AddChild (child);
  190. }
  191. }
  192. return se;
  193. }
  194. [MonoTODO]
  195. public override IPermission Union (IPermission target)
  196. {
  197. throw new NotImplementedException ();
  198. }
  199. #endregion // Methods
  200. }
  201. }