//
// Authors:
// Tim Coleman <tim@timcoleman.com>
-// Sebastien Pouliot <spouliot@motus.com>
+// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2002, Tim Coleman
// Portions Copyright (C) 2003 Motus Technologies (http://www.motus.com)
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-using System;
using System.Collections;
-using System.Security.Permissions;
+using System.Runtime.InteropServices;
using System.Text;
-
-namespace System.Security.Permissions {
-
+\r
+namespace System.Security.Permissions {\r
+\r
+#if NET_2_0
+ [ComVisible (true)]
+#endif
[Serializable]
public sealed class EnvironmentPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
#region Fields
+ private const int version = 1;
+
EnvironmentPermissionAccess flags;
PermissionState _state;
ArrayList readList;
public EnvironmentPermission (PermissionState state) : base ()
{
- switch (state) {
- case PermissionState.None:
- case PermissionState.Unrestricted:
- _state = state;
- readList = new ArrayList ();
- writeList = new ArrayList ();
- break;
- default:
- throw new ArgumentException ("Invalid PermissionState", "state");
- }
+ _state = CheckPermissionState (state, true);
+ readList = new ArrayList ();
+ writeList = new ArrayList ();
}
public EnvironmentPermission (EnvironmentPermissionAccess flag, string pathList) : base ()
{
if (pathList == null)
throw new ArgumentNullException ("pathList");
+
string[] paths;
switch (flag) {
case EnvironmentPermissionAccess.AllAccess:
}
break;
default:
- throw new ArgumentException ("Invalid EnvironmentPermissionAccess", "flag");
+ ThrowInvalidFlag (flag, false);
+ break;
}
}
public override void FromXml (SecurityElement esd)
{
- if (esd == null)
- throw new ArgumentNullException ("esd");
- if (esd.Tag != "IPermission")
- throw new ArgumentException ("not IPermission");
- if (!(esd.Attributes ["class"] as string).StartsWith ("System.Security.Permissions.EnvironmentPermission"))
- throw new ArgumentException ("not EnvironmentPermission");
- if ((esd.Attributes ["version"] as string) != "1")
- throw new ArgumentException ("wrong version");
-
- string read = (esd.Attributes ["Read"] as string);
+ // General validation in CodeAccessPermission
+ CheckSecurityElement (esd, "esd", version, version);
+ // Note: we do not (yet) care about the return value
+ // as we only accept version 1 (min/max values)
+
+ if (IsUnrestricted (esd))
+ _state = PermissionState.Unrestricted;
+
+ string read = esd.Attribute ("Read");
if ((read != null) && (read.Length > 0))
SetPathList (EnvironmentPermissionAccess.Read, read);
- string write = (esd.Attributes ["Write"] as string);
+
+ string write = esd.Attribute ("Write");
if ((write != null) && (write.Length > 0))
SetPathList (EnvironmentPermissionAccess.Write, write);
-
- // Unrestricted ???
}
public string GetPathList (EnvironmentPermissionAccess flag)
{
- StringBuilder sb = new StringBuilder ();
switch (flag) {
case EnvironmentPermissionAccess.AllAccess:
case EnvironmentPermissionAccess.NoAccess:
- throw new ArgumentException ("Invalid EnvironmentPermissionAccess in context", "flag");
- case EnvironmentPermissionAccess.Read:
- foreach (string path in readList) {
- sb.Append (path);
- sb.Append (";");
- }
+ ThrowInvalidFlag (flag, true);
break;
+ case EnvironmentPermissionAccess.Read:
+ return GetPathList (readList);
case EnvironmentPermissionAccess.Write:
- foreach (string path in writeList) {
- sb.Append (path);
- sb.Append (";");
- }
- break;
+ return GetPathList (writeList);
default:
- throw new ArgumentException ("Unknown EnvironmentPermissionAccess", "flag");
+ ThrowInvalidFlag (flag, false);
+ break;
}
- string result = sb.ToString ();
- // remove last ';'
- int n = result.Length;
- if (n > 0)
- return result.Substring (0, n - 1);
- return ((_state == PermissionState.Unrestricted) ? String.Empty : null);
+ return null; // never reached
}
public override IPermission Intersect (IPermission target)
{
- if (target == null)
+ EnvironmentPermission ep = Cast (target);
+ if (ep == null)
return null;
- if (! (target is EnvironmentPermission))
- throw new ArgumentException ("wrong type");
- EnvironmentPermission o = (EnvironmentPermission) target;
- int n = 0;
if (IsUnrestricted ())
- return o.Copy ();
- if (o.IsUnrestricted ())
+ return ep.Copy ();
+ if (ep.IsUnrestricted ())
return Copy ();
- EnvironmentPermission ep = new EnvironmentPermission (PermissionState.None);
- string readTarget = o.GetPathList (EnvironmentPermissionAccess.Read);
+ int n = 0;
+ EnvironmentPermission result = new EnvironmentPermission (PermissionState.None);
+ string readTarget = ep.GetPathList (EnvironmentPermissionAccess.Read);
if (readTarget != null) {
string[] targets = readTarget.Split (';');
foreach (string t in targets) {
if (readList.Contains (t)) {
- ep.AddPathList (EnvironmentPermissionAccess.Read, t);
+ result.AddPathList (EnvironmentPermissionAccess.Read, t);
n++;
}
}
}
- string writeTarget = o.GetPathList (EnvironmentPermissionAccess.Write);
+ string writeTarget = ep.GetPathList (EnvironmentPermissionAccess.Write);
if (writeTarget != null) {
string[] targets = writeTarget.Split (';');
foreach (string t in targets) {
if (writeList.Contains (t)) {
- ep.AddPathList (EnvironmentPermissionAccess.Write, t);
+ result.AddPathList (EnvironmentPermissionAccess.Write, t);
n++;
}
}
}
- return ((n > 0) ? ep : null);
+ return ((n > 0) ? result : null);
}
public override bool IsSubsetOf (IPermission target)
{
- if (target == null)
+ EnvironmentPermission ep = Cast (target);
+ if (ep == null)
return false;
- if (! (target is EnvironmentPermission))
- throw new ArgumentException ("wrong type");
-
- EnvironmentPermission o = (EnvironmentPermission) target;
if (IsUnrestricted ())
- return o.IsUnrestricted ();
- else if (o.IsUnrestricted ())
+ return ep.IsUnrestricted ();
+ else if (ep.IsUnrestricted ())
return true;
foreach (string s in readList) {
- if (!o.readList.Contains (s))
+ if (!ep.readList.Contains (s))
return false;
}
foreach (string s in writeList) {
- if (!o.writeList.Contains (s))
+ if (!ep.writeList.Contains (s))
return false;
}
}
break;
default:
- throw new ArgumentException ("Invalid EnvironmentPermissionAccess", "flag");
+ ThrowInvalidFlag (flag, false);
+ break;
}
}
public override SecurityElement ToXml ()
{
- SecurityElement se = Element (this, 1);
+ SecurityElement se = Element (version);
+
if (_state == PermissionState.Unrestricted) {
se.AddAttribute ("Unrestricted", "true");
}
public override IPermission Union (IPermission other)
{
- if (other == null)
+ EnvironmentPermission ep = Cast (other);
+ if (ep == null)
return Copy ();
- if (! (other is EnvironmentPermission))
- throw new ArgumentException ("wrong type");
- EnvironmentPermission o = (EnvironmentPermission) other;
- if (IsUnrestricted () || o.IsUnrestricted ())
+ if (IsUnrestricted () || ep.IsUnrestricted ())
return new EnvironmentPermission (PermissionState.Unrestricted);
- EnvironmentPermission ep = (EnvironmentPermission) Copy ();
- string path = o.GetPathList (EnvironmentPermissionAccess.Read);
+ if (IsEmpty () && ep.IsEmpty ())
+ return null;
+
+ EnvironmentPermission result = (EnvironmentPermission) Copy ();
+ string path = ep.GetPathList (EnvironmentPermissionAccess.Read);
if (path != null)
- ep.AddPathList (EnvironmentPermissionAccess.Read, path);
- path = o.GetPathList (EnvironmentPermissionAccess.Write);
+ result.AddPathList (EnvironmentPermissionAccess.Read, path);
+ path = ep.GetPathList (EnvironmentPermissionAccess.Write);
if (path != null)
- ep.AddPathList (EnvironmentPermissionAccess.Write, path);
- return ep;
+ result.AddPathList (EnvironmentPermissionAccess.Write, path);
+ return result;
}
// IBuiltInPermission
int IBuiltInPermission.GetTokenIndex ()
{
- return 0;
+ return (int) BuiltInToken.Environment;
+ }
+
+ // helpers
+
+ private bool IsEmpty ()
+ {
+ return ((_state == PermissionState.None) && (readList.Count == 0) && (writeList.Count == 0));
+ }
+
+ private EnvironmentPermission Cast (IPermission target)
+ {
+ if (target == null)
+ return null;
+
+ EnvironmentPermission ep = (target as EnvironmentPermission);
+ if (ep == null) {
+ ThrowInvalidPermission (target, typeof (EnvironmentPermission));
+ }
+
+ return ep;
+ }
+
+ internal void ThrowInvalidFlag (EnvironmentPermissionAccess flag, bool context)
+ {
+ string msg = null;
+ if (context)
+ msg = Locale.GetText ("Unknown flag '{0}'.");
+ else
+ msg = Locale.GetText ("Invalid flag '{0}' in this context.");
+ throw new ArgumentException (String.Format (msg, flag), "flag");
+ }
+
+ private string GetPathList (ArrayList list)
+ {
+ if (IsUnrestricted ())
+ return String.Empty;
+#if NET_2_0
+ if (list.Count == 0)
+ return String.Empty;
+#else
+ if (list.Count == 0)
+ return null;
+#endif
+ StringBuilder sb = new StringBuilder ();
+ foreach (string path in list) {
+ sb.Append (path);
+ sb.Append (";");
+ }
+
+ string result = sb.ToString ();
+ // remove last ';'
+ int n = result.Length;
+ if (n > 0)
+ return result.Substring (0, n - 1);
+#if NET_2_0
+ return String.Empty;
+#else
+ return ((_state == PermissionState.Unrestricted) ? String.Empty : null);
+#endif
}
#endregion // Methods