Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Experiments / Attic / Security / Castle.Services.Security / CustomPermission.cs
blob0a29049f747bffd8a0d209b304ed22dd7f12d3b0
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Services.Security
17 using System;
18 using System.Security;
19 using System.Security.Permissions;
20 using System.Security.Principal;
21 using System.Threading;
23 [Serializable]
24 public sealed class CustomPermission : IPermission, ISecurityEncodable
26 private string permissionName;
28 public CustomPermission(String permissionName)
30 this.permissionName = permissionName;
33 public CustomPermission(PermissionState state)
37 #region IPermission implementation
39 public IPermission Copy()
41 return new CustomPermission(permissionName);
44 public IPermission Intersect(IPermission target)
46 if (target == null)
48 return null;
51 CustomPermission other = target as CustomPermission;
53 if (other == null)
55 throw new ArgumentException("Wrong type specified. Expecting CustomPermission", "target");
58 if (other.permissionName.Equals(permissionName))
60 return new CustomPermission(permissionName);
63 return null;
66 public IPermission Union(IPermission target)
68 return Copy();
71 public bool IsSubsetOf(IPermission target)
73 if (target == null)
75 return false;
78 CustomPermission other = target as CustomPermission;
80 if (other == null)
82 throw new ArgumentException("Wrong type specified. Expecting CustomPermission", "target");
85 return (other.permissionName.Equals(permissionName));
88 public void Demand()
90 IPrincipal principal = Thread.CurrentPrincipal;
92 IExtendedPrincipal extendedPrincipal = principal as IExtendedPrincipal;
94 if (extendedPrincipal == null)
96 throw new SecurityException("The current principal does not implement IExtendedPrincipal");
99 if (!extendedPrincipal.HasPermission(permissionName))
101 throw new SecurityException("Current principal does not have permission " + permissionName);
105 #endregion
107 #region ISecurityEncodable implementation
109 public SecurityElement ToXml()
111 SecurityElement elem = new SecurityElement("IPermission");
113 elem.AddAttribute("class",
114 String.Format("{0}, {1}",
115 typeof(CustomPermission).FullName, typeof(CustomPermission).Assembly.FullName ));
116 elem.AddAttribute("version", "1");
117 elem.AddAttribute("Unrestricted", "false");
119 if (permissionName != null)
121 elem.AddAttribute("permName", permissionName);
124 return elem;
127 public void FromXml(SecurityElement elem)
129 object permName = elem.Attributes["permName"];
131 if (permName != null)
133 permissionName = permName.ToString();
137 #endregion