4d6570786dd65770cc08708b5bfebeaa1c9d0855
[mono.git] / mcs / class / referencesource / mscorlib / system / security / policy / unioncodegroup.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 //  UnionCodeGroup.cs
7 // 
8 // <OWNER>[....]</OWNER>
9 //
10 //  Representation for code groups used for the policy mechanism
11 //
12
13 namespace System.Security.Policy {
14     
15     using System;
16     using System.Security.Util;
17     using System.Security;
18     using System.Collections;
19     using System.Diagnostics.Contracts;
20     
21     [Serializable]
22 [System.Runtime.InteropServices.ComVisible(true)]
23     [Obsolete("This type is obsolete and will be removed in a future release of the .NET Framework. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
24     sealed public class UnionCodeGroup : CodeGroup, IUnionSemanticCodeGroup
25     {
26         internal UnionCodeGroup()
27             : base()
28         {
29         }
30         
31         internal UnionCodeGroup( IMembershipCondition membershipCondition, PermissionSet permSet )
32             : base( membershipCondition, permSet )
33         {
34         }
35         
36         public UnionCodeGroup( IMembershipCondition membershipCondition, PolicyStatement policy )
37             : base( membershipCondition, policy )
38         {
39         }
40         
41         
42         [System.Security.SecuritySafeCritical]  // auto-generated
43         public override PolicyStatement Resolve( Evidence evidence )
44         {
45             if (evidence == null)
46                 throw new ArgumentNullException("evidence");
47             Contract.EndContractBlock();
48
49             object usedEvidence = null;
50             if (PolicyManager.CheckMembershipCondition(MembershipCondition, evidence, out usedEvidence))
51             {
52                 PolicyStatement thisPolicy = PolicyStatement;   // PolicyStatement getter makes a copy for us
53
54                 // If any delay-evidence was used to generate this grant set, then we need to keep track of
55                 // that for potentially later forcing it to be verified.
56                 IDelayEvaluatedEvidence delayEvidence = usedEvidence as IDelayEvaluatedEvidence;
57                 bool delayEvidenceNeedsVerification = delayEvidence != null && !delayEvidence.IsVerified;
58                 if (delayEvidenceNeedsVerification)
59                 {
60                     thisPolicy.AddDependentEvidence(delayEvidence);
61                 }
62
63                 bool foundExclusiveChild = false;
64                 IEnumerator enumerator = this.Children.GetEnumerator();
65                 while (enumerator.MoveNext() && !foundExclusiveChild)
66                 {
67                     PolicyStatement childPolicy = PolicyManager.ResolveCodeGroup(enumerator.Current as CodeGroup,
68                                                                                  evidence);
69
70                     if (childPolicy != null)
71                     {
72                         thisPolicy.InplaceUnion(childPolicy);
73
74                         if ((childPolicy.Attributes & PolicyStatementAttribute.Exclusive) == PolicyStatementAttribute.Exclusive)
75                         {
76                             foundExclusiveChild = true;
77                         }
78                     }
79                 }
80
81                 return thisPolicy;
82             }           
83             else
84             {
85                 return null;
86             }
87         }        
88
89         /// <internalonly/>
90         PolicyStatement IUnionSemanticCodeGroup.InternalResolve( Evidence evidence )
91         {
92             if (evidence == null)
93                 throw new ArgumentNullException("evidence");
94             Contract.EndContractBlock();
95                 
96             if (this.MembershipCondition.Check( evidence ))
97             {
98                 return this.PolicyStatement;
99             }
100             else
101             {
102                 return null;
103             }        
104         }
105
106         public override CodeGroup ResolveMatchingCodeGroups( Evidence evidence )
107         {
108             if (evidence == null)
109                 throw new ArgumentNullException("evidence");
110             Contract.EndContractBlock();
111
112             if (this.MembershipCondition.Check( evidence ))
113             {
114                 CodeGroup retGroup = this.Copy();
115
116                 retGroup.Children = new ArrayList();
117
118                 IEnumerator enumerator = this.Children.GetEnumerator();
119                 
120                 while (enumerator.MoveNext())
121                 {
122                     CodeGroup matchingGroups = ((CodeGroup)enumerator.Current).ResolveMatchingCodeGroups( evidence );
123                     
124                     // If the child has a policy, we are done.
125                     
126                     if (matchingGroups != null)
127                     {
128                         retGroup.AddChild( matchingGroups );
129                     }
130                 }
131
132                 return retGroup;
133                 
134             }
135             else
136             {
137                 return null;
138             }
139         }
140
141
142         public override CodeGroup Copy()
143         {
144             UnionCodeGroup group = new UnionCodeGroup();
145             
146             group.MembershipCondition = this.MembershipCondition;
147             group.PolicyStatement = this.PolicyStatement;
148             group.Name = this.Name;
149             group.Description = this.Description;
150
151             IEnumerator enumerator = this.Children.GetEnumerator();
152
153             while (enumerator.MoveNext())
154             {
155                 group.AddChild( (CodeGroup)enumerator.Current );
156             }
157
158             
159             return group;
160         }
161         
162         public override String MergeLogic
163         {
164             get
165             {
166                 return Environment.GetResourceString( "MergeLogic_Union" );
167             }
168         }
169         
170         internal override String GetTypeName()
171         {
172             return "System.Security.Policy.UnionCodeGroup";
173         }
174     
175     }                
176
177 }