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