1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Security
;
19 using System
.Security
.Permissions
;
20 using System
.Security
.Principal
;
21 using System
.Threading
;
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
)
51 CustomPermission other
= target
as CustomPermission
;
55 throw new ArgumentException("Wrong type specified. Expecting CustomPermission", "target");
58 if (other
.permissionName
.Equals(permissionName
))
60 return new CustomPermission(permissionName
);
66 public IPermission
Union(IPermission target
)
71 public bool IsSubsetOf(IPermission target
)
78 CustomPermission other
= target
as CustomPermission
;
82 throw new ArgumentException("Wrong type specified. Expecting CustomPermission", "target");
85 return (other
.permissionName
.Equals(permissionName
));
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
);
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
);
127 public void FromXml(SecurityElement elem
)
129 object permName
= elem
.Attributes
["permName"];
131 if (permName
!= null)
133 permissionName
= permName
.ToString();