DbDataPermission.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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) permission._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. if (permissionAttribute.ConnectionString.Length > 0) {
  74. Add (permissionAttribute.ConnectionString, permissionAttribute.KeyRestrictions,
  75. permissionAttribute.KeyRestrictionBehavior);
  76. }
  77. }
  78. }
  79. protected DBDataPermission (PermissionState state)
  80. {
  81. this.state = PermissionHelper.CheckPermissionState (state, true);
  82. _connections = new Hashtable ();
  83. }
  84. #if NET_2_0
  85. [Obsolete ("use DBDataPermission (PermissionState.None)", true)]
  86. protected
  87. #else
  88. public
  89. #endif
  90. DBDataPermission (PermissionState state, bool allowBlankPassword)
  91. : this (state)
  92. {
  93. this.allowBlankPassword = allowBlankPassword;
  94. }
  95. #endregion // Constructors
  96. #region Properties
  97. public bool AllowBlankPassword {
  98. get { return allowBlankPassword; }
  99. set { allowBlankPassword = value; }
  100. }
  101. #endregion // Properties
  102. #region Methods
  103. public virtual void Add (string connectionString, string restrictions, KeyRestrictionBehavior behavior)
  104. {
  105. state = PermissionState.None;
  106. _connections [connectionString] = new object [2] { restrictions, behavior };
  107. }
  108. protected void Clear ()
  109. {
  110. _connections.Clear ();
  111. }
  112. public override IPermission Copy ()
  113. {
  114. DBDataPermission dbdp = CreateInstance ();
  115. dbdp.allowBlankPassword = this.allowBlankPassword;
  116. dbdp._connections = (Hashtable) this._connections.Clone ();
  117. return dbdp;
  118. }
  119. protected virtual DBDataPermission CreateInstance ()
  120. {
  121. return (DBDataPermission) Activator.CreateInstance (this.GetType (), new object [1] { PermissionState.None });
  122. }
  123. public override void FromXml (SecurityElement securityElement)
  124. {
  125. PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
  126. // Note: we do not (yet) care about the return value
  127. // as we only accept version 1 (min/max values)
  128. state = (PermissionHelper.IsUnrestricted (securityElement) ?
  129. PermissionState.Unrestricted : PermissionState.None);
  130. allowBlankPassword = false;
  131. string blank = securityElement.Attribute ("AllowBlankPassword");
  132. if (blank != null) {
  133. #if NET_2_0
  134. // avoid possible exceptions with Fx 2.0
  135. if (!Boolean.TryParse (blank, out allowBlankPassword))
  136. allowBlankPassword = false;
  137. #else
  138. try {
  139. allowBlankPassword = Boolean.Parse (blank);
  140. }
  141. catch {
  142. allowBlankPassword = false;
  143. }
  144. #endif
  145. }
  146. if (securityElement.Children != null) {
  147. foreach (SecurityElement child in securityElement.Children) {
  148. string connect = child.Attribute ("ConnectionString");
  149. string restricts = child.Attribute ("KeyRestrictions");
  150. KeyRestrictionBehavior behavior = (KeyRestrictionBehavior) Enum.Parse (
  151. typeof (KeyRestrictionBehavior), child.Attribute ("KeyRestrictionBehavior"));
  152. if ((connect != null) && (connect.Length > 0))
  153. Add (connect, restricts, behavior);
  154. }
  155. }
  156. }
  157. [MonoTODO ("restrictions not completely implemented - nor documented")]
  158. public override IPermission Intersect (IPermission target)
  159. {
  160. DBDataPermission dbdp = Cast (target);
  161. if (dbdp == null)
  162. return null;
  163. if (IsUnrestricted ()) {
  164. if (dbdp.IsUnrestricted ()) {
  165. DBDataPermission u = CreateInstance ();
  166. u.state = PermissionState.Unrestricted;
  167. return u;
  168. }
  169. return dbdp.Copy ();
  170. }
  171. if (dbdp.IsUnrestricted ())
  172. return Copy ();
  173. if (IsEmpty () || dbdp.IsEmpty ())
  174. return null;
  175. DBDataPermission p = CreateInstance ();
  176. p.allowBlankPassword = (allowBlankPassword && dbdp.allowBlankPassword);
  177. foreach (DictionaryEntry de in _connections) {
  178. object o = dbdp._connections [de.Key];
  179. if (o != null)
  180. p._connections.Add (de.Key, de.Value);
  181. }
  182. return (p._connections.Count > 0) ? p : null;
  183. }
  184. [MonoTODO ("restrictions not completely implemented - nor documented")]
  185. public override bool IsSubsetOf (IPermission target)
  186. {
  187. DBDataPermission dbdp = Cast (target);
  188. if (dbdp == null)
  189. return IsEmpty ();
  190. if (dbdp.IsUnrestricted ())
  191. return true;
  192. if (IsUnrestricted ())
  193. return dbdp.IsUnrestricted ();
  194. if (allowBlankPassword && !dbdp.allowBlankPassword)
  195. return false;
  196. if (_connections.Count > dbdp._connections.Count)
  197. return false;
  198. foreach (DictionaryEntry de in _connections) {
  199. object o = dbdp._connections [de.Key];
  200. if (o == null)
  201. return false;
  202. // FIXME: this is a subset of what is required
  203. // it seems that we must process both the connect string
  204. // and the restrictions - but this has other effects :-/
  205. }
  206. return true;
  207. }
  208. public bool IsUnrestricted ()
  209. {
  210. return (state == PermissionState.Unrestricted);
  211. }
  212. public override SecurityElement ToXml ()
  213. {
  214. SecurityElement se = PermissionHelper.Element (this.GetType (), version);
  215. if (IsUnrestricted ()) {
  216. se.AddAttribute ("Unrestricted", "true");
  217. }
  218. else {
  219. // attribute is present for both True and False
  220. se.AddAttribute ("AllowBlankPassword", allowBlankPassword.ToString ());
  221. foreach (DictionaryEntry de in _connections) {
  222. SecurityElement child = new SecurityElement ("add");
  223. child.AddAttribute ("ConnectionString", (string) de.Key);
  224. object[] restrictionsInfo = (object[]) de.Value;
  225. child.AddAttribute ("KeyRestrictions", (string) restrictionsInfo [0]);
  226. KeyRestrictionBehavior krb = (KeyRestrictionBehavior) restrictionsInfo [1];
  227. child.AddAttribute ("KeyRestrictionBehavior", krb.ToString ());
  228. se.AddChild (child);
  229. }
  230. }
  231. return se;
  232. }
  233. [MonoTODO ("restrictions not completely implemented - nor documented")]
  234. public override IPermission Union (IPermission target)
  235. {
  236. DBDataPermission dbdp = Cast (target);
  237. if (dbdp == null)
  238. return Copy ();
  239. if (IsEmpty () && dbdp.IsEmpty ())
  240. return Copy ();
  241. DBDataPermission p = CreateInstance ();
  242. if (IsUnrestricted () || dbdp.IsUnrestricted ()) {
  243. p.state = PermissionState.Unrestricted;
  244. }
  245. else {
  246. p.allowBlankPassword = (allowBlankPassword || dbdp.allowBlankPassword);
  247. p._connections = new Hashtable (_connections.Count + dbdp._connections.Count);
  248. foreach (DictionaryEntry de in _connections)
  249. p._connections.Add (de.Key, de.Value);
  250. // don't duplicate
  251. foreach (DictionaryEntry de in dbdp._connections)
  252. p._connections [de.Key] = de.Value;
  253. }
  254. return p;
  255. }
  256. // helpers
  257. private bool IsEmpty ()
  258. {
  259. return ((state != PermissionState.Unrestricted) && (_connections.Count == 0));
  260. }
  261. private DBDataPermission Cast (IPermission target)
  262. {
  263. if (target == null)
  264. return null;
  265. DBDataPermission dbdp = (target as DBDataPermission);
  266. if (dbdp == null) {
  267. PermissionHelper.ThrowInvalidPermission (target, this.GetType ());
  268. }
  269. return dbdp;
  270. }
  271. #endregion // Methods
  272. }
  273. }