2 // System.Security.Permissions.PermissionSetAttribute.cs
5 // Duncan Mak <duncan@ximian.com>
6 // Sebastien Pouliot <spouliot@videotron.ca>
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
36 using System.Security.Policy;
41 namespace System.Security.Permissions {
43 [AttributeUsage (AttributeTargets.Assembly | AttributeTargets.Class |
44 AttributeTargets.Struct | AttributeTargets.Constructor |
45 AttributeTargets.Method, AllowMultiple=true, Inherited=false)]
47 public sealed class PermissionSetAttribute : CodeAccessSecurityAttribute {
52 private bool isUnicodeEncoded;
56 public PermissionSetAttribute (SecurityAction action)
74 public bool UnicodeEncoded
76 get { return isUnicodeEncoded; }
77 set { isUnicodeEncoded = value; }
87 public override IPermission CreatePermission ()
89 return null; // Not used, used for inheritance from SecurityAttribute
92 private PermissionSet CreateFromXml (string xml)
94 SecurityParser sp = new SecurityParser ();
96 SecurityElement se = sp.ToXml ();
98 string className = se.Attribute ("class");
99 if (className == null)
102 PermissionState state = PermissionState.None;
103 if (se.Attribute ("Unrestricted") == "true")
104 state = PermissionState.Unrestricted;
106 if (className.EndsWith ("NamedPermissionSet")) {
107 NamedPermissionSet nps = new NamedPermissionSet (se.Attribute ("Name"), state);
108 return (PermissionSet) nps;
110 else if (className.EndsWith ("PermissionSet")) {
111 PermissionSet ps = new PermissionSet (state);
117 public PermissionSet CreatePermissionSet ()
119 PermissionSet pset = null;
120 if (this.Unrestricted)
121 pset = new PermissionSet (PermissionState.Unrestricted);
123 pset = new PermissionSet (PermissionState.None);
125 return PolicyLevel.CreateAppDomainLevel ().GetNamedPermissionSet (name);
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 ());
133 else if (xml != null) {
134 pset = CreateFromXml (xml);