2 // System.Security.Policy.FileCodeGroup
5 // Nick Drochak (ndrochak@gol.com)
7 // (C) 2001 Nick Drochak, All rights reserved.
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Runtime.InteropServices;
32 using System.Security.Permissions;
34 namespace System.Security.Policy {
40 public sealed class FileCodeGroup : CodeGroup {
42 private FileIOPermissionAccess m_access;
44 public FileCodeGroup (IMembershipCondition membershipCondition, FileIOPermissionAccess access)
45 : base (membershipCondition, null)
47 // note: FileIOPermissionAccess is a [Flag]
51 // for PolicyLevel (to avoid validation duplication)
52 internal FileCodeGroup (SecurityElement e, PolicyLevel level)
57 public override CodeGroup Copy ()
59 FileCodeGroup copy = new FileCodeGroup (MembershipCondition, m_access);
60 copy.Name = this.Name;
61 copy.Description = this.Description;
62 foreach (CodeGroup child in Children) {
63 copy.AddChild (child.Copy ()); // deep copy
68 public override string MergeLogic {
69 get { return "Union";}
72 public override PolicyStatement Resolve (Evidence evidence)
75 throw new ArgumentNullException("evidence");
77 if (!MembershipCondition.Check (evidence))
80 PermissionSet ps = null;
81 if (this.PolicyStatement == null)
82 ps = new PermissionSet (PermissionState.None);
84 ps = this.PolicyStatement.PermissionSet.Copy ();
86 if (this.Children.Count > 0) {
87 foreach (CodeGroup child_cg in this.Children) {
88 PolicyStatement child_pst = child_cg.Resolve (evidence);
89 if (child_pst != null) {
90 ps = ps.Union (child_pst.PermissionSet);
95 PolicyStatement pst = null;
96 if (this.PolicyStatement != null)
97 pst = this.PolicyStatement.Copy ();
99 pst = PolicyStatement.Empty ();
100 pst.PermissionSet = ps;
104 public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
106 if (null == evidence)
107 throw new ArgumentNullException("evidence");
109 if (!MembershipCondition.Check (evidence))
112 FileCodeGroup matchRoot = new FileCodeGroup (MembershipCondition, m_access);
114 foreach (CodeGroup child in Children) {
115 CodeGroup childMatchingCodeGroup = child.ResolveMatchingCodeGroups (evidence);
116 if (childMatchingCodeGroup != null)
117 matchRoot.AddChild (childMatchingCodeGroup);
123 public override string AttributeString {
127 public override string PermissionSetName {
128 get { return "Same directory FileIO - " + m_access.ToString (); }
131 public override bool Equals (object o)
133 if (!(o is FileCodeGroup))
136 if (this.m_access != ((FileCodeGroup)o).m_access)
139 return Equals((CodeGroup)o, false);
142 public override int GetHashCode ()
144 return m_access.GetHashCode ();
147 protected override void ParseXml (SecurityElement e, PolicyLevel level)
149 string a = e.Attribute ("Access");
151 m_access = (FileIOPermissionAccess) Enum.Parse (typeof (FileIOPermissionAccess), a, true);
153 m_access = FileIOPermissionAccess.NoAccess;
156 protected override void CreateXml (SecurityElement element, PolicyLevel level)
158 element.AddAttribute ("Access", m_access.ToString ());