* ReaderWriterLock.cs: Changed some methods to private.
[mono.git] / mcs / class / corlib / System.Security / CodeAccessPermission.cs
1 //
2 // System.Security.CodeAccessPermission.cs
3 //
4 // Authors:
5 //      Miguel de Icaza (miguel@ximian.com)
6 //      Nick Drochak, ndrochak@gol.com
7 //      Sebastien Pouliot (spouliot@motus.com)
8 //
9 // (C) Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2001 Nick Drochak, All Rights Reserved
11 // Portions (C) 2004 Motus Technologies Inc. (http://www.motus.com)
12 //
13
14 using System.Globalization;
15 using System.Security.Permissions;
16 using System.Text;
17
18 namespace System.Security {
19
20         [Serializable]
21         public abstract class CodeAccessPermission : IPermission, ISecurityEncodable, IStackWalk {
22
23                 protected CodeAccessPermission () {}
24
25                 // LAMESPEC: Documented as virtual
26                 [MonoTODO("SecurityStackFrame not ready")]
27                 public void Assert ()
28                 {
29                         // throw a SecurityException if Assertion is denied
30                         new SecurityPermission (SecurityPermissionFlag.Assertion).Demand ();
31 //                      SecurityStackFrame.Current.Assert = this;
32                 }
33
34                 public abstract IPermission Copy ();
35
36                 // LAMESPEC: Documented as virtual
37                 [MonoTODO("MS contralize demands, but I think we should branch back into indivual permission classes.")]
38                 public void Demand ()
39                 {
40                         IBuiltInPermission perm = (this as IBuiltInPermission);
41                         if (perm == null)
42                                 return; // not sure about this :(
43
44                         // TODO : Loop the stack
45                         switch (perm.GetTokenIndex ()) {
46                                 case 0: // EnvironmentPermission
47                                         // TODO
48                                         break;
49                                 case 1: // FileDialogPermission
50                                         // TODO
51                                         break;
52                                 case 2: // FileIOPermission
53                                         // TODO
54                                         break;
55                                 case 3: // IsolatedStorageFilePermission
56                                         // TODO
57                                         break;
58                                 case 4: // ReflectionPermission
59                                         // TODO
60                                         break;
61                                 case 5: // RegistryPermission
62                                         // TODO
63                                         break;
64                                 case 6: // SecurityPermission
65                                         // TODO
66                                         break;
67                                 case 7: // UIPermission
68                                         // TODO
69                                         break;
70                                 case 8: // PrincipalPermission
71                                         // TODO
72                                         break;
73                                 case 9: // PublisherIdentityPermission
74                                         // TODO
75                                         break;
76                                 case 10: // SiteIdentityPermission
77                                         // TODO
78                                         break;
79                                 case 11: // StrongNameIdentityPermission
80                                         // TODO
81                                         break;
82                                 case 12: // UrlIdentityPermission
83                                         // TODO
84                                         break;
85                                 case 13: // ZoneIdentityPermission
86                                         // TODO
87                                         break;
88                                 default:
89                                         string message = String.Format (Locale.GetText ("Unknown IBuiltInPermission #{0}"), perm.GetTokenIndex ());
90                                         throw new SecurityException (message);
91                         }
92                 }
93
94                 // LAMESPEC: Documented as virtual
95                 [MonoTODO("SecurityStackFrame not ready")]
96                 public void Deny ()
97                 {
98 //                      SecurityStackFrame.Current.Deny = this;
99                 }
100
101                 public abstract void FromXml (SecurityElement elem);
102
103                 public abstract IPermission Intersect (IPermission target);
104
105                 public abstract bool IsSubsetOf (IPermission target);
106
107                 public override string ToString ()
108                 {
109                         SecurityElement elem = ToXml ();
110                         return elem.ToString ();
111                 }
112
113                 public abstract SecurityElement ToXml ();
114
115                 public virtual IPermission Union (IPermission other)
116                 {
117                         if (null != other)
118                                 throw new System.NotSupportedException (); // other is not null.
119                         return null;
120                 }
121
122                 // LAMESPEC: Documented as virtual
123                 [MonoTODO("SecurityStackFrame not ready")]
124                 public void PermitOnly ()
125                 {
126 //                      SecurityStackFrame.Current.PermitOnly = this;
127                 }
128
129                 [MonoTODO("SecurityStackFrame not ready")]
130                 public static void RevertAll ()
131                 {
132 //                      SecurityStackFrame.Current.RevertAll ();
133                 }
134
135                 [MonoTODO("SecurityStackFrame not ready")]
136                 public static void RevertAssert () 
137                 {
138 //                      SecurityStackFrame.Current.RevertAssert ();
139                 }
140
141                 [MonoTODO("SecurityStackFrame not ready")]
142                 public static void RevertDeny ()
143                 {
144 //                      SecurityStackFrame.Current.RevertDeny ();
145                 }
146
147                 [MonoTODO("SecurityStackFrame not ready")]
148                 public static void RevertPermitOnly () 
149                 {
150 //                      SecurityStackFrame.Current.RevertPermitOnly ();
151                 }
152
153                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
154                 internal SecurityElement Element (object o, int version) 
155                 {
156                         SecurityElement se = new SecurityElement ("IPermission");
157                         Type type = this.GetType ();
158                         StringBuilder asmName = new StringBuilder (type.Assembly.ToString ());
159                         asmName.Replace ('\"', '\'');
160                         se.AddAttribute ("class", type.FullName + ", " + asmName);
161                         se.AddAttribute ("version", version.ToString ());
162                         return se;
163                 }
164         }
165 }