2004-08-19 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Permissions / UrlIdentityPermission.cs
1 //
2 // System.Security.Permissions.UrlIdentityPermission.cs
3 //
4 // Author
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2003 Motus Technologies. http://www.motus.com
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31
32 namespace System.Security.Permissions {
33
34         [Serializable]
35         public sealed class UrlIdentityPermission : CodeAccessPermission, IBuiltInPermission {
36
37                 private const int version = 1;
38
39                 private string url;
40
41                 public UrlIdentityPermission (PermissionState state) : base ()
42                 {
43                         // false == do not allow Unrestricted for Identity Permissions
44                         CheckPermissionState (state, false);
45                 }
46
47                 public UrlIdentityPermission (string site) : base ()
48                 {
49                         if (site == null)
50                                 throw new ArgumentNullException ("site");
51                         url = site;
52                 }
53
54                 public string Url { 
55                         get { 
56                                 if (url == null)
57                                         throw new NullReferenceException ("Url");
58                                 return url; 
59                         }
60                         set { url = value; }
61                 }
62
63                 public override IPermission Copy () 
64                 {
65                         return new UrlIdentityPermission (url);
66                 }
67
68                 public override void FromXml (SecurityElement esd)
69                 {
70                         // General validation in CodeAccessPermission
71                         CheckSecurityElement (esd, "esd", 1, 1);
72                         // Note: we do not (yet) care about the return value 
73                         // as we only accept version 1 (min/max values)
74
75                         url = esd.Attribute ("Url");
76                 }
77
78                 [MonoTODO]
79                 public override IPermission Intersect (IPermission target) 
80                 {
81                         // if one permission is null (object or url) then there's no intersection
82                         // if both are null then intersection is null
83                         UrlIdentityPermission uip = Cast (target);
84                         if ((uip == null) || (url == null))
85                                 return null;
86
87                         if (uip.Url == null)
88                                 return null;
89
90                         // TODO
91                         return null;
92                 }
93
94                 [MonoTODO]
95                 public override bool IsSubsetOf (IPermission target) 
96                 {
97                         return false;
98                 }
99
100                 public override SecurityElement ToXml () 
101                 {
102                         SecurityElement se = Element (version);
103                         se.AddAttribute ("Url", url);
104                         return se;
105                 }
106
107                 [MonoTODO]
108                 public override IPermission Union (IPermission target) 
109                 {
110                         return null;
111                 }
112
113                 // IBuiltInPermission
114                 int IBuiltInPermission.GetTokenIndex ()
115                 {
116                         return (int) BuiltInToken.UrlIdentity;
117                 }
118
119                 // helpers
120
121                 private UrlIdentityPermission Cast (IPermission target)
122                 {
123                         if (target == null)
124                                 return null;
125
126                         UrlIdentityPermission uip = (target as UrlIdentityPermission);
127                         if (uip == null) {
128                                 ThrowInvalidPermission (target, typeof (UrlIdentityPermission));
129                         }
130
131                         return uip;
132                 }
133         }
134 }