**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Security.Policy / NetCodeGroup.cs
blob6c9d1162aa67c78863ef45d47e881d69a27c0198
1 //
2 // System.Security.Policy.NetCodeGroup
3 //
4 // Authors:
5 // Jackson Harper (Jackson@LatitudeGeo.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) 2002 Jackson Harper, All rights reserved
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
31 using System.Collections;
32 using System.Globalization;
34 namespace System.Security.Policy {
36 [Serializable]
37 public sealed class NetCodeGroup : CodeGroup {
39 #if NET_2_0
40 public static readonly string AbsentOriginScheme = String.Empty;
41 public static readonly string AnyOtherOriginScheme = "*";
43 private CodeGroupGrantScope _scope = CodeGroupGrantScope.Assembly;
44 private Hashtable _rules = new Hashtable ();
45 private int _hashcode;
46 #endif
48 public NetCodeGroup (IMembershipCondition condition)
49 : base (condition, null)
53 // for PolicyLevel (to avoid validation duplication)
54 internal NetCodeGroup (SecurityElement e, PolicyLevel level)
55 : base (e, level)
60 // Public Properties
63 public override string AttributeString {
64 get { return null; }
67 public override string MergeLogic {
68 get { return "Union"; }
71 public override string PermissionSetName {
72 #if NET_2_0
73 get { return "Same site Web"; }
74 #else
75 get { return "Same site Web."; }
76 #endif
79 #if NET_2_0
80 public CodeGroupGrantScope Scope {
81 get { return _scope; }
82 set { _scope = value; }
84 #endif
87 // Public Methods
90 #if NET_2_0
91 [MonoTODO ("missing validations")]
92 public void AddConnectAccess (string originScheme, CodeConnectAccess connectAccess)
94 if (originScheme == null)
95 throw new ArgumentException ("originScheme");
97 // TODO - invalid characters in originScheme
98 if ((originScheme == AbsentOriginScheme) && (connectAccess.Scheme == CodeConnectAccess.OriginScheme)) {
99 throw new ArgumentOutOfRangeException ("connectAccess", Locale.GetText (
100 "Schema == CodeConnectAccess.OriginScheme"));
103 if (_rules.ContainsKey (originScheme)) {
104 // NULL has no effect
105 if (connectAccess != null) {
106 CodeConnectAccess[] existing = (CodeConnectAccess[]) _rules [originScheme];
107 CodeConnectAccess[] array = new CodeConnectAccess [existing.Length + 1];
108 Array.Copy (existing, 0, array, 0, existing.Length);
109 array [existing.Length] = connectAccess;
110 _rules [originScheme] = array;
113 else {
114 CodeConnectAccess[] array = new CodeConnectAccess [1];
115 array [0] = connectAccess;
116 _rules.Add (originScheme, array);
117 // add null to prevent access
120 #endif
122 public override CodeGroup Copy ()
124 NetCodeGroup copy = new NetCodeGroup (MembershipCondition);
125 copy.Name = Name;
126 copy.Description = Description;
127 copy.PolicyStatement = PolicyStatement;
129 foreach (CodeGroup child in Children) {
130 copy.AddChild (child.Copy ()); // deep copy
132 return copy;
135 #if NET_2_0
136 private bool Equals (CodeConnectAccess[] rules1, CodeConnectAccess[] rules2)
138 for (int i=0; i < rules1.Length; i++) {
139 bool found = false;
140 for (int j=0; j < rules2.Length; j++) {
141 if (rules1 [i].Equals (rules2 [j])) {
142 found = true;
143 break;
146 if (!found)
147 return false;
149 return true;
152 public override bool Equals (object o)
154 if (!base.Equals (o))
155 return false;
156 NetCodeGroup ncg = (o as NetCodeGroup);
157 if (ncg == null)
158 return false;
160 // check rules
161 foreach (DictionaryEntry de in _rules) {
162 bool found = false;
163 CodeConnectAccess[] ccas = (CodeConnectAccess[]) ncg._rules [de.Key];
164 if (ccas != null)
165 found = Equals ((CodeConnectAccess[]) de.Value, ccas);
166 else
167 found = (de.Value == null);
169 if (!found)
170 return false;
172 return true;
175 public DictionaryEntry[] GetConnectAccessRules ()
177 DictionaryEntry[] result = new DictionaryEntry [_rules.Count];
178 _rules.CopyTo (result, 0);
179 return result;
182 public override int GetHashCode ()
184 if (_hashcode == 0) {
185 _hashcode = base.GetHashCode ();
186 foreach (DictionaryEntry de in _rules) {
187 CodeConnectAccess[] ccas = (CodeConnectAccess[]) de.Value;
188 if (ccas != null) {
189 foreach (CodeConnectAccess cca in ccas) {
190 _hashcode ^= cca.GetHashCode ();
195 return _hashcode;
197 #endif
199 public override PolicyStatement Resolve (Evidence evidence)
201 if (evidence == null)
202 throw new ArgumentNullException ("evidence");
204 if (!MembershipCondition.Check (evidence))
205 return null;
207 PolicyStatement pst = this.PolicyStatement.Copy ();
209 if (this.Children.Count > 0) {
210 foreach (CodeGroup child_cg in this.Children) {
211 PolicyStatement child_pst = child_cg.Resolve (evidence);
212 if (child_pst != null) {
213 foreach (IPermission perm in child_pst.PermissionSet) {
214 pst.PermissionSet.AddPermission (perm);
219 return pst;
222 #if NET_2_0
223 public void ResetConnectAccess ()
225 _rules.Clear ();
227 #endif
229 public override CodeGroup ResolveMatchingCodeGroups (Evidence evidence)
231 if (evidence == null)
232 throw new ArgumentNullException ("evidence");
234 CodeGroup return_group = null;
235 if (MembershipCondition.Check (evidence)) {
236 return_group = Copy ();
238 foreach (CodeGroup child_group in Children) {
239 CodeGroup matching =
240 child_group.ResolveMatchingCodeGroups (evidence);
241 if (matching == null)
242 continue;
243 return_group.AddChild (matching);
247 return return_group;
250 #if NET_2_0
251 [MonoTODO]
252 protected override void CreateXml (SecurityElement element, PolicyLevel level)
254 base.CreateXml (element, level);
257 [MonoTODO]
258 protected override void ParseXml (SecurityElement e, PolicyLevel level)
260 base.ParseXml (e, level);
262 #endif