2 // System.Net.Mail.SmtpPermission
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System
.Security
;
32 using System
.Security
.Permissions
;
34 namespace System
.Net
.Mail
{
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
)
48 this.unrestricted
= unrestricted
;
49 access
= unrestricted
? SmtpAccess
.ConnectToUnrestrictedPort
: SmtpAccess
.None
;
52 public SmtpPermission (PermissionState state
)
55 unrestricted
= (state
== PermissionState
.Unrestricted
);
56 access
= unrestricted
? SmtpAccess
.ConnectToUnrestrictedPort
: SmtpAccess
.None
;
59 public SmtpPermission (SmtpAccess access
)
62 // this ctor can accept invalid enum values
67 public SmtpAccess Access
{
68 get { return access; }
72 public void AddPermission (SmtpAccess access
)
74 if (!unrestricted
&& (access
> this.access
)) {
79 public override IPermission
Copy ()
82 return new SmtpPermission (true);
84 return new SmtpPermission (access
);
88 public override IPermission
Intersect (IPermission target
)
90 SmtpPermission sp
= Cast (target
);
94 if (unrestricted
&& sp
.unrestricted
)
95 return new SmtpPermission (true);
96 else if (access
> sp
.access
)
97 return new SmtpPermission (sp
.access
);
99 return new SmtpPermission (access
);
102 public override bool IsSubsetOf (IPermission target
)
104 SmtpPermission sp
= Cast (target
);
109 return sp
.unrestricted
;
111 return (access
<= sp
.access
);
115 public bool IsUnrestricted ()
120 public override SecurityElement
ToXml ()
122 SecurityElement se
= PermissionHelper
.Element (typeof (SmtpPermission
), version
);
124 se
.AddAttribute ("Unrestricted", "true");
127 case SmtpAccess
.ConnectToUnrestrictedPort
:
128 se
.AddAttribute ("Access", "ConnectToUnrestrictedPort");
130 case SmtpAccess
.Connect
:
131 se
.AddAttribute ("Access", "Connect");
133 // note: SmtpAccess.None and invalid values aren't serialized to XML
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
;
150 access
= SmtpAccess
.None
;
153 public override IPermission
Union (IPermission target
)
155 SmtpPermission sp
= Cast (target
);
159 if (unrestricted
|| sp
.unrestricted
)
160 return new SmtpPermission (true);
161 else if (access
> sp
.access
)
162 return new SmtpPermission (access
);
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
)
179 SmtpPermission sp
= (target
as SmtpPermission
);
181 PermissionHelper
.ThrowInvalidPermission (target
, typeof (SmtpPermission
));