merged Sys.Web.Services 2.0 support in my branch:
[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-2005 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 #if NET_2_0
38         [ComVisible (true)]
39 #endif
40         public sealed class PolicyStatement : ISecurityEncodable, ISecurityPolicyEncodable {
41
42                 private PermissionSet perms;\r
43                 private PolicyStatementAttribute attrs;\r
44 \r
45                 public PolicyStatement (PermissionSet perms) :
46                         this (perms, PolicyStatementAttribute.Nothing)\r
47                 {
48                 }\r
49 \r
50                 public PolicyStatement (PermissionSet perms, PolicyStatementAttribute attrs) 
51                 {\r
52                         if (perms != null) {\r
53                                 this.perms = perms.Copy ();
54                                 this.perms.SetReadOnly (true);
55                         }\r
56                         this.attrs = attrs;\r
57                 }\r
58                 \r
59                 public PermissionSet PermissionSet {\r
60                         get {
61                                 if (perms == null) {
62                                         perms = new PermissionSet (PermissionState.None);
63                                         perms.SetReadOnly (true);
64                                 }
65                                 return perms;
66                         }\r
67                         set { perms = value; }\r
68                 }\r
69                 \r
70                 public PolicyStatementAttribute Attributes {\r
71                         get { return attrs; }\r
72                         set {
73                                 // note: yes it's a flag but all possible values have a corresponding name
74                                 switch (value) {
75                                 case PolicyStatementAttribute.Nothing:
76                                 case PolicyStatementAttribute.Exclusive:
77                                 case PolicyStatementAttribute.LevelFinal:
78                                 case PolicyStatementAttribute.All:
79                                         attrs = value;
80                                         break;
81                                 default:
82                                         string msg = Locale.GetText ("Invalid value for {0}.");
83                                         throw new ArgumentException (String.Format (msg, "PolicyStatementAttribute"));
84                                 }
85                         }\r
86                 }\r
87 \r
88                 public string AttributeString {\r
89                         get {
90                                 switch (attrs) {
91                                 case PolicyStatementAttribute.Exclusive:
92                                         return "Exclusive";
93                                 case PolicyStatementAttribute.LevelFinal:
94                                         return "LevelFinal";
95                                 case PolicyStatementAttribute.All:
96                                         return "Exclusive LevelFinal";
97                                 default:
98                                         return String.Empty;
99                                 }
100                         }\r
101                 }\r
102 \r
103                 public PolicyStatement Copy ()\r
104                 {\r
105                         return new PolicyStatement (perms, attrs);\r
106                 }\r
107 \r
108                 // ISecurityEncodable\r
109 \r
110                 public void FromXml (SecurityElement e)\r
111                 {\r
112                         FromXml (e, null);\r
113                 }\r
114 \r
115                 public void FromXml (SecurityElement e, PolicyLevel level)\r
116                 {
117                         if (e == null)
118                                 throw new ArgumentNullException ("e");
119                         if (e.Tag != "PolicyStatement")
120                                 throw new ArgumentException (Locale.GetText ("Invalid tag."));
121 \r
122 \r
123                         string attributes = e.Attribute ("Attributes");\r
124                         if (attributes != null) {\r
125                                 attrs = (PolicyStatementAttribute) Enum.Parse (\r
126                                         typeof (PolicyStatementAttribute), attributes);\r
127                         }
128 \r
129                         SecurityElement permissions = e.SearchForChildByTag ("PermissionSet");\r
130                         PermissionSet.FromXml (permissions);\r
131                 }\r
132                 \r
133                 public SecurityElement ToXml ()\r
134                 {\r
135                         return ToXml (null);\r
136                 }\r
137 \r
138                 public SecurityElement ToXml (PolicyLevel level)\r
139                 {\r
140                         SecurityElement element = new SecurityElement ("PolicyStatement");\r
141                         element.AddAttribute ("version", "1");\r
142 \r
143                         if (attrs != PolicyStatementAttribute.Nothing)\r
144                                 element.AddAttribute ("Attributes", attrs.ToString ());\r
145                         \r
146                         element.AddChild (PermissionSet.ToXml ());\r
147 \r
148                         return element;\r
149                 }
150
151 #if NET_2_0
152                 [ComVisible (false)]
153                 public override bool Equals (object obj)
154                 {
155                         if (obj == null)
156                                 return false;
157                         PolicyStatement ps = (obj as PolicyStatement);
158                         if (ps == null)
159                                 return false;
160
161                         return (PermissionSet.Equals (obj) && (attrs == ps.attrs));
162                 }
163
164                 [ComVisible (false)]
165                 public override int GetHashCode ()
166                 {
167                         // return same hash code if two PolicyStatement are equals
168                         return (PermissionSet.GetHashCode () ^ (int) attrs);
169                 }
170 #endif
171
172                 internal static PolicyStatement Empty ()
173                 {
174                         return new PolicyStatement (new PermissionSet (PermissionState.None));
175                 }
176         }\r
177 }\r