2004-01-26 Sebastien Pouliot <spouliot@videotron.ca>
[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 using System;
12 using System.IO;
13 using System.Security.Policy;
14 using System.Text;
15
16 using Mono.Xml;
17
18 namespace System.Security.Permissions {
19
20         [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
21                          AttributeTargets.Struct | AttributeTargets.Constructor |
22                          AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
23         [Serializable]
24         public sealed class PermissionSetAttribute : CodeAccessSecurityAttribute {
25
26                 // Fields
27                 private string file;
28                 private string name;
29                 private bool isUnicodeEncoded;
30                 private string xml;
31                 
32                 // Constructor
33                 public PermissionSetAttribute (SecurityAction action)
34                         : base (action)
35                 {
36                 }
37                 
38                 // Properties
39                 public string File
40                 {
41                         get { return file; }
42                         set { file = value; }
43                 }
44
45                 public string Name
46                 {
47                         get { return name; }
48                         set { name = value; }
49                 }
50
51                 public bool UnicodeEncoded
52                 {
53                         get { return isUnicodeEncoded; }
54                         set { isUnicodeEncoded = value; }
55                 }
56
57                 public string XML
58                 {
59                         get { return xml; }
60                         set { xml = value; }
61                 }
62                 
63                 // Methods
64                 public override IPermission CreatePermission ()
65                 {
66                         return null;      // Not used, used for inheritance from SecurityAttribute
67                 }
68
69                 private PermissionSet CreateFromXml (string xml) 
70                 {
71                         SecurityParser sp = new SecurityParser ();
72                         sp.LoadXml (xml);
73                         SecurityElement se = sp.ToXml ();
74
75                         string className = se.Attribute ("class");
76                         if (className == null)
77                                 return null;
78
79                         PermissionState state = PermissionState.None;
80                         if (se.Attribute ("Unrestricted") == "true")
81                                 state = PermissionState.Unrestricted;
82
83                         if (className.EndsWith ("NamedPermissionSet")) {
84                                 NamedPermissionSet nps = new NamedPermissionSet (se.Attribute ("Name"), state);
85                                 return (PermissionSet) nps;
86                         }
87                         else if (className.EndsWith ("PermissionSet")) {
88                                 PermissionSet ps = new PermissionSet (state);
89                                 return ps;
90                         }
91                         return null;
92                 }
93
94                 public PermissionSet CreatePermissionSet ()
95                 {
96                         PermissionSet pset = null;
97                         if (this.Unrestricted)
98                                 pset = new PermissionSet (PermissionState.Unrestricted);
99                         else {
100                                 pset = new PermissionSet (PermissionState.None);
101                                 if (name != null) {
102                                         return PolicyLevel.CreateAppDomainLevel ().GetNamedPermissionSet (name);
103                                 }
104                                 else if (file != null) {
105                                         Encoding e = ((isUnicodeEncoded) ? System.Text.Encoding.Unicode : System.Text.Encoding.ASCII);
106                                         using (StreamReader sr = new StreamReader (file, e)) {
107                                                 pset = CreateFromXml (sr.ReadToEnd ());
108                                         }
109                                 }
110                                 else if (xml != null) {
111                                         pset = CreateFromXml (xml);
112                                 }
113                         }
114                         return pset;
115                 }
116         }
117 }