DbDataPermission.cs 11 KB

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