Example #1
0
        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
            {
                bool isEmpty = !IsUnrestricted() && AllowedAccess.Count == 0;
                return(isEmpty);
            }
            XamlLoadPermission other = CastPermission(target, "target");

            if (other.IsUnrestricted())
            {
                return(true);
            }
            if (this.IsUnrestricted())
            {
                return(false);
            }

            foreach (XamlAccessLevel accessLevel in this.AllowedAccess)
            {
                if (!other.Includes(accessLevel))
                {
                    return(false);
                }
            }
            return(true);
        }
Example #2
0
        public override IPermission Union(IPermission other)
        {
            if (other == null)
            {
                return(this.Copy());
            }
            XamlLoadPermission permission = CastPermission(other, "other");

            if (this.IsUnrestricted() || permission.IsUnrestricted())
            {
                return(new XamlLoadPermission(PermissionState.Unrestricted));
            }
            List <XamlAccessLevel> allowedAccess = new List <XamlAccessLevel>(this.AllowedAccess);

            foreach (XamlAccessLevel level in permission.AllowedAccess)
            {
                if (!this.Includes(level))
                {
                    allowedAccess.Add(level);
                    if (level.PrivateAccessToTypeName != null)
                    {
                        for (int i = 0; i < allowedAccess.Count; i++)
                        {
                            if ((allowedAccess[i].PrivateAccessToTypeName == null) && (allowedAccess[i].AssemblyNameString == level.AssemblyNameString))
                            {
                                allowedAccess.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
            }
            return(new XamlLoadPermission(allowedAccess));
        }
Example #3
0
        public override IPermission Intersect(IPermission target)
        {
            if (target == null)
            {
                return(null);
            }
            XamlLoadPermission permission = CastPermission(target, "target");

            if (permission.IsUnrestricted())
            {
                return(this.Copy());
            }
            if (this.IsUnrestricted())
            {
                return(permission.Copy());
            }
            List <XamlAccessLevel> allowedAccess = new List <XamlAccessLevel>();

            foreach (XamlAccessLevel level in this.AllowedAccess)
            {
                if (permission.Includes(level))
                {
                    allowedAccess.Add(level);
                }
                else if (level.PrivateAccessToTypeName != null)
                {
                    XamlAccessLevel requestedAccess = level.AssemblyOnly();
                    if (permission.Includes(requestedAccess))
                    {
                        allowedAccess.Add(requestedAccess);
                    }
                }
            }
            return(new XamlLoadPermission(allowedAccess));
        }
Example #4
0
        public override IPermission Intersect(IPermission target)
        {
            if (target == null)
            {
                return(null);
            }
            XamlLoadPermission other = CastPermission(target, "target");

            if (other.IsUnrestricted())
            {
                return(this.Copy());
            }
            if (this.IsUnrestricted())
            {
                return(other.Copy());
            }

            List <XamlAccessLevel> result = new List <XamlAccessLevel>();

            // We could optimize this with a hash, but we don't expect people to be creating
            // large unions of access levels.
            foreach (XamlAccessLevel accessLevel in this.AllowedAccess)
            {
                // First try the full access level
                if (other.Includes(accessLevel))
                {
                    result.Add(accessLevel);
                }
                // Then try the assembly subset
                else if (accessLevel.PrivateAccessToTypeName != null)
                {
                    XamlAccessLevel assemblyAccess = accessLevel.AssemblyOnly();
                    if (other.Includes(assemblyAccess))
                    {
                        result.Add(assemblyAccess);
                    }
                }
            }
            return(new XamlLoadPermission(result));
        }
Example #5
0
        public override IPermission Union(IPermission other)
        {
            if (other == null)
            {
                return(this.Copy());
            }
            XamlLoadPermission xamlOther = CastPermission(other, "other");

            if (IsUnrestricted() || xamlOther.IsUnrestricted())
            {
                return(new XamlLoadPermission(PermissionState.Unrestricted));
            }

            List <XamlAccessLevel> mergedAccess = new List <XamlAccessLevel>(this.AllowedAccess);

            foreach (XamlAccessLevel accessLevel in xamlOther.AllowedAccess)
            {
                if (!this.Includes(accessLevel))
                {
                    mergedAccess.Add(accessLevel);
                    if (accessLevel.PrivateAccessToTypeName != null)
                    {
                        // If we have an entry for access to just the assembly of this type, it is now redundant
                        for (int i = 0; i < mergedAccess.Count; i++)
                        {
                            if (mergedAccess[i].PrivateAccessToTypeName == null &&
                                mergedAccess[i].AssemblyNameString == accessLevel.AssemblyNameString)
                            {
                                mergedAccess.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
            }
            return(new XamlLoadPermission(mergedAccess));
        }
Example #6
0
        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
            {
                return(!this.IsUnrestricted() && (this.AllowedAccess.Count == 0));
            }
            XamlLoadPermission permission = CastPermission(target, "target");

            if (!permission.IsUnrestricted())
            {
                if (this.IsUnrestricted())
                {
                    return(false);
                }
                foreach (XamlAccessLevel level in this.AllowedAccess)
                {
                    if (!permission.Includes(level))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }