Ejemplo n.º 1
0
        /// <summary>
        /// Invokes a list of functions with specified arguments.
        /// (Created to avoid code re-usage in the event-firers.)
        /// </summary>
        /// <param name="invocList">A list of functions to invoke.</param>
        /// <param name="injector">The injector data to inject.</param>
        /// <param name="fieldName">The field name of the data object to inject.</param>
        /// <param name="p">A Privelege Container of the user attempting to call the function.</param>
        /// <returns>A compilation of the returns from all the functions.</returns>
        private string[] InvocationTunnel(Delegate[] invocList, object injector, string fieldName, PrivelegeContainer p )
        {
            Type t = moduleProxy.ModuleInstance.GetType();
            FieldInfo injectorData = null;
            if ( injector != null ) {
                injectorData = t.GetField( fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetField | BindingFlags.Static );
                injectorData.SetValue( moduleProxy.ModuleInstance, injector );
            }
            FieldInfo returnString = t.GetField( "returns", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField | BindingFlags.Static | BindingFlags.SetField );

            //TODO: Evaluate if these locks were a good idea.
            lock ( moduleProxy.ModuleInstance ) {

                int n = invocList.Length;
                string[][] returns = new string[n][];

                for ( int i = 0; i < n; i++ ) {

                    bool continueWithCall = true;

                    if ( p == null || !p.HasPrivelege( Priveleges.SuperUser ) ) {

                        //Check method permissions, can we call it?
                        object[] privreq = invocList[i].Method.GetCustomAttributes( false );

                        for ( int j = 0; j < privreq.Length; j++ ) {

                            if ( continueWithCall && privreq[j].GetType().Equals( typeof( PrivelegeRequiredAttribute ) ) ) {
                                PrivelegeRequiredAttribute pra = (PrivelegeRequiredAttribute)privreq[j];
                                continueWithCall = p != null ? p.HasPrivelege( pra.Required ) : ( (ulong)pra.Required == 0 );
                            }
                            if ( continueWithCall && privreq[j].GetType().Equals( typeof( UserLevelRequiredAttribute ) ) ) {
                                UserLevelRequiredAttribute ura = (UserLevelRequiredAttribute)privreq[j];
                                continueWithCall = p != null ? p.NumericalLevel >= ura.Required : ( ura.Required == 0 );
                            }

                            if ( !continueWithCall )
                                break;
                        }

                    }

                    //If we can continue with the call. Let's DO IT!
                    if ( continueWithCall ) {
                        //Null the return string to prevent multiple spams.
                        returnString.SetValue( moduleProxy.ModuleInstance, null );
                        AppDomain.CurrentDomain.DoCallBack( (CrossAppDomainDelegate)invocList[i] );
                        returns[i] = (string[])returnString.GetValue( moduleProxy.ModuleInstance );
                    }
                }

                //TODO:
                //WARNING: Performance issues? Every single event ever needinfg to get unrwapped into a single
                //sized array. Maybe evaluate where the slowdowns are later on.
                List<string> flatreturn = new List<string>( returns.Length );

                for ( int i = 0; i < returns.Length; i++ )
                    if ( returns[i] != null )
                        flatreturn.AddRange( returns[i] );

                return flatreturn.ToArray();

            }
        }
Ejemplo n.º 2
0
        private string[][] InvocationTunnel(Delegate[] invocList, PrivelegeContainer p, params object[] args)
        {
            int n = invocList.Length;
            string[][] returns = new string[n][];
            for ( int i = 0; i < n; i++ ) {

                bool continueWithCall = true;

                Delegate invoc = invocList[i];

                if ( p == null || !p.HasPrivelege( Priveleges.SuperUser ) ) {

                    object[] privreq = invoc.Method.GetCustomAttributes( false );

                    for ( int j = 0; j < privreq.Length; j++ ) {
                        if ( continueWithCall && privreq[j].GetType().Equals( typeof( PrivelegeRequiredAttribute ) ) ) {
                            PrivelegeRequiredAttribute pra = (PrivelegeRequiredAttribute)privreq[j];
                            continueWithCall = p != null ? p.HasPrivelege( pra.Required ) : ( (ulong)pra.Required == 0 );
                        }
                        if ( continueWithCall && privreq[j].GetType().Equals( typeof( UserLevelRequiredAttribute ) ) ) {
                            UserLevelRequiredAttribute ura = (UserLevelRequiredAttribute)privreq[j];
                            continueWithCall = p != null ? p.NumericalLevel >= ura.Required : ( ura.Required == 0 );
                        }

                        if ( !continueWithCall )
                            break;
                    }

                }

                if ( continueWithCall )
                    returns[i] = (string[])invoc.Method.Invoke( invoc.Target, args );
                else {
                    returns[i] = null;
                }

            }
            return returns;
        }