add ISafeSerializationData
[mcs.git] / class / System / System.Net.Mail / SmtpPermission.cs
blob49f2f671210df7becfc04256b483bb59359abf9b
1 //
2 // System.Net.Mail.SmtpPermission
3 //
4 // Authors:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #if NET_2_0
31 using System.Security;
32 using System.Security.Permissions;
34 namespace System.Net.Mail {
36 [Serializable]
37 public sealed class SmtpPermission : CodeAccessPermission, IUnrestrictedPermission {
39 private const int version = 1;
41 private bool unrestricted;
42 private SmtpAccess access;
45 public SmtpPermission (bool unrestricted)
46 : base ()
48 this.unrestricted = unrestricted;
49 access = unrestricted ? SmtpAccess.ConnectToUnrestrictedPort : SmtpAccess.None;
52 public SmtpPermission (PermissionState state)
53 : base ()
55 unrestricted = (state == PermissionState.Unrestricted);
56 access = unrestricted ? SmtpAccess.ConnectToUnrestrictedPort : SmtpAccess.None;
59 public SmtpPermission (SmtpAccess access)
60 : base ()
62 // this ctor can accept invalid enum values
63 this.access = access;
67 public SmtpAccess Access {
68 get { return access; }
72 public void AddPermission (SmtpAccess access)
74 if (!unrestricted && (access > this.access)) {
75 this.access = access;
79 public override IPermission Copy ()
81 if (unrestricted) {
82 return new SmtpPermission (true);
83 } else {
84 return new SmtpPermission (access);
88 public override IPermission Intersect (IPermission target)
90 SmtpPermission sp = Cast (target);
91 if (sp == null)
92 return null;
94 if (unrestricted && sp.unrestricted)
95 return new SmtpPermission (true);
96 else if (access > sp.access)
97 return new SmtpPermission (sp.access);
98 else
99 return new SmtpPermission (access);
102 public override bool IsSubsetOf (IPermission target)
104 SmtpPermission sp = Cast (target);
105 if (sp == null)
106 return IsEmpty ();
108 if (unrestricted) {
109 return sp.unrestricted;
110 } else {
111 return (access <= sp.access);
115 public bool IsUnrestricted ()
117 return unrestricted;
120 public override SecurityElement ToXml ()
122 SecurityElement se = PermissionHelper.Element (typeof (SmtpPermission), version);
123 if (unrestricted) {
124 se.AddAttribute ("Unrestricted", "true");
125 } else {
126 switch (access) {
127 case SmtpAccess.ConnectToUnrestrictedPort:
128 se.AddAttribute ("Access", "ConnectToUnrestrictedPort");
129 break;
130 case SmtpAccess.Connect:
131 se.AddAttribute ("Access", "Connect");
132 break;
133 // note: SmtpAccess.None and invalid values aren't serialized to XML
136 return se;
139 public override void FromXml (SecurityElement securityElement)
141 PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
143 // LAMESPEC: it says to throw an ArgumentNullException in this case
144 if (securityElement.Tag != "IPermission")
145 throw new ArgumentException ("securityElement");
147 if (PermissionHelper.IsUnrestricted (securityElement))
148 access = SmtpAccess.Connect;
149 else
150 access = SmtpAccess.None;
153 public override IPermission Union (IPermission target)
155 SmtpPermission sp = Cast (target);
156 if (sp == null)
157 return Copy ();
159 if (unrestricted || sp.unrestricted)
160 return new SmtpPermission (true);
161 else if (access > sp.access)
162 return new SmtpPermission (access);
163 else
164 return new SmtpPermission (sp.access);
167 // Internal helpers methods
169 private bool IsEmpty ()
171 return (!unrestricted && (access == SmtpAccess.None));
174 private SmtpPermission Cast (IPermission target)
176 if (target == null)
177 return null;
179 SmtpPermission sp = (target as SmtpPermission);
180 if (sp == null) {
181 PermissionHelper.ThrowInvalidPermission (target, typeof (SmtpPermission));
184 return sp;
189 #endif