GetElement() public method

public GetElement ( int position, bool bCreate ) : SecurityElement
position int
bCreate bool
return SecurityElement
Exemplo n.º 1
0
        internal virtual void FromXml( SecurityDocument doc, int position, bool allowInternalOnly )
        {
            if (doc == null)
                throw new ArgumentNullException("doc");
            Contract.EndContractBlock();
            
            if (!doc.GetTagForElement( position ).Equals(s_str_PermissionSet))
                throw new ArgumentException(String.Format( null, Environment.GetResourceString( "Argument_InvalidXMLElement" ), "PermissionSet", this.GetType().FullName) );
            
            Reset();
            m_allPermissionsDecoded = false;
            Exception savedException = null;
            String strUnrestricted = doc.GetAttributeForElement( position, "Unrestricted" );
            if (strUnrestricted != null)
                m_Unrestricted = strUnrestricted.Equals( "True" ) || strUnrestricted.Equals( "true" ) || strUnrestricted.Equals( "TRUE" );
            else
                m_Unrestricted = false;

            ArrayList childrenIndices = doc.GetChildrenPositionForElement( position );
            int childCount = childrenIndices.Count;
            for (int i = 0; i < childCount; ++i)
            {
                int childIndex = (int)childrenIndices[i];
                if (IsPermissionTag( doc.GetTagForElement( childIndex ), allowInternalOnly ))
                {
                    try
                    {
                        String className = doc.GetAttributeForElement( childIndex, "class" );

                        PermissionToken token;
                        Object objectToInsert;
                        
                        if (className != null)
                        {
                            token = PermissionToken.GetToken( className );
                            if (token == null)
                            {
                                objectToInsert = CreatePerm( doc.GetElement( childIndex, true ) );

                                if (objectToInsert != null)
                                {
#if _DEBUG
                                    PermissionToken tokenDebug = PermissionToken.GetToken( (IPermission)objectToInsert );
                                    Contract.Assert((tokenDebug != null), "PermissionToken.GetToken returned null ");
                                    Contract.Assert( (tokenDebug.m_type & PermissionTokenType.BuiltIn) != 0, "This should only be called for built-ins" );
#endif
                                    Contract.Assert( objectToInsert.GetType().Module.Assembly == System.Reflection.Assembly.GetExecutingAssembly(),
                                        "PermissionToken.GetToken returned null for non-mscorlib permission" );
                                    token = PermissionToken.GetToken( (IPermission)objectToInsert );
                                    Contract.Assert((token != null), "PermissionToken.GetToken returned null ");
                                    Contract.Assert( (token.m_type & PermissionTokenType.DontKnow) == 0, "We should always know the permission type when getting a token from an instance" );
                                }
                            }
                            else
                            {
                                objectToInsert = ((ISecurityElementFactory)new SecurityDocumentElement(doc, childIndex)).CreateSecurityElement();
                            }
                        }
                        else
                        {
                            IPermission ip = CreatePerm( doc.GetElement( childIndex, true ) );
                            if (ip == null)
                            {
                                token = null;
                                objectToInsert = null;
                            }
                            else
                            {
                                token = PermissionToken.GetToken( ip );
                                Contract.Assert( PermissionToken.IsTokenProperlyAssigned( ip, token ),
                                                 "PermissionToken was improperly assigned" );
                                objectToInsert = ip;
                            }
                        }

                        if (token != null && objectToInsert != null)
                        {
                            if (m_permSet == null)
                                m_permSet = new TokenBasedSet();

                            IPermission permInSlot = null;
                            if (this.m_permSet.GetItem( token.m_index ) != null)
                            {
                                // If there is already something in that slot, let's union them
                                // together.
                                
                                if (this.m_permSet.GetItem( token.m_index ) is IPermission)
                                    permInSlot = (IPermission)this.m_permSet.GetItem( token.m_index );
                                else
                                    permInSlot = CreatePerm( this.m_permSet.GetItem( token.m_index ) );
                            }

                            if (permInSlot != null)
                            {
                                if (objectToInsert is IPermission)
                                    objectToInsert = permInSlot.Union((IPermission)objectToInsert);
                                else
                                    objectToInsert = permInSlot.Union(CreatePerm( objectToInsert ));
                            }

                            if(m_Unrestricted && objectToInsert is IPermission)
                                objectToInsert = null;

                            this.m_permSet.SetItem( token.m_index, objectToInsert );
                        }
                    }
                    catch (Exception e)
                    {
#if _DEBUG
                        if (debug)
                            DEBUG_WRITE( "error while decoding permission set =\n" + e.ToString() );
#endif
                        if (savedException == null)
                            savedException = e;

                    }
                }
            }

            if (savedException != null)
                throw savedException;
                
        }
 SecurityElement ISecurityElementFactory.CreateSecurityElement()
 {
     return(m_document.GetElement(m_position, true));
 }
 internal virtual void FromXml(SecurityDocument doc, int position, bool allowInternalOnly)
 {
     if (doc == null)
     {
         throw new ArgumentNullException("doc");
     }
     if (!doc.GetTagForElement(position).Equals("PermissionSet"))
     {
         throw new ArgumentException(string.Format(null, Environment.GetResourceString("Argument_InvalidXMLElement"), new object[] { "PermissionSet", base.GetType().FullName }));
     }
     this.Reset();
     this.m_allPermissionsDecoded = false;
     Exception exception = null;
     string attributeForElement = doc.GetAttributeForElement(position, "Unrestricted");
     if (attributeForElement != null)
     {
         this.m_Unrestricted = (attributeForElement.Equals("True") || attributeForElement.Equals("true")) || attributeForElement.Equals("TRUE");
     }
     else
     {
         this.m_Unrestricted = false;
     }
     ArrayList childrenPositionForElement = doc.GetChildrenPositionForElement(position);
     int count = childrenPositionForElement.Count;
     for (int i = 0; i < count; i++)
     {
         int num3 = (int) childrenPositionForElement[i];
         if (IsPermissionTag(doc.GetTagForElement(num3), allowInternalOnly))
         {
             try
             {
                 PermissionToken token;
                 object obj2;
                 string typeStr = doc.GetAttributeForElement(num3, "class");
                 if (typeStr != null)
                 {
                     token = PermissionToken.GetToken(typeStr);
                     if (token == null)
                     {
                         obj2 = this.CreatePerm(doc.GetElement(num3, true));
                         if (obj2 != null)
                         {
                             token = PermissionToken.GetToken((IPermission) obj2);
                         }
                     }
                     else
                     {
                         obj2 = ((ISecurityElementFactory) new SecurityDocumentElement(doc, num3)).CreateSecurityElement();
                     }
                 }
                 else
                 {
                     IPermission perm = this.CreatePerm(doc.GetElement(num3, true));
                     if (perm == null)
                     {
                         token = null;
                         obj2 = null;
                     }
                     else
                     {
                         token = PermissionToken.GetToken(perm);
                         obj2 = perm;
                     }
                 }
                 if ((token != null) && (obj2 != null))
                 {
                     if (this.m_permSet == null)
                     {
                         this.m_permSet = new TokenBasedSet();
                     }
                     IPermission item = null;
                     if (this.m_permSet.GetItem(token.m_index) != null)
                     {
                         if (this.m_permSet.GetItem(token.m_index) is IPermission)
                         {
                             item = (IPermission) this.m_permSet.GetItem(token.m_index);
                         }
                         else
                         {
                             item = this.CreatePerm(this.m_permSet.GetItem(token.m_index));
                         }
                     }
                     if (item != null)
                     {
                         if (obj2 is IPermission)
                         {
                             obj2 = item.Union((IPermission) obj2);
                         }
                         else
                         {
                             obj2 = item.Union(this.CreatePerm(obj2));
                         }
                     }
                     if (this.m_Unrestricted && (obj2 is IPermission))
                     {
                         obj2 = null;
                     }
                     this.m_permSet.SetItem(token.m_index, obj2);
                 }
             }
             catch (Exception exception2)
             {
                 if (exception == null)
                 {
                     exception = exception2;
                 }
             }
         }
     }
     if (exception != null)
     {
         throw exception;
     }
 }