969e00a7affbfe48cee5954df2975e86801f4e78
[mono.git] / mcs / class / corlib / System.Security.Permissions / SecurityPermission.cs
1 //
2 // System.Security.Permissions.SecurityPermission.cs
3 //
4 // Authors:
5 //      Dan Lewis (dihlewis@yahoo.co.uk)
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2002
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Globalization;
37
38 namespace System.Security.Permissions {
39         
40         [Serializable]
41         public sealed class SecurityPermission :
42                 CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
43
44                 private SecurityPermissionFlag flags;
45
46                 // constructors
47
48                 public SecurityPermission (PermissionState state)
49                 {
50                         if (state == PermissionState.Unrestricted)
51                                 flags = SecurityPermissionFlag.AllFlags;
52                         else
53                                 flags = SecurityPermissionFlag.NoFlags;
54                 }
55
56                 public SecurityPermission (SecurityPermissionFlag flags) 
57                 {
58                         this.flags = flags;
59                 }
60
61                 public SecurityPermissionFlag Flags {
62                         get { return flags; }
63                         set { flags = value; }
64                 }
65
66                 public bool IsUnrestricted () 
67                 {
68                         return (flags == SecurityPermissionFlag.AllFlags);
69                 }
70
71                 public override IPermission Copy () 
72                 {
73                         return new SecurityPermission (flags);
74                 }
75
76                 internal SecurityPermission Cast (IPermission target) 
77                 {
78                         SecurityPermission perm = (target as SecurityPermission);
79                         if (perm == null)
80                                 throw new ArgumentException ("wrong type for target");
81                         return perm;
82                 }
83
84                 public override IPermission Intersect (IPermission target) 
85                 {
86                         if (target == null)
87                                 return null;
88
89                         SecurityPermission perm = Cast (target);
90                         if (this.IsUnrestricted () && perm.IsUnrestricted ())
91                                 return new SecurityPermission (PermissionState.Unrestricted);
92                         if (this.IsUnrestricted ())
93                                 return perm.Copy ();
94                         if (perm.IsUnrestricted ())
95                                 return this.Copy ();
96                         return new SecurityPermission (flags & perm.flags);
97                 }
98
99                 public override IPermission Union (IPermission target) 
100                 {
101                         if (target == null)
102                                 return this.Copy ();
103
104                         SecurityPermission perm = Cast (target);
105                         if (this.IsUnrestricted () || perm.IsUnrestricted ())
106                                 return new SecurityPermission (PermissionState.Unrestricted);
107                         
108                         return new SecurityPermission (flags | perm.flags);
109                 }
110
111                 public override bool IsSubsetOf (IPermission target) 
112                 {
113                         if (target == null) 
114                                 return (flags == SecurityPermissionFlag.NoFlags);
115
116                         SecurityPermission perm = Cast (target);
117                         if (perm.IsUnrestricted ())
118                                 return true;
119                         if (this.IsUnrestricted ())
120                                 return false;
121
122                         return ((flags & ~perm.flags) == 0);
123                 }
124
125                 public override void FromXml (SecurityElement e) 
126                 {
127                         if (e == null)
128                                 throw new ArgumentNullException (
129                                         Locale.GetText ("The argument is null."));
130                         
131                         if (e.Attribute ("class") != GetType ().AssemblyQualifiedName)
132                                 throw new ArgumentException (
133                                         Locale.GetText ("The argument is not valid"));
134
135                         if (e.Attribute ("version") != "1")
136                                 throw new ArgumentException (
137                                         Locale.GetText ("The argument is not valid"));
138
139                         flags = (SecurityPermissionFlag) Enum.Parse (
140                                 typeof (SecurityPermissionFlag), e.Attribute ("Flags"));
141                 }
142
143                 public override SecurityElement ToXml () 
144                 {
145                         SecurityElement e = new SecurityElement ("IPermission");
146                         e.AddAttribute ("class", GetType ().AssemblyQualifiedName);
147                         e.AddAttribute ("version", "1");
148
149                         e.AddAttribute ("Flags", flags.ToString ());
150
151                         return e;
152                 }
153
154                 // IBuiltInPermission
155                 int IBuiltInPermission.GetTokenIndex ()
156                 {
157                         return 6;
158                 }
159         }
160 }