44e2171bad024c8b73bfa5b13a1d5fbeb3d98d7f
[mono.git] / mcs / class / System.Drawing / System.Drawing.Printing / PrintingPermission.cs
1 //
2 // System.Drawing.PrintingPermission.cs
3 //
4 // Authors:
5 //      Dennis Hayes (dennish@Raytek.com)
6 //      Herve Poussineau (hpoussineau@fr.st)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (C) 2002 Ximian, Inc
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Globalization;
33 using System.Security;
34 using System.Security.Permissions;
35
36 namespace System.Drawing.Printing {
37
38         [Serializable]
39         public sealed class PrintingPermission : CodeAccessPermission, IUnrestrictedPermission {
40
41                 private const int version = 1;
42
43                 private PrintingPermissionLevel _Level;
44                 
45                 public PrintingPermission (PermissionState state) 
46                 {
47                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
48                                 _Level = PrintingPermissionLevel.AllPrinting;
49                 }
50
51                 public PrintingPermission (PrintingPermissionLevel printingLevel) 
52                 {
53                         Level = printingLevel;
54                 }
55                 
56                 // properties
57
58                 public PrintingPermissionLevel Level{
59                         get { return _Level; }
60                         set {
61                                 if (!Enum.IsDefined (typeof (PrintingPermissionLevel), value)) {
62                                         string msg = Locale.GetText ("Invalid enum {0}");
63                                         throw new ArgumentException (String.Format (msg, value), "Level");
64                                 }
65                                  _Level = value;
66                         }
67                 }
68
69                 // methods
70
71                 public override IPermission Copy ()
72                 {
73                         return new PrintingPermission (this.Level);
74                 }
75                 
76                 public override void FromXml (SecurityElement esd)
77                 {
78                         CheckSecurityElement (esd, "esd", version, version);
79                         // Note: we do not (yet) care about the return value 
80                         // as we only accept version 1 (min/max values)
81
82                         if (IsUnrestricted (esd))
83                                 _Level = PrintingPermissionLevel.AllPrinting;
84                         else {
85                                 string level = esd.Attribute ("Level");
86                                 if (level != null) {
87                                         _Level = (PrintingPermissionLevel) Enum.Parse (
88                                                 typeof (PrintingPermissionLevel), level);
89                                 }
90                                 else
91                                         _Level = PrintingPermissionLevel.NoPrinting;
92                         }
93                 }
94                 
95                 public override IPermission Intersect (IPermission target)
96                 {
97                         PrintingPermission pp = Cast (target);
98                         if ((pp == null) || IsEmpty () || pp.IsEmpty ())
99                                 return null;
100
101                         PrintingPermissionLevel level = (_Level <= pp.Level) ? _Level : pp.Level;
102                         return new PrintingPermission (level);
103                 }
104                 
105                 public override bool IsSubsetOf (IPermission target)
106                 {
107                         PrintingPermission pp = Cast (target);
108                         if (pp == null)
109                                 return IsEmpty ();
110                         
111                         return (_Level <= pp.Level);
112                 }
113                 
114                 public bool IsUnrestricted ()
115                 {
116                         return (_Level == PrintingPermissionLevel.AllPrinting);
117                 }
118                 
119                 public override SecurityElement ToXml ()
120                 {
121                         SecurityElement se = Element (version);
122                         if (IsUnrestricted ())
123                                 se.AddAttribute ("Unrestricted", "true");
124                         else
125                                 se.AddAttribute ("Level", _Level.ToString ());
126                         return se;
127                 }
128                 
129                 public override IPermission Union (IPermission target)
130                 {
131                         PrintingPermission pp = Cast (target);
132                         if (pp == null)
133                                 return new PrintingPermission (_Level);
134                         if (IsUnrestricted () || pp.IsUnrestricted ())
135                                 return new PrintingPermission (PermissionState.Unrestricted);
136                         if (IsEmpty () && pp.IsEmpty ())
137                                 return null;
138
139                         PrintingPermissionLevel level = (_Level > pp.Level) ? _Level : pp.Level;
140                         return new PrintingPermission (level);
141                 }
142
143                 // Internal helpers methods
144
145                 private bool IsEmpty ()
146                 {
147                         return (_Level == PrintingPermissionLevel.NoPrinting);
148                 }
149
150                 private PrintingPermission Cast (IPermission target)
151                 {
152                         if (target == null)
153                                 return null;
154
155                         PrintingPermission pp = (target as PrintingPermission);
156                         if (pp == null) {
157                                 ThrowInvalidPermission (target, typeof (PrintingPermission));
158                         }
159
160                         return pp;
161                 }
162
163                 // NOTE: The following static methods should be moved out to a (static?) class 
164                 // if (ever) System.Drawing.dll gets more than one permission in it's assembly.
165
166                 // snippet moved from FileIOPermission (nickd) to be reused in all derived classes
167                 internal SecurityElement Element (int version) 
168                 {
169                         SecurityElement se = new SecurityElement ("IPermission");
170                         Type type = this.GetType ();
171                         se.AddAttribute ("class", type.FullName + ", " + type.Assembly.ToString ().Replace ('\"', '\''));
172                         se.AddAttribute ("version", version.ToString ());
173                         return se;
174                 }
175
176                 internal static PermissionState CheckPermissionState (PermissionState state, bool allowUnrestricted)
177                 {
178                         string msg;
179                         switch (state) {
180                         case PermissionState.None:
181                                 break;
182                         case PermissionState.Unrestricted:
183                                 if (!allowUnrestricted) {
184                                         msg = Locale.GetText ("Unrestricted isn't not allowed for identity permissions.");
185                                         throw new ArgumentException (msg, "state");
186                                 }
187                                 break;
188                         default:
189                                 msg = String.Format (Locale.GetText ("Invalid enum {0}"), state);
190                                 throw new ArgumentException (msg, "state");
191                         }
192                         return state;
193                 }
194
195                 // logic isn't identical to CodeAccessPermission.CheckSecurityElement - see unit tests
196                 internal static int CheckSecurityElement (SecurityElement se, string parameterName, int minimumVersion, int maximumVersion) 
197                 {
198                         if (se == null)
199                                 throw new ArgumentNullException (parameterName);
200
201                         string c = se.Attribute ("class");
202                         if (c == null) {
203                                 string msg = Locale.GetText ("Missing 'class' attribute.");
204                                 throw new ArgumentException (msg, parameterName);
205                         }
206                         // we assume minimum version if no version number is supplied
207                         int version = minimumVersion;
208                         string v = se.Attribute ("version");
209                         if (v != null) {
210                                 try {
211                                         version = Int32.Parse (v);
212                                 }
213                                 catch (Exception e) {
214                                         string msg = Locale.GetText ("Couldn't parse version from '{0}'.");
215                                         msg = String.Format (msg, v);
216                                         throw new ArgumentException (msg, parameterName, e);
217                                 }
218                         }
219
220                         if ((version < minimumVersion) || (version > maximumVersion)) {
221                                 string msg = Locale.GetText ("Unknown version '{0}', expected versions between ['{1}','{2}'].");
222                                 msg = String.Format (msg, version, minimumVersion, maximumVersion);
223                                 throw new ArgumentException (msg, parameterName);
224                         }
225                         return version;
226                 }
227
228                 // must be called after CheckSecurityElement (i.e. se != null)
229                 internal static bool IsUnrestricted (SecurityElement se) 
230                 {
231                         string value = se.Attribute ("Unrestricted");
232                         if (value == null)
233                                 return false;
234                         return (String.Compare (value, Boolean.TrueString, true, CultureInfo.InvariantCulture) == 0);
235                 }
236
237                 internal static void ThrowInvalidPermission (IPermission target, Type expected) 
238                 {
239                         string msg = Locale.GetText ("Invalid permission type '{0}', expected type '{1}'.");
240                         msg = String.Format (msg, target.GetType (), expected);
241                         throw new ArgumentException (msg, "target");
242                 }
243         }
244 }