2 // System.Security.NamedPermissionSet
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
35 namespace System.Security {
39 public sealed class NamedPermissionSet : PermissionSet {
42 private string description;
44 // for PolicyLevel (to avoid validation duplication)
45 internal NamedPermissionSet ()
50 public NamedPermissionSet (string name, PermissionSet permSet)
56 public NamedPermissionSet (string name, PermissionState state)
62 public NamedPermissionSet (NamedPermissionSet permSet)
65 name = permSet.name; // name can be null here
66 description = permSet.description;
69 public NamedPermissionSet (string name)
70 : this (name, PermissionState.Unrestricted)
76 public string Description {
77 get { return description; }
78 set { description = value; }
84 if ((value == null) || (value == String.Empty)) {
85 throw new ArgumentException (Locale.GetText ("invalid name"));
93 public override PermissionSet Copy ()
95 return new NamedPermissionSet (this);
98 public NamedPermissionSet Copy (string name)
100 NamedPermissionSet nps = new NamedPermissionSet (this);
101 nps.Name = name; // get the new name
105 public override void FromXml (SecurityElement et)
108 // strangely it can import a null Name (bypassing property setter)
109 name = et.Attribute ("Name");
110 description = et.Attribute ("Description");
111 if (description == null)
112 description = String.Empty;
115 public override SecurityElement ToXml ()
117 SecurityElement se = base.ToXml ();
119 se.AddAttribute ("Name", name);
120 if (description != null)
121 se.AddAttribute ("Description", description);
126 public override bool Equals (object obj)
130 NamedPermissionSet nps = (obj as NamedPermissionSet);
133 // description isn't part of the comparaison
134 return ((name == nps.Name) && base.Equals (obj));
138 public override int GetHashCode ()
140 int hc = base.GetHashCode ();
141 // name is part of the hash code (except when null)
143 hc ^= name.GetHashCode ();
144 // description is never part of the hash code