2 // System.Security.Policy.NetCodeGroup
5 // Jackson Harper (Jackson@LatitudeGeo.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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
{
37 public sealed class NetCodeGroup
: CodeGroup
{
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
;
48 public NetCodeGroup (IMembershipCondition condition
)
49 : base (condition
, null)
53 // for PolicyLevel (to avoid validation duplication)
54 internal NetCodeGroup (SecurityElement e
, PolicyLevel level
)
63 public override string AttributeString
{
67 public override string MergeLogic
{
68 get { return "Union"; }
71 public override string PermissionSetName
{
73 get { return "Same site Web"; }
75 get { return "Same site Web."; }
80 public CodeGroupGrantScope Scope
{
81 get { return _scope; }
82 set { _scope = value; }
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
;
114 CodeConnectAccess
[] array
= new CodeConnectAccess
[1];
115 array
[0] = connectAccess
;
116 _rules
.Add (originScheme
, array
);
117 // add null to prevent access
122 public override CodeGroup
Copy ()
124 NetCodeGroup copy
= new NetCodeGroup (MembershipCondition
);
126 copy
.Description
= Description
;
127 copy
.PolicyStatement
= PolicyStatement
;
129 foreach (CodeGroup child
in Children
) {
130 copy
.AddChild (child
.Copy ()); // deep copy
136 private bool Equals (CodeConnectAccess
[] rules1
, CodeConnectAccess
[] rules2
)
138 for (int i
=0; i
< rules1
.Length
; i
++) {
140 for (int j
=0; j
< rules2
.Length
; j
++) {
141 if (rules1
[i
].Equals (rules2
[j
])) {
152 public override bool Equals (object o
)
154 if (!base.Equals (o
))
156 NetCodeGroup ncg
= (o
as NetCodeGroup
);
161 foreach (DictionaryEntry de
in _rules
) {
163 CodeConnectAccess
[] ccas
= (CodeConnectAccess
[]) ncg
._rules
[de
.Key
];
165 found
= Equals ((CodeConnectAccess
[]) de
.Value
, ccas
);
167 found
= (de
.Value
== null);
175 public DictionaryEntry
[] GetConnectAccessRules ()
177 DictionaryEntry
[] result
= new DictionaryEntry
[_rules
.Count
];
178 _rules
.CopyTo (result
, 0);
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
;
189 foreach (CodeConnectAccess cca
in ccas
) {
190 _hashcode ^
= cca
.GetHashCode ();
199 public override PolicyStatement
Resolve (Evidence evidence
)
201 if (evidence
== null)
202 throw new ArgumentNullException ("evidence");
204 if (!MembershipCondition
.Check (evidence
))
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
);
223 public void ResetConnectAccess ()
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
) {
240 child_group
.ResolveMatchingCodeGroups (evidence
);
241 if (matching
== null)
243 return_group
.AddChild (matching
);
252 protected override void CreateXml (SecurityElement element
, PolicyLevel level
)
254 base.CreateXml (element
, level
);
258 protected override void ParseXml (SecurityElement e
, PolicyLevel level
)
260 base.ParseXml (e
, level
);