2004-08-17 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Permissions / PermissionSetAttribute.cs
1 //
2 // System.Security.Permissions.PermissionSetAttribute.cs
3 //
4 // Authors
5 //      Duncan Mak <duncan@ximian.com>
6 //      Sebastien Pouliot  <spouliot@videotron.ca>
7 //
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.IO;
36 using System.Security.Policy;
37 using System.Text;
38
39 using Mono.Xml;
40
41 namespace System.Security.Permissions {
42
43         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
44                          AttributeTargets.Struct | AttributeTargets.Constructor |
45                          AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
46         [Serializable]
47         public sealed class PermissionSetAttribute : CodeAccessSecurityAttribute {
48
49                 // Fields
50                 private string file;
51                 private string name;
52                 private bool isUnicodeEncoded;
53                 private string xml;
54                 
55                 // Constructor
56                 public PermissionSetAttribute (SecurityAction action)
57                         : base (action)
58                 {
59                 }
60                 
61                 // Properties
62                 public string File
63                 {
64                         get { return file; }
65                         set { file = value; }
66                 }
67
68                 public string Name
69                 {
70                         get { return name; }
71                         set { name = value; }
72                 }
73
74                 public bool UnicodeEncoded
75                 {
76                         get { return isUnicodeEncoded; }
77                         set { isUnicodeEncoded = value; }
78                 }
79
80                 public string XML
81                 {
82                         get { return xml; }
83                         set { xml = value; }
84                 }
85                 
86                 // Methods
87                 public override IPermission CreatePermission ()
88                 {
89                         return null;      // Not used, used for inheritance from SecurityAttribute
90                 }
91
92                 private PermissionSet CreateFromXml (string xml) 
93                 {
94                         SecurityParser sp = new SecurityParser ();
95                         sp.LoadXml (xml);
96                         SecurityElement se = sp.ToXml ();
97
98                         string className = se.Attribute ("class");
99                         if (className == null)
100                                 return null;
101
102                         PermissionState state = PermissionState.None;
103                         if (se.Attribute ("Unrestricted") == "true")
104                                 state = PermissionState.Unrestricted;
105
106                         if (className.EndsWith ("NamedPermissionSet")) {
107                                 NamedPermissionSet nps = new NamedPermissionSet (se.Attribute ("Name"), state);
108                                 return (PermissionSet) nps;
109                         }
110                         else if (className.EndsWith ("PermissionSet")) {
111                                 PermissionSet ps = new PermissionSet (state);
112                                 return ps;
113                         }
114                         return null;
115                 }
116
117                 public PermissionSet CreatePermissionSet ()
118                 {
119                         PermissionSet pset = null;
120                         if (this.Unrestricted)
121                                 pset = new PermissionSet (PermissionState.Unrestricted);
122                         else {
123                                 pset = new PermissionSet (PermissionState.None);
124                                 if (name != null) {
125                                         return PolicyLevel.CreateAppDomainLevel ().GetNamedPermissionSet (name);
126                                 }
127                                 else if (file != null) {
128                                         Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
129                                         using (StreamReader sr = new StreamReader (file, e)) {
130                                                 pset = CreateFromXml (sr.ReadToEnd ());
131                                         }
132                                 }
133                                 else if (xml != null) {
134                                         pset = CreateFromXml (xml);
135                                 }
136                         }
137                         return pset;
138                 }
139         }
140 }