1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 //
5 // ==--==
6 //  FirstMatchCodeGroup.cs
7 //
8 // <OWNER>Microsoft</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;
17     using System.Security.Util;
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 FirstMatchCodeGroup : CodeGroup
25     {
FirstMatchCodeGroup()26         internal FirstMatchCodeGroup()
27             : base()
28         {
29         }
30 
FirstMatchCodeGroup( IMembershipCondition membershipCondition, PolicyStatement policy )31         public FirstMatchCodeGroup( IMembershipCondition membershipCondition, PolicyStatement policy )
32             : base( membershipCondition, policy )
33         {
34         }
35 
36 
37         [System.Security.SecuritySafeCritical]  // auto-generated
Resolve( Evidence evidence )38         public override PolicyStatement Resolve( Evidence evidence )
39         {
40             if (evidence == null)
41                 throw new ArgumentNullException("evidence");
42             Contract.EndContractBlock();
43 
44             object usedEvidence = null;
45             if (PolicyManager.CheckMembershipCondition(MembershipCondition,
46                                                        evidence,
47                                                        out usedEvidence))
48             {
49                 PolicyStatement childPolicy = null;
50 
51                 IEnumerator enumerator = this.Children.GetEnumerator();
52 
53                 while (enumerator.MoveNext())
54                 {
55                     childPolicy = PolicyManager.ResolveCodeGroup(enumerator.Current as CodeGroup,
56                                                                  evidence);
57 
58                     // If the child has a policy, we are done.
59                     if (childPolicy != null)
60                     {
61                         break;
62                     }
63                 }
64 
65                 // If any delay-evidence was used to generate this grant set, then we need to keep track of
66                 // that for potentially later forcing it to be verified.
67                 IDelayEvaluatedEvidence delayEvidence = usedEvidence as IDelayEvaluatedEvidence;
68                 bool delayEvidenceNeedsVerification = delayEvidence != null && !delayEvidence.IsVerified;
69 
70                 PolicyStatement thisPolicy = this.PolicyStatement; // PolicyStatement getter makes a copy for us
71 
72                 if (thisPolicy == null)
73                 {
74                     // We didn't add any permissions, but we enabled our children to be evaluated, and
75                     // therefore its grant set is dependent on any of our delay evidence.
76                     if (delayEvidenceNeedsVerification)
77                     {
78                         childPolicy = childPolicy.Copy();
79                         childPolicy.AddDependentEvidence(delayEvidence);
80                     }
81 
82                     return childPolicy;
83                 }
84                 else if (childPolicy != null)
85                 {
86                     // Combine the child and this policy and return it.
87 
88                     PolicyStatement combined = thisPolicy.Copy();
89 
90                     if (delayEvidenceNeedsVerification)
91                     {
92                         combined.AddDependentEvidence(delayEvidence);
93                     }
94 
95                     combined.InplaceUnion(childPolicy);
96                     return combined;
97                 }
98                 else
99                 {
100                     // Otherwise we just copy the this policy.
101                     if (delayEvidenceNeedsVerification)
102                     {
103                         thisPolicy.AddDependentEvidence(delayEvidence);
104                     }
105 
106                     return thisPolicy;
107                 }
108             }
109             else
110             {
111                 return null;
112             }
113         }
114 
ResolveMatchingCodeGroups( Evidence evidence )115         public override CodeGroup ResolveMatchingCodeGroups( Evidence evidence )
116         {
117             if (evidence == null)
118                 throw new ArgumentNullException("evidence");
119             Contract.EndContractBlock();
120 
121             if (this.MembershipCondition.Check( evidence ))
122             {
123                 CodeGroup retGroup = this.Copy();
124 
125                 retGroup.Children = new ArrayList();
126 
127                 IEnumerator enumerator = this.Children.GetEnumerator();
128 
129                 while (enumerator.MoveNext())
130                 {
131                     CodeGroup matchingGroups = ((CodeGroup)enumerator.Current).ResolveMatchingCodeGroups( evidence );
132 
133                     // If the child has a policy, we are done.
134 
135                     if (matchingGroups != null)
136                     {
137                         retGroup.AddChild( matchingGroups );
138                         break;
139                     }
140                 }
141 
142                 return retGroup;
143 
144             }
145             else
146             {
147                 return null;
148             }
149         }
150 
Copy()151         public override CodeGroup Copy()
152         {
153             FirstMatchCodeGroup group = new FirstMatchCodeGroup();
154 
155             group.MembershipCondition = this.MembershipCondition;
156             group.PolicyStatement = this.PolicyStatement;
157             group.Name = this.Name;
158             group.Description = this.Description;
159 
160             IEnumerator enumerator = this.Children.GetEnumerator();
161 
162             while (enumerator.MoveNext())
163             {
164                 group.AddChild( (CodeGroup)enumerator.Current );
165             }
166 
167             return group;
168         }
169 
170 
171         public override String MergeLogic
172         {
173             get
174             {
175                 return Environment.GetResourceString( "MergeLogic_FirstMatch" );
176             }
177         }
178 
GetTypeName()179         internal override String GetTypeName()
180         {
181             return "System.Security.Policy.FirstMatchCodeGroup";
182         }
183 
184     }
185 
186 
187 }
188