2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System.Security.Policy / PolicyStatement.cs
1 //\r
2 // System.Security.Policy.PolicyStatement\r
3 //\r
4 // Authors:\r
5 //      Dan Lewis (dihlewis@yahoo.co.uk)\r
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //\r
8 // (C) 2002\r
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
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.
29 //
30 \r
31 using System.Runtime.InteropServices;
32 using System.Security.Permissions;\r
33 \r
34 namespace System.Security.Policy {\r
35 \r
36         [Serializable]\r
37         public sealed class PolicyStatement : ISecurityEncodable, ISecurityPolicyEncodable {
38
39                 private PermissionSet perms;\r
40                 private PolicyStatementAttribute attrs;\r
41 \r
42                 public PolicyStatement (PermissionSet perms) :\r
43                         this (perms, PolicyStatementAttribute.Nothing)\r
44                 {\r
45                 }\r
46 \r
47                 public PolicyStatement (PermissionSet perms, PolicyStatementAttribute attrs) 
48                 {\r
49                         this.perms = perms;\r
50                         this.attrs = attrs;\r
51                 }\r
52                 \r
53                 public PermissionSet PermissionSet {\r
54                         get { return perms; }\r
55                         set { perms = value; }\r
56                 }\r
57                 \r
58                 public PolicyStatementAttribute Attributes {\r
59                         get { return attrs; }\r
60                         set { attrs = value; }\r
61                 }\r
62 \r
63                 public string AttributeString {\r
64                         get {
65                                 switch (attrs) {
66                                         case PolicyStatementAttribute.Exclusive:
67                                                 return "Exclusive";
68                                         case PolicyStatementAttribute.LevelFinal:
69                                                 return "LevelFinal";
70                                         case PolicyStatementAttribute.All:
71                                                 return "Exclusive LevelFinal";
72                                         default:
73                                                 return String.Empty;
74                                 }
75                         }\r
76                 }\r
77 \r
78                 public PolicyStatement Copy ()\r
79                 {\r
80                         return new PolicyStatement (perms, attrs);\r
81                 }\r
82 \r
83                 // ISecurityEncodable\r
84 \r
85                 public void FromXml (SecurityElement e)\r
86                 {\r
87                         FromXml (e, null);\r
88                 }\r
89 \r
90                 public void FromXml (SecurityElement e, PolicyLevel level)\r
91                 {\r
92                         SecurityElement permissions = e.SearchForChildByTag ("PermissionSet");\r
93 \r
94                         string attributes = e.Attribute ("Attributes");\r
95 \r
96                         if (attributes != null)\r
97                                 attrs = (PolicyStatementAttribute) Enum.Parse (\r
98                                         typeof (PolicyStatementAttribute), attributes);\r
99                                 \r
100                         perms = new PermissionSet (PermissionState.None);\r
101                         perms.FromXml (permissions);\r
102                 }\r
103                 \r
104                 public SecurityElement ToXml ()\r
105                 {\r
106                         return ToXml (null);\r
107                 }\r
108 \r
109                 public SecurityElement ToXml (PolicyLevel level)\r
110                 {\r
111                         SecurityElement element = new SecurityElement ("PolicyStatement");\r
112                         element.AddAttribute ("version", "1");\r
113 \r
114                         if (attrs != PolicyStatementAttribute.Nothing)\r
115                                 element.AddAttribute ("Attributes", attrs.ToString ());\r
116                         \r
117                         element.AddChild (perms.ToXml ());\r
118 \r
119                         return element;\r
120                 }
121
122 #if NET_2_0
123                 [ComVisible (false)]
124                 public override bool Equals (object obj)
125                 {
126                         if (obj == null)
127                                 return false;
128                         PolicyStatement ps = (obj as PolicyStatement);
129                         if (ps == null)
130                                 return false;
131
132                         return (perms.Equals (obj) && (attrs == ps.attrs));
133                 }
134
135                 [ComVisible (false)]
136                 public override int GetHashCode ()
137                 {
138                         // return same hash code if two PolicyStatement are equals
139                         return (perms.GetHashCode () ^ (int) attrs);
140                 }
141 #endif
142
143                 internal static PolicyStatement Empty ()
144                 {
145                         return new PolicyStatement (new PermissionSet (PermissionState.None));
146                 }\r
147         }\r
148 }\r