dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Security/Permissions E


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/runtime/System/Security/Permissions EnvironmentPermission.cs,NONE,1.1EnvironmentPermissionAccess.cs,NONE,1.1 FileIOPermission.cs,NONE,1.1 FileIOPermissionAccess.cs,NONE,1.1 IUnrestrictedPermission.cs,NONE,1.1ReflectionPermission.cs,NONE,1.1 EnvironmentPermissionAttribute.cs,1.1,1.2 FileIOPermissionAttribute.cs,1.1,1.2 ReflectionPermissionAttribute.cs,1.1,1.2SecurityPermission.cs,1.3,1.4
Date: Sun, 30 Mar 2003 20:00:40 -0500

Update of /cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security/Permissions
In directory subversions:/tmp/cvs-serv25703/runtime/System/Security/Permissions

Modified Files:
        EnvironmentPermissionAttribute.cs FileIOPermissionAttribute.cs 
        ReflectionPermissionAttribute.cs SecurityPermission.cs 
Added Files:
        EnvironmentPermission.cs EnvironmentPermissionAccess.cs 
        FileIOPermission.cs FileIOPermissionAccess.cs 
        IUnrestrictedPermission.cs ReflectionPermission.cs 
Log Message:


Implement the rest of the ECMA-compatible permissions classes.


--- NEW FILE ---
/*
 * EnvironmentPermission.cs - Implementation of the
 *              "System.Security.Permissions.EnvironmentPermission" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Security.Permissions
{

using System;
using System.IO;
using System.Collections;
using System.Security;

public sealed class EnvironmentPermission
        : CodeAccessPermission, IUnrestrictedPermission
{
        // Internal state.
        private PermissionState state;
        private String[] readList;
        private String[] writeList;

        // Constructors.
        public EnvironmentPermission(PermissionState state)
                        {
                                if(state != PermissionState.Unrestricted &&
                                   state != PermissionState.None)
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionState"));
                                }
                                this.state = state;
                        }
        public EnvironmentPermission(EnvironmentPermissionAccess flag,
                                                                 String 
pathList)
                        {
                                if(pathList == null)
                                {
                                        throw new 
ArgumentNullException("pathList");
                                }
                                if((flag & 
~(EnvironmentPermissionAccess.AllAccess)) != 0)
                                {
                                        throw new 
ArgumentException(_("Arg_EnvironmentAccess"));
                                }
                                this.state = PermissionState.None;
                                if((flag & EnvironmentPermissionAccess.Read) != 
0)
                                {
                                        readList = SplitPath(pathList, 
Path.PathSeparator);
                                }
                                if((flag & EnvironmentPermissionAccess.Write) 
!= 0)
                                {
                                        writeList = SplitPath(pathList, 
Path.PathSeparator);
                                }
                        }
        internal EnvironmentPermission(PermissionState state, String[] readList,
                                                                   String[] 
writeList)
                        {
                                this.state = state;
                                this.readList = readList;
                                this.writeList = writeList;
                        }

        // Split a path string into an array.
        internal static String[] SplitPath(String path, char separator)
                        {
                                if(path == null || path == String.Empty)
                                {
                                        return null;
                                }
                                if(separator == ';')
                                {
                                        return path.Split(';');
                                }
                                else
                                {
                                        // On Unix platforms, the caller might 
have constructed a
                                        // string using PathSeparator, or 
supplied an explicit
                                        // path string using ';'.  This will 
handle both cases.
                                        return path.Split(separator, ';');
                                }
                        }
        internal static String[] SplitPath(String path)
                        {
                                return SplitPath(path, Path.PathSeparator);
                        }

        // Create the intersection of two string lists.
        internal static String[] Intersect(String[] list1, String[] list2)
                        {
                                if(list1 == null || list2 == null)
                                {
                                        return null;
                                }
                                int count = 0;
                                foreach(String s1 in list1)
                                {
                                        if(((IList)list2).Contains(s1))
                                        {
                                                ++count;
                                        }
                                }
                                if(count == 0)
                                {
                                        return null;
                                }
                                String[] list = new String [count];
                                count = 0;
                                foreach(String s2 in list1)
                                {
                                        if(((IList)list2).Contains(s2))
                                        {
                                                list[count++] = s2;
                                        }
                                }
                                return list;
                        }

        // Determine if one string list is a subset of another.
        internal static bool IsSubsetOf(String[] list1, String[] list2)
                        {
                                if(list1 == null)
                                {
                                        return true;
                                }
                                else if(list2 == null)
                                {
                                        return false;
                                }
                                foreach(String s in list1)
                                {
                                        if(!((IList)list2).Contains(s))
                                        {
                                                return false;
                                        }
                                }
                                return true;
                        }

        // Create the union of two string lists.
        internal static String[] Union(String[] list1, String[] list2, bool 
clone)
                        {
                                if(list1 == null)
                                {
                                        if(list2 == null || !clone)
                                        {
                                                return list2;
                                        }
                                        else
                                        {
                                                return 
(String[])(list2.Clone());
                                        }
                                }
                                else if(list2 == null)
                                {
                                        return list1;
                                }
                                int count = list1.Length;
                                foreach(String s1 in list2)
                                {
                                        if(!((IList)list1).Contains(s1))
                                        {
                                                ++count;
                                        }
                                }
                                if(count == 0)
                                {
                                        return null;
                                }
                                String[] list = new String [count];
                                count = list1.Length;
                                Array.Copy(list1, 0, list, 0, count);
                                foreach(String s2 in list2)
                                {
                                        if(!((IList)list1).Contains(s2))
                                        {
                                                list[count++] = s2;
                                        }
                                }
                                return list;
                        }

        // Convert an XML value into a permissions value.
        public override void FromXml(SecurityElement esd)
                        {
                                String value;
                                if(esd == null)
                                {
                                        throw new ArgumentNullException("esd");
                                }
                                if(esd.Attribute("version") != "1")
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionVersion"));
                                }
                                value = esd.Attribute("Unrestricted");
                                if(value != null && Boolean.Parse(value))
                                {
                                        state = PermissionState.Unrestricted;
                                }
                                else
                                {
                                        state = PermissionState.None;
                                }
                                if(state != PermissionState.Unrestricted)
                                {
                                        readList = 
SplitPath(esd.Attribute("Read"), ';');
                                        writeList = 
SplitPath(esd.Attribute("Write"), ';');
                                }
                        }

        // Convert this permissions object into an XML value.
        public override SecurityElement ToXml()
                        {
                                SecurityElement element;
                                element = new SecurityElement("IPermission");
                                element.AddAttribute
                                        ("class",
                                         
SecurityElement.Escape(typeof(EnvironmentPermission).
                                                                                
        AssemblyQualifiedName));
                                element.AddAttribute("version", "1");
                                if(state == PermissionState.Unrestricted)
                                {
                                        element.AddAttribute("Unrestricted", 
"true");
                                }
                                else
                                {
                                        // Always use ";" as the separator so 
that we can
                                        // guarantee a fixed external form, 
regardless of
                                        // whatever PathSeparator is set to.
                                        if(readList != null)
                                        {
                                                element.AddAttribute
                                                        ("Read", 
String.Join(";", readList));
                                        }
                                        if(writeList != null)
                                        {
                                                element.AddAttribute
                                                        ("Write", 
String.Join(";", writeList));
                                        }
                                }
                                return element;
                        }

        // Implement the IPermission interface.
        public override IPermission Copy()
                        {
                                return new EnvironmentPermission(state, 
readList, writeList);
                        }
        public override IPermission Intersect(IPermission target)
                        {
                                // Handle the easy cases first.
                                if(target == null)
                                {
                                        return target;
                                }
                                else if(!(target is EnvironmentPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else 
if(((EnvironmentPermission)target).IsUnrestricted())
                                {
                                        return Copy();
                                }
                                else if(IsUnrestricted())
                                {
                                        return target.Copy();
                                }

                                // Create a new object and intersect the lists.
                                return new EnvironmentPermission
                                        (PermissionState.None,
                                         Intersect(readList,
                                                           
((EnvironmentPermission)target).readList),
                                         Intersect(writeList,
                                                           
((EnvironmentPermission)target).writeList));
                        }
        public override bool IsSubsetOf(IPermission target)
                        {
                                if(target == null)
                                {
                                        return (state == PermissionState.None &&
                                                        readList == null && 
writeList == null);
                                }
                                else if(!(target is EnvironmentPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else 
if(((EnvironmentPermission)target).IsUnrestricted())
                                {
                                        return true;
                                }
                                else if(IsUnrestricted())
                                {
                                        return false;
                                }
                                else
                                {
                                        return IsSubsetOf
                                                (readList, 
((EnvironmentPermission)target).readList) &&
                                                        IsSubsetOf
                                                (writeList, 
((EnvironmentPermission)target).writeList);
                                }
                        }
        public override IPermission Union(IPermission target)
                        {
                                if(target == null)
                                {
                                        return Copy();
                                }
                                else if(!(target is EnvironmentPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else if(IsUnrestricted() ||
                                        
((EnvironmentPermission)target).IsUnrestricted())
                                {
                                        return new EnvironmentPermission
                                                (PermissionState.Unrestricted);
                                }
                                else
                                {
                                        return new EnvironmentPermission
                                                (PermissionState.None,
                                                 Union(readList,
                                                           
((EnvironmentPermission)target).readList,
                                                           false),
                                                 Union(writeList,
                                                           
((EnvironmentPermission)target).writeList,
                                                           false));
                                }
                        }

        // Determine if this object has unrestricted permissions.
#if ECMA_COMPAT
        private bool IsUnrestricted()
                        {
                                return (state == PermissionState.Unrestricted);
                        }
        bool IUnrestrictedPermission.IsUnrestricted()
#else
        public bool IsUnrestricted()
#endif
                        {
                                return (state == PermissionState.Unrestricted);
                        }

#if !ECMA_COMPAT

        // Set the path list information.
        public void SetPathList(EnvironmentPermissionAccess flag, String 
pathList)
                        {
                                if(pathList == null)
                                {
                                        throw new 
ArgumentNullException("pathList");
                                }
                                if((flag & 
~(EnvironmentPermissionAccess.AllAccess)) != 0)
                                {
                                        throw new 
ArgumentException(_("Arg_EnvironmentAccess"));
                                }
                                if((flag & EnvironmentPermissionAccess.Read) != 
0)
                                {
                                        readList = SplitPath(pathList, 
Path.PathSeparator);
                                }
                                if((flag & EnvironmentPermissionAccess.Write) 
!= 0)
                                {
                                        writeList = SplitPath(pathList, 
Path.PathSeparator);
                                }
                        }

        // Add to the path list information.
        public void AddPathList(EnvironmentPermissionAccess flag, String 
pathList)
                        {
                                if(pathList == null)
                                {
                                        throw new 
ArgumentNullException("pathList");
                                }
                                if((flag & 
~(EnvironmentPermissionAccess.AllAccess)) != 0)
                                {
                                        throw new 
ArgumentException(_("Arg_EnvironmentAccess"));
                                }
                                if((flag & EnvironmentPermissionAccess.Read) != 
0)
                                {
                                        readList = Union(readList,
                                                SplitPath(pathList, 
Path.PathSeparator), false);
                                }
                                if((flag & EnvironmentPermissionAccess.Write) 
!= 0)
                                {
                                        writeList = Union(writeList,
                                                SplitPath(pathList, 
Path.PathSeparator), false);
                                }
                        }

        // Get a specific path list.
        public String GetPathList(EnvironmentPermissionAccess flag)
                        {
                                switch(flag)
                                {
                                        case EnvironmentPermissionAccess.Read:
                                        {
                                                if(readList != null)
                                                {
                                                        return String.Join
                                                                
(Path.PathSeparator.ToString(), readList);
                                                }
                                                else
                                                {
                                                        return String.Empty;
                                                }
                                        }
                                        // Not reached.

                                        case EnvironmentPermissionAccess.Write:
                                        {
                                                if(writeList != null)
                                                {
                                                        return String.Join
                                                                
(Path.PathSeparator.ToString(), writeList);
                                                }
                                                else
                                                {
                                                        return String.Empty;
                                                }
                                        }
                                        // Not reached.

                                        default:
                                        {
                                                throw new 
ArgumentException(_("Arg_EnvironmentAccess"));
                                        }
                                        // Not reached.
                                }
                        }

#endif // !ECMA_COMPAT

}; // class EnvironmentPermission

}; // namespace System.Security.Permissions

--- NEW FILE ---
/*
 * EnvironmentPermissionAccess.cs - Implementation of the
 *                      
"System.Security.Permissions.EnvironmentPermissionAccess" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Security.Permissions
{

[Flags]
public enum EnvironmentPermissionAccess
{

        NoAccess  = 0x0000,
        Read      = 0x0001,
        Write     = 0x0002,
        AllAccess = 0x0003

}; // enum EnvironmentPermissionAccess

}; // namespace System.Security.Permissions

--- NEW FILE ---
/*
 * FileIOPermission.cs - Implementation of the
 *              "System.Security.Permissions.FileIOPermission" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Security.Permissions
{

using System;
using System.IO;
using System.Collections;
using System.Security;

public sealed class FileIOPermission
        : CodeAccessPermission, IUnrestrictedPermission
{
        // Internal state.
        private PermissionState state;
        private String[] readList;
        private String[] writeList;
        private String[] appendList;
        private String[] discoveryList;
        private FileIOPermissionAccess allLocalFiles;
        private FileIOPermissionAccess allFiles;

        // Constructors.
        public FileIOPermission(PermissionState state)
                        {
                                if(state != PermissionState.Unrestricted &&
                                   state != PermissionState.None)
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionState"));
                                }
                                this.state = state;
                                if(state == PermissionState.Unrestricted)
                                {
                                        allLocalFiles = 
FileIOPermissionAccess.AllAccess;
                                        allFiles = 
FileIOPermissionAccess.AllAccess;
                                }
                                else
                                {
                                        allLocalFiles = 
FileIOPermissionAccess.NoAccess;
                                        allFiles = 
FileIOPermissionAccess.NoAccess;
                                }
                        }
        public FileIOPermission(FileIOPermissionAccess flag, String pathList)
                        {
                                if(pathList == null)
                                {
                                        throw new 
ArgumentNullException("pathList");
                                }
                                if((flag & ~(FileIOPermissionAccess.AllAccess)) 
!= 0)
                                {
                                        throw new 
ArgumentException(_("Arg_FileIOAccess"));
                                }
                                this.state = PermissionState.None;
                                String[] split = 
EnvironmentPermission.SplitPath(pathList);
                                if((flag & FileIOPermissionAccess.Read) != 0)
                                {
                                        readList = split;
                                }
                                if((flag & FileIOPermissionAccess.Write) != 0)
                                {
                                        writeList = split;
                                }
                                if((flag & FileIOPermissionAccess.Append) != 0)
                                {
                                        appendList = split;
                                }
                                if((flag & 
FileIOPermissionAccess.PathDiscovery) != 0)
                                {
                                        discoveryList = split;
                                }
                                allLocalFiles = FileIOPermissionAccess.NoAccess;
                                allFiles = FileIOPermissionAccess.NoAccess;
                        }
        internal FileIOPermission(PermissionState state, String[] readList,
                                                          String[] writeList, 
String[] appendList,
                                                          String[] 
discoveryList,
                                                          
FileIOPermissionAccess allLocalFiles,
                                                          
FileIOPermissionAccess allFiles)
                        {
                                this.state = state;
                                this.readList = readList;
                                this.writeList = writeList;
                                this.appendList = appendList;
                                this.discoveryList = discoveryList;
                                this.allLocalFiles = allLocalFiles;
                                this.allFiles = allFiles;
                        }

        // Convert an XML value into a permissions value.
        public override void FromXml(SecurityElement esd)
                        {
                                String value;
                                if(esd == null)
                                {
                                        throw new ArgumentNullException("esd");
                                }
                                if(esd.Attribute("version") != "1")
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionVersion"));
                                }
                                value = esd.Attribute("Unrestricted");
                                if(value != null && Boolean.Parse(value))
                                {
                                        state = PermissionState.Unrestricted;
                                }
                                else
                                {
                                        state = PermissionState.None;
                                }
                                if(state != PermissionState.Unrestricted)
                                {
                                        readList = 
EnvironmentPermission.SplitPath
                                                (esd.Attribute("Read"), ';');
                                        writeList = 
EnvironmentPermission.SplitPath
                                                (esd.Attribute("Write"), ';');
                                        appendList = 
EnvironmentPermission.SplitPath
                                                (esd.Attribute("Append"), ';');
                                        discoveryList = 
EnvironmentPermission.SplitPath
                                                
(esd.Attribute("PathDiscovery"), ';');
                                }
                        }

        // Convert this permissions object into an XML value.
        public override SecurityElement ToXml()
                        {
                                SecurityElement element;
                                element = new SecurityElement("IPermission");
                                element.AddAttribute
                                        ("class",
                                         
SecurityElement.Escape(typeof(FileIOPermission).
                                                                                
        AssemblyQualifiedName));
                                element.AddAttribute("version", "1");
                                if(state == PermissionState.Unrestricted)
                                {
                                        element.AddAttribute("Unrestricted", 
"true");
                                }
                                else
                                {
                                        // Always use ";" as the separator so 
that we can
                                        // guarantee a fixed external form, 
regardless of
                                        // whatever PathSeparator is set to.
                                        if(readList != null)
                                        {
                                                element.AddAttribute
                                                        ("Read", 
String.Join(";", readList));
                                        }
                                        if(writeList != null)
                                        {
                                                element.AddAttribute
                                                        ("Write", 
String.Join(";", writeList));
                                        }
                                        if(appendList != null)
                                        {
                                                element.AddAttribute
                                                        ("Append", 
String.Join(";", appendList));
                                        }
                                        if(discoveryList != null)
                                        {
                                                element.AddAttribute
                                                        ("PathDiscovery", 
String.Join(";", discoveryList));
                                        }
                                }
                                return element;
                        }

        // Implement the IPermission interface.
        public override IPermission Copy()
                        {
                                return new FileIOPermission
                                        (state, readList, writeList, 
appendList, discoveryList,
                                         allLocalFiles, allFiles);
                        }
        public override IPermission Intersect(IPermission target)
                        {
                                // Handle the easy cases first.
                                if(target == null)
                                {
                                        return target;
                                }
                                else if(!(target is FileIOPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else 
if(((FileIOPermission)target).IsUnrestricted())
                                {
                                        return Copy();
                                }
                                else if(IsUnrestricted())
                                {
                                        return target.Copy();
                                }

                                // Create a new object and intersect the lists.
                                return new FileIOPermission
                                        (PermissionState.None,
                                         
EnvironmentPermission.Intersect(readList,
                                                           
((FileIOPermission)target).readList),
                                         
EnvironmentPermission.Intersect(writeList,
                                                           
((FileIOPermission)target).writeList),
                                         
EnvironmentPermission.Intersect(appendList,
                                                           
((FileIOPermission)target).appendList),
                                         
EnvironmentPermission.Intersect(discoveryList,
                                                           
((FileIOPermission)target).discoveryList),
                                         allLocalFiles & 
((FileIOPermission)target).allLocalFiles,
                                         allFiles & 
((FileIOPermission)target).allFiles);
                        }
        public override bool IsSubsetOf(IPermission target)
                        {
                                if(target == null)
                                {
                                        return (state == PermissionState.None &&
                                                        readList == null && 
writeList == null);
                                }
                                else if(!(target is FileIOPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else 
if(((FileIOPermission)target).IsUnrestricted())
                                {
                                        return true;
                                }
                                else if(IsUnrestricted())
                                {
                                        return false;
                                }
                                else
                                {
                                        return EnvironmentPermission.IsSubsetOf
                                                (readList, 
((FileIOPermission)target).readList) &&
                                                        
EnvironmentPermission.IsSubsetOf
                                                (writeList, 
((FileIOPermission)target).writeList) &&
                                                        
EnvironmentPermission.IsSubsetOf
                                                (appendList, 
((FileIOPermission)target).appendList) &&
                                                        
EnvironmentPermission.IsSubsetOf
                                                (discoveryList,
                                                 
((FileIOPermission)target).discoveryList) &&
                                                ((allLocalFiles &
                                                        
((FileIOPermission)target).allLocalFiles) ==
                                                                allLocalFiles) 
&&
                                                ((allFiles &
                                                        
((FileIOPermission)target).allFiles) ==
                                                                allFiles);
                                }
                        }
        public override IPermission Union(IPermission target)
                        {
                                if(target == null)
                                {
                                        return Copy();
                                }
                                else if(!(target is FileIOPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else if(IsUnrestricted() ||
                                        
((FileIOPermission)target).IsUnrestricted())
                                {
                                        return new FileIOPermission
                                                (PermissionState.Unrestricted);
                                }
                                else
                                {
                                        return new FileIOPermission
                                                (PermissionState.None,
                                                 
EnvironmentPermission.Union(readList,
                                                           
((FileIOPermission)target).readList, false),
                                                 
EnvironmentPermission.Union(writeList,
                                                           
((FileIOPermission)target).writeList, false),
                                                 
EnvironmentPermission.Union(appendList,
                                                           
((FileIOPermission)target).appendList, false),
                                                 
EnvironmentPermission.Union(discoveryList,
                                                           
((FileIOPermission)target).discoveryList, false),
                                                 allLocalFiles |
                                                        
((FileIOPermission)target).allLocalFiles,
                                                 allFiles | 
((FileIOPermission)target).allFiles);
                                }
                        }

        // Determine if this object has unrestricted permissions.
#if ECMA_COMPAT
        private bool IsUnrestricted()
                        {
                                return (state == PermissionState.Unrestricted);
                        }
        bool IUnrestrictedPermission.IsUnrestricted()
#else
        public bool IsUnrestricted()
#endif
                        {
                                return (state == PermissionState.Unrestricted);
                        }

#if !ECMA_COMPAT

        // Clear specific path lists.
        private void ClearPathList(FileIOPermissionAccess access)
                        {
                                if((access & 
~(FileIOPermissionAccess.AllAccess)) != 0)
                                {
                                        throw new 
ArgumentException(_("Arg_FileIOAccess"));
                                }
                                if((access & FileIOPermissionAccess.Read) != 0)
                                {
                                        readList = null;
                                }
                                if((access & FileIOPermissionAccess.Write) != 0)
                                {
                                        writeList = null;
                                }
                                if((access & FileIOPermissionAccess.Append) != 
0)
                                {
                                        appendList = null;
                                }
                                if((access & 
FileIOPermissionAccess.PathDiscovery) != 0)
                                {
                                        discoveryList = null;
                                }
                        }

        // Set the path list information.
        public void SetPathList(FileIOPermissionAccess access, String path)
                        {
                                ClearPathList(access);
                                AddPathList(access, new String[] { path });
                        }
        public void SetPathList(FileIOPermissionAccess access, String[] 
pathList)
                        {
                                ClearPathList(access);
                                AddPathList(access, pathList);
                        }

        // Add to the path list information.
        public void AddPathList(FileIOPermissionAccess access, String path)
                        {
                                AddPathList(access, new String[] { path });
                        }
        public void AddPathList(FileIOPermissionAccess access, String[] 
pathList)
                        {
                                if(pathList == null)
                                {
                                        throw new 
ArgumentNullException("pathList");
                                }
                                if((access & 
~(FileIOPermissionAccess.AllAccess)) != 0)
                                {
                                        throw new 
ArgumentException(_("Arg_FileIOAccess"));
                                }
                                foreach(String s in pathList)
                                {
                                        if(s == null)
                                        {
                                                throw new 
ArgumentNullException("pathList element");
                                        }
                                }
                                if((access & FileIOPermissionAccess.Read) != 0)
                                {
                                        readList = EnvironmentPermission.Union
                                                (readList, pathList, true);
                                }
                                if((access & FileIOPermissionAccess.Write) != 0)
                                {
                                        writeList = EnvironmentPermission.Union
                                                (writeList, pathList, true);
                                }
                                if((access & FileIOPermissionAccess.Append) != 
0)
                                {
                                        appendList = EnvironmentPermission.Union
                                                (appendList, pathList, true);
                                }
                                if((access & 
FileIOPermissionAccess.PathDiscovery) != 0)
                                {
                                        discoveryList = 
EnvironmentPermission.Union
                                                (discoveryList, pathList, true);
                                }
                        }

        // Get a specific path list.
        public String[] GetPathList(FileIOPermissionAccess access)
                        {
                                switch(access)
                                {
                                        case FileIOPermissionAccess.Read:
                                        {
                                                if(readList != null)
                                                {
                                                        return 
(String[])(readList.Clone());
                                                }
                                                else
                                                {
                                                        return null;
                                                }
                                        }
                                        // Not reached.

                                        case FileIOPermissionAccess.Write:
                                        {
                                                if(writeList != null)
                                                {
                                                        return 
(String[])(writeList.Clone());
                                                }
                                                else
                                                {
                                                        return null;
                                                }
                                        }
                                        // Not reached.

                                        case FileIOPermissionAccess.Append:
                                        {
                                                if(appendList != null)
                                                {
                                                        return 
(String[])(appendList.Clone());
                                                }
                                                else
                                                {
                                                        return null;
                                                }
                                        }
                                        // Not reached.

                                        case 
FileIOPermissionAccess.PathDiscovery:
                                        {
                                                if(discoveryList != null)
                                                {
                                                        return 
(String[])(discoveryList.Clone());
                                                }
                                                else
                                                {
                                                        return null;
                                                }
                                        }
                                        // Not reached.

                                        default:
                                        {
                                                throw new 
ArgumentException(_("Arg_FileIOAccess"));
                                        }
                                        // Not reached.
                                }
                        }

        // Get or set the "all local files" flags.
        public FileIOPermissionAccess AllLocalFiles
                        {
                                get
                                {
                                        return allLocalFiles;
                                }
                                set
                                {
                                        if(state != 
PermissionState.Unrestricted)
                                        {
                                                allLocalFiles =
                                                        value & 
FileIOPermissionAccess.AllAccess;
                                        }
                                }
                        }

        // Get or set the "all files" flags.
        public FileIOPermissionAccess AllFiles
                        {
                                get
                                {
                                        return allFiles;
                                }
                                set
                                {
                                        if(state != 
PermissionState.Unrestricted)
                                        {
                                                allFiles = value & 
FileIOPermissionAccess.AllAccess;
                                        }
                                }
                        }

#endif // !ECMA_COMPAT

}; // class FileIOPermission

}; // namespace System.Security.Permissions

--- NEW FILE ---
/*
 * FileIOPermissionAccess.cs - Implementation of the
 *                      "System.Security.Permissions.FileIOPermissionAccess" 
class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Security.Permissions
{

[Flags]
public enum FileIOPermissionAccess
{

        NoAccess      = 0x0000,
        Read          = 0x0001,
        Write         = 0x0002,
        Append        = 0x0004,
        PathDiscovery = 0x0008,
        AllAccess     = 0x000F

}; // enum FileIOPermissionAccess

}; // namespace System.Security.Permissions

--- NEW FILE ---
/*
 * IUnrestrictedPermission.cs - Implementation of the
 *              "System.Security.Permissions.IUnrestrictedPermission" interface.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Security.Permissions
{

using System;

#if ECMA_COMPAT
internal
#else
public
#endif
interface IUnrestrictedPermission
{

        // Determine if this permission is unrestricted.
        bool IsUnrestricted();

}; // interface IUnrestrictedPermission

}; // namespace System.Security.Permissions

--- NEW FILE ---
/*
 * ReflectionPermission.cs - Implementation of the
 *              "System.Security.Permissions.ReflectionPermission" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace System.Security.Permissions
{

using System;
using System.Security;

public sealed class ReflectionPermission
        : CodeAccessPermission, IUnrestrictedPermission
{
        // Internal state.
        private PermissionState state;
        private ReflectionPermissionFlag flags;

        // Constructor.
        public ReflectionPermission(PermissionState state)
                        {
                                this.state = state;
                        }
        public ReflectionPermission(ReflectionPermissionFlag flags)
                        {
                                this.flags = flags;
                        }

        // Convert an XML value into a permissions value.
        public override void FromXml(SecurityElement esd)
                        {
                                String value;
                                if(esd == null)
                                {
                                        throw new ArgumentNullException("esd");
                                }
                                if(esd.Attribute("version") != "1")
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionVersion"));
                                }
                                value = esd.Attribute("Unrestricted");
                                if(value != null && Boolean.Parse(value))
                                {
                                        state = PermissionState.Unrestricted;
                                }
                                else
                                {
                                        state = PermissionState.None;
                                }
                                value = esd.Attribute("Flags");
                                if(value != null)
                                {
                                        flags = (ReflectionPermissionFlag)
                                                
Enum.Parse(typeof(ReflectionPermissionFlag), value);
                                }
                                else
                                {
                                        flags = 
ReflectionPermissionFlag.NoFlags;
                                }
                        }

        // Convert this permissions object into an XML value.
        public override SecurityElement ToXml()
                        {
                                SecurityElement element;
                                element = new SecurityElement("IPermission");
                                element.AddAttribute
                                        ("class",
                                         
SecurityElement.Escape(typeof(ReflectionPermission).
                                                                                
        AssemblyQualifiedName));
                                element.AddAttribute("version", "1");
                                if(flags != ReflectionPermissionFlag.NoFlags)
                                {
                                        element.AddAttribute("Flags", 
flags.ToString());
                                }
                                else if(state == PermissionState.Unrestricted)
                                {
                                        element.AddAttribute("Unrestricted", 
"true");
                                }
                                return element;
                        }

        // Implement the IPermission interface.
        public override IPermission Copy()
                        {
                                if(flags != ReflectionPermissionFlag.NoFlags)
                                {
                                        return new ReflectionPermission(flags);
                                }
                                else
                                {
                                        return new ReflectionPermission(state);
                                }
                        }
        public override IPermission Intersect(IPermission target)
                        {
                                ReflectionPermissionFlag newFlags;
                                if(target == null)
                                {
                                        return target;
                                }
                                else if(!(target is ReflectionPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else 
if(((ReflectionPermission)target).IsUnrestricted())
                                {
                                        if(IsUnrestricted())
                                        {
                                                return Copy();
                                        }
                                        else
                                        {
                                                newFlags = flags;
                                        }
                                }
                                else if(IsUnrestricted())
                                {
                                        newFlags = 
((ReflectionPermission)target).flags;
                                }
                                else
                                {
                                        newFlags = 
((ReflectionPermission)target).flags & flags;
                                }
                                if(newFlags == 0)
                                {
                                        return null;
                                }
                                else
                                {
                                        return new 
ReflectionPermission(newFlags);
                                }
                        }
        public override bool IsSubsetOf(IPermission target)
                        {
                                if(target == null)
                                {
                                        return (flags == 
ReflectionPermissionFlag.NoFlags);
                                }
                                else if(!(target is ReflectionPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else 
if(((ReflectionPermission)target).IsUnrestricted())
                                {
                                        return true;
                                }
                                else if(IsUnrestricted())
                                {
                                        return false;
                                }
                                else
                                {
                                        return ((flags & 
~(((ReflectionPermission)target).flags))
                                                                == 0);
                                }
                        }
        public override IPermission Union(IPermission target)
                        {
                                if(target == null)
                                {
                                        return Copy();
                                }
                                else if(!(target is ReflectionPermission))
                                {
                                        throw new 
ArgumentException(_("Arg_PermissionMismatch"));
                                }
                                else if(IsUnrestricted() ||
                                        
((ReflectionPermission)target).IsUnrestricted())
                                {
                                        return new ReflectionPermission
                                                (PermissionState.Unrestricted);
                                }
                                else
                                {
                                        return new ReflectionPermission
                                                (flags | 
((ReflectionPermission)target).flags);
                                }
                        }

        // Determine if this object has unrestricted permissions.
#if ECMA_COMPAT
        private bool IsUnrestricted()
                        {
                                return (state == PermissionState.Unrestricted);
                        }
        bool IUnrestrictedPermission.IsUnrestricted()
#else
        public bool IsUnrestricted()
#endif
                        {
                                return (state == PermissionState.Unrestricted);
                        }

#if !ECMA_COMPAT

        // Get or set the flags on this permissions object.
        public ReflectionPermissionFlag Flags
                        {
                                get
                                {
                                        return flags;
                                }
                                set
                                {
                                        if((flags & 
~(ReflectionPermissionFlag.AllFlags)) != 0)
                                        {
                                                throw new 
ArgumentException(_("Arg_ReflectionFlag"));
                                        }
                                }
                        }

#endif // !ECMA_COMPAT

}; // class ReflectionPermission

}; // namespace System.Security.Permissions

Index: EnvironmentPermissionAttribute.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security/Permissions/EnvironmentPermissionAttribute.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** EnvironmentPermissionAttribute.cs   30 Mar 2003 11:58:44 -0000      1.1
--- EnvironmentPermissionAttribute.cs   31 Mar 2003 01:00:38 -0000      1.2
***************
*** 83,91 ****
  
        // Create a permission object that corresponds to this attribute.
-       [TODO]
        public override IPermission CreatePermission()
                        {
!                               // TODO
!                               return null;
                        }
  
--- 83,92 ----
  
        // Create a permission object that corresponds to this attribute.
        public override IPermission CreatePermission()
                        {
!                               return new EnvironmentPermission
!                                       (PermissionState.None,
!                                        EnvironmentPermission.SplitPath(read),
!                                        
EnvironmentPermission.SplitPath(write));
                        }
  

Index: FileIOPermissionAttribute.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security/Permissions/FileIOPermissionAttribute.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** FileIOPermissionAttribute.cs        30 Mar 2003 11:58:44 -0000      1.1
--- FileIOPermissionAttribute.cs        31 Mar 2003 01:00:38 -0000      1.2
***************
*** 112,120 ****
  
        // Create a permission object that corresponds to this attribute.
-       [TODO]
        public override IPermission CreatePermission()
                        {
!                               // TODO
!                               return null;
                        }
  
--- 112,125 ----
  
        // Create a permission object that corresponds to this attribute.
        public override IPermission CreatePermission()
                        {
!                               return new FileIOPermission
!                                       (PermissionState.None,
!                                        EnvironmentPermission.SplitPath(read),
!                                        EnvironmentPermission.SplitPath(write),
!                                        
EnvironmentPermission.SplitPath(append),
!                                        
EnvironmentPermission.SplitPath(pathDiscovery),
!                                        FileIOPermissionAccess.NoAccess,
!                                        FileIOPermissionAccess.NoAccess);
                        }
  

Index: ReflectionPermissionAttribute.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security/Permissions/ReflectionPermissionAttribute.cs,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** ReflectionPermissionAttribute.cs    30 Mar 2003 11:58:44 -0000      1.1
--- ReflectionPermissionAttribute.cs    31 Mar 2003 01:00:38 -0000      1.2
***************
*** 121,129 ****
  
        // Create a permission object that corresponds to this attribute.
-       [TODO]
        public override IPermission CreatePermission()
                        {
!                               // TODO
!                               return null;
                        }
  
--- 121,127 ----
  
        // Create a permission object that corresponds to this attribute.
        public override IPermission CreatePermission()
                        {
!                               return new ReflectionPermission(flags);
                        }
  

Index: SecurityPermission.cs
===================================================================
RCS file: 
/cvsroot/dotgnu-pnet/pnetlib/runtime/System/Security/Permissions/SecurityPermission.cs,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** SecurityPermission.cs       30 Mar 2003 11:58:44 -0000      1.3
--- SecurityPermission.cs       31 Mar 2003 01:00:38 -0000      1.4
***************
*** 1,7 ****
  /*
   * SecurityPermission.cs - Implementation of the
!  *            "System.Security.SecurityPermission" class.
   *
!  * Copyright (C) 2001  Southern Storm Software, Pty Ltd.
   *
   * This program is free software; you can redistribute it and/or modify
--- 1,7 ----
  /*
   * SecurityPermission.cs - Implementation of the
!  *            "System.Security.Permissions.SecurityPermission" class.
   *
!  * Copyright (C) 2001, 2003  Southern Storm Software, Pty Ltd.
   *
   * This program is free software; you can redistribute it and/or modify
***************
*** 26,30 ****
  using System.Security;
  
! public sealed class SecurityPermission : CodeAccessPermission
  {
        // Internal state.
--- 26,31 ----
  using System.Security;
  
! public sealed class SecurityPermission
!       : CodeAccessPermission, IUnrestrictedPermission
  {
        // Internal state.
***************
*** 196,207 ****
        // Determine if this object has unrestricted permissions.
  #if ECMA_COMPAT
!       internal
  #else
!       public
  #endif
-       bool IsUnrestricted()
                        {
                                return (state == PermissionState.Unrestricted);
                        }
  
  }; // class SecurityPermission
--- 197,231 ----
        // Determine if this object has unrestricted permissions.
  #if ECMA_COMPAT
!       private bool IsUnrestricted()
!                       {
!                               return (state == PermissionState.Unrestricted);
!                       }
!       bool IUnrestrictedPermission.IsUnrestricted()
  #else
!       public bool IsUnrestricted()
  #endif
                        {
                                return (state == PermissionState.Unrestricted);
                        }
+ 
+ #if !ECMA_COMPAT
+ 
+       // Get or set the flags on this permissions object.
+       public SecurityPermissionFlag Flags
+                       {
+                               get
+                               {
+                                       return flags;
+                               }
+                               set
+                               {
+                                       if((flags & 
~(SecurityPermissionFlag.AllFlags)) != 0)
+                                       {
+                                               throw new 
ArgumentException(_("Arg_SecurityFlag"));
+                                       }
+                               }
+                       }
+ 
+ #endif // !ECMA_COMPAT
  
  }; // class SecurityPermission





reply via email to

[Prev in Thread] Current Thread [Next in Thread]