PageRenderTime 5256ms CodeModel.GetById 58ms RepoModel.GetById 6ms app.codeStats 1ms

/mcs/class/referencesource/mscorlib/system/security/permissions/siteidentitypermission.cs

https://github.com/pruiz/mono
C# | 363 lines | 293 code | 27 blank | 43 comment | 106 complexity | 0c402d362b01d4df03fbda4f16501c2e MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // ==++==
  2. //
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // ==--==
  6. // SiteIdentityPermission.cs
  7. //
  8. // <OWNER>Microsoft</OWNER>
  9. //
  10. namespace System.Security.Permissions
  11. {
  12. using System;
  13. #if FEATURE_CAS_POLICY
  14. using SecurityElement = System.Security.SecurityElement;
  15. #endif // FEATURE_CAS_POLICY
  16. using SiteString = System.Security.Util.SiteString;
  17. using System.Text;
  18. using System.Collections;
  19. using System.Collections.Generic;
  20. using System.Globalization;
  21. using System.Runtime.Serialization;
  22. [System.Runtime.InteropServices.ComVisible(true)]
  23. [Serializable]
  24. sealed public class SiteIdentityPermission : CodeAccessPermission, IBuiltInPermission
  25. {
  26. //------------------------------------------------------
  27. //
  28. // PRIVATE STATE DATA
  29. //
  30. //------------------------------------------------------
  31. [OptionalField(VersionAdded = 2)]
  32. private bool m_unrestricted;
  33. [OptionalField(VersionAdded = 2)]
  34. private SiteString[] m_sites;
  35. #if FEATURE_REMOTING
  36. // This field will be populated only for non X-AD scenarios where we create a XML-ised string of the Permission
  37. [OptionalField(VersionAdded = 2)]
  38. private String m_serializedPermission;
  39. // This field is legacy info from v1.x and is never used in v2.0 and beyond: purely for serialization purposes
  40. private SiteString m_site;
  41. [OnDeserialized]
  42. private void OnDeserialized(StreamingContext ctx)
  43. {
  44. // v2.0 and beyond XML case
  45. if (m_serializedPermission != null)
  46. {
  47. FromXml(SecurityElement.FromString(m_serializedPermission));
  48. m_serializedPermission = null;
  49. }
  50. else if (m_site != null) //v1.x case where we read the m_site value
  51. {
  52. m_unrestricted = false;
  53. m_sites = new SiteString[1];
  54. m_sites[0] = m_site;
  55. m_site = null;
  56. }
  57. }
  58. [OnSerializing]
  59. private void OnSerializing(StreamingContext ctx)
  60. {
  61. if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
  62. {
  63. m_serializedPermission = ToXml().ToString(); //for the v2 and beyond case
  64. if (m_sites != null && m_sites.Length == 1) // for the v1.x case
  65. m_site = m_sites[0];
  66. }
  67. }
  68. [OnSerialized]
  69. private void OnSerialized(StreamingContext ctx)
  70. {
  71. if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
  72. {
  73. m_serializedPermission = null;
  74. m_site = null;
  75. }
  76. }
  77. #endif // FEATURE_REMOTING
  78. //------------------------------------------------------
  79. //
  80. // PUBLIC CONSTRUCTORS
  81. //
  82. //------------------------------------------------------
  83. public SiteIdentityPermission(PermissionState state)
  84. {
  85. if (state == PermissionState.Unrestricted)
  86. {
  87. m_unrestricted = true;
  88. }
  89. else if (state == PermissionState.None)
  90. {
  91. m_unrestricted = false;
  92. }
  93. else
  94. {
  95. throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
  96. }
  97. }
  98. public SiteIdentityPermission( String site )
  99. {
  100. Site = site;
  101. }
  102. //------------------------------------------------------
  103. //
  104. // PUBLIC ACCESSOR METHODS
  105. //
  106. //------------------------------------------------------
  107. public String Site
  108. {
  109. set
  110. {
  111. m_unrestricted = false;
  112. m_sites = new SiteString[1];
  113. m_sites[0] = new SiteString( value );
  114. }
  115. get
  116. {
  117. if(m_sites == null)
  118. return "";
  119. if(m_sites.Length == 1)
  120. return m_sites[0].ToString();
  121. throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
  122. }
  123. }
  124. //------------------------------------------------------
  125. //
  126. // PRIVATE AND PROTECTED HELPERS FOR ACCESSORS AND CONSTRUCTORS
  127. //
  128. //------------------------------------------------------
  129. //------------------------------------------------------
  130. //
  131. // CODEACCESSPERMISSION IMPLEMENTATION
  132. //
  133. //------------------------------------------------------
  134. //------------------------------------------------------
  135. //
  136. // IPERMISSION IMPLEMENTATION
  137. //
  138. //------------------------------------------------------
  139. public override IPermission Copy()
  140. {
  141. SiteIdentityPermission perm = new SiteIdentityPermission( PermissionState.None );
  142. perm.m_unrestricted = this.m_unrestricted;
  143. if (this.m_sites != null)
  144. {
  145. perm.m_sites = new SiteString[this.m_sites.Length];
  146. int n;
  147. for(n = 0; n < this.m_sites.Length; n++)
  148. perm.m_sites[n] = (SiteString)this.m_sites[n].Copy();
  149. }
  150. return perm;
  151. }
  152. public override bool IsSubsetOf(IPermission target)
  153. {
  154. if (target == null)
  155. {
  156. if(m_unrestricted)
  157. return false;
  158. if(m_sites == null)
  159. return true;
  160. if(m_sites.Length == 0)
  161. return true;
  162. return false;
  163. }
  164. SiteIdentityPermission that = target as SiteIdentityPermission;
  165. if(that == null)
  166. throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
  167. if(that.m_unrestricted)
  168. return true;
  169. if(m_unrestricted)
  170. return false;
  171. if(this.m_sites != null)
  172. {
  173. foreach(SiteString ssThis in this.m_sites)
  174. {
  175. bool bOK = false;
  176. if(that.m_sites != null)
  177. {
  178. foreach(SiteString ssThat in that.m_sites)
  179. {
  180. if(ssThis.IsSubsetOf(ssThat))
  181. {
  182. bOK = true;
  183. break;
  184. }
  185. }
  186. }
  187. if(!bOK)
  188. return false;
  189. }
  190. }
  191. return true;
  192. }
  193. public override IPermission Intersect(IPermission target)
  194. {
  195. if (target == null)
  196. return null;
  197. SiteIdentityPermission that = target as SiteIdentityPermission;
  198. if(that == null)
  199. throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
  200. if(this.m_unrestricted && that.m_unrestricted)
  201. {
  202. SiteIdentityPermission res = new SiteIdentityPermission(PermissionState.None);
  203. res.m_unrestricted = true;
  204. return res;
  205. }
  206. if(this.m_unrestricted)
  207. return that.Copy();
  208. if(that.m_unrestricted)
  209. return this.Copy();
  210. if(this.m_sites == null || that.m_sites == null || this.m_sites.Length == 0 || that.m_sites.Length == 0)
  211. return null;
  212. List<SiteString> alSites = new List<SiteString>();
  213. foreach(SiteString ssThis in this.m_sites)
  214. {
  215. foreach(SiteString ssThat in that.m_sites)
  216. {
  217. SiteString ssInt = (SiteString)ssThis.Intersect(ssThat);
  218. if(ssInt != null)
  219. alSites.Add(ssInt);
  220. }
  221. }
  222. if(alSites.Count == 0)
  223. return null;
  224. SiteIdentityPermission result = new SiteIdentityPermission(PermissionState.None);
  225. result.m_sites = alSites.ToArray();
  226. return result;
  227. }
  228. public override IPermission Union(IPermission target)
  229. {
  230. if (target == null)
  231. {
  232. if((this.m_sites == null || this.m_sites.Length == 0) && !this.m_unrestricted)
  233. return null;
  234. return this.Copy();
  235. }
  236. SiteIdentityPermission that = target as SiteIdentityPermission;
  237. if(that == null)
  238. throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
  239. if(this.m_unrestricted || that.m_unrestricted)
  240. {
  241. SiteIdentityPermission res = new SiteIdentityPermission(PermissionState.None);
  242. res.m_unrestricted = true;
  243. return res;
  244. }
  245. if (this.m_sites == null || this.m_sites.Length == 0)
  246. {
  247. if(that.m_sites == null || that.m_sites.Length == 0)
  248. return null;
  249. return that.Copy();
  250. }
  251. if(that.m_sites == null || that.m_sites.Length == 0)
  252. return this.Copy();
  253. List<SiteString> alSites = new List<SiteString>();
  254. foreach(SiteString ssThis in this.m_sites)
  255. alSites.Add(ssThis);
  256. foreach(SiteString ssThat in that.m_sites)
  257. {
  258. bool bDupe = false;
  259. foreach(SiteString ss in alSites)
  260. {
  261. if(ssThat.Equals(ss))
  262. {
  263. bDupe = true;
  264. break;
  265. }
  266. }
  267. if(!bDupe)
  268. alSites.Add(ssThat);
  269. }
  270. SiteIdentityPermission result = new SiteIdentityPermission(PermissionState.None);
  271. result.m_sites = alSites.ToArray();
  272. return result;
  273. }
  274. #if FEATURE_CAS_POLICY
  275. public override void FromXml(SecurityElement esd)
  276. {
  277. m_unrestricted = false;
  278. m_sites = null;
  279. CodeAccessPermission.ValidateElement( esd, this );
  280. String unr = esd.Attribute( "Unrestricted" );
  281. if(unr != null && String.Compare(unr, "true", StringComparison.OrdinalIgnoreCase) == 0)
  282. {
  283. m_unrestricted = true;
  284. return;
  285. }
  286. String elem = esd.Attribute( "Site" );
  287. List<SiteString> al = new List<SiteString>();
  288. if(elem != null)
  289. al.Add(new SiteString( elem ));
  290. ArrayList alChildren = esd.Children;
  291. if(alChildren != null)
  292. {
  293. foreach(SecurityElement child in alChildren)
  294. {
  295. elem = child.Attribute( "Site" );
  296. if(elem != null)
  297. al.Add(new SiteString( elem ));
  298. }
  299. }
  300. if(al.Count != 0)
  301. m_sites = al.ToArray();
  302. }
  303. public override SecurityElement ToXml()
  304. {
  305. SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.SiteIdentityPermission" );
  306. if (m_unrestricted)
  307. esd.AddAttribute( "Unrestricted", "true" );
  308. else if (m_sites != null)
  309. {
  310. if (m_sites.Length == 1)
  311. esd.AddAttribute( "Site", m_sites[0].ToString() );
  312. else
  313. {
  314. int n;
  315. for(n = 0; n < m_sites.Length; n++)
  316. {
  317. SecurityElement child = new SecurityElement("Site");
  318. child.AddAttribute( "Site", m_sites[n].ToString() );
  319. esd.AddChild(child);
  320. }
  321. }
  322. }
  323. return esd;
  324. }
  325. #endif // FEATURE_CAS_POLICY
  326. /// <internalonly/>
  327. int IBuiltInPermission.GetTokenIndex()
  328. {
  329. return SiteIdentityPermission.GetTokenIndex();
  330. }
  331. internal static int GetTokenIndex()
  332. {
  333. return BuiltInPermissionIndex.SiteIdentityPermissionIndex;
  334. }
  335. }
  336. }