Archives

Archives / 2003 / April
  • Writing regex that doesn't get you beat up in the parking lot after work.

    I have recently become enlightened in the ways of regex. Regex has really allowed me to do some things that would have been very difficult to do with just string manipulation. But alas, I am gaining a reputation for churning out incomprehensible code because of the regex that is involved. There is a great article over on the Show Us Your Code site that reminded me that sometimes its not cool to "do it in one line of code".

  • Ahh the good ol' days.

    One of the very first computers I actually owned myself was a timex sinclair. I remember the days of writing a hex loader in basic so that I could hand POKE some assembly into the darn thing. Maybe in C# 2.0 they can add a poke keyword! bwahaha. Check out this great zx-spectrum emulator and let the nastalgia flow.

  • Authorization Extender

    [ProvideProperty("Roles", typeof(Control))]
     [DesignerSerializer(typeof(AuthExtenderCodeDomSerializer), typeof(CodeDomSerializer))]
     public class AuthExtender : System.ComponentModel.Component, IExtenderProvider
     {
      #region IExtenderProvider Members

      public bool CanExtend(object extendee)
      {
       if (extendee is AuthExtender || extendee is Page)
        return false;
       return true;
      }

      #endregion

      internal Hashtable _roles = new Hashtable();
      public string GetRoles(Control c)
      {
       return _roles[c] as string;
      }

      public void SetRoles(Control c, string roles)
      {
       _roles[c] = roles;
       c.PreRender += new EventHandler(Control_OnPreRender);
      }

      private void Control_OnPreRender(object sender, EventArgs e)
      {
       Control c = sender as Control;
       if (c.Visible)
       {
        string[] roles = ((string)_roles[c]).Split(',');
        c.Visible = false;
        foreach (string role in roles)
        {
         if (HttpContext.Current.User.IsInRole(role))
         {
          c.Visible = true;
          break;
         }
        }
       }
      }
     }


     internal class AuthExtenderCodeDomSerializer : CodeDomSerializer
     {
      public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
      {
       // This is how we associate the component with the serializer.
       CodeDomSerializer baseClassSerializer =
        (CodeDomSerializer)manager.GetSerializer(
         typeof(AuthExtender).BaseType, typeof(CodeDomSerializer));

       object root = baseClassSerializer.Deserialize(manager, codeObject);

       if (codeObject is CodeStatementCollection)
       {
        CodeStatementCollection statements = (CodeStatementCollection)codeObject;

        foreach (CodeStatement statement in statements)
        {
         // if the statement is an expression it should be
         // the SetRoles method invoke expression
         if (statement is CodeExpressionStatement)
         {
          base.DeserializeStatement(manager, statement);
         }
        }
       }

       return root;
      }
     
      public override object Serialize(IDesignerSerializationManager manager, object value)
      {
       CodeDomSerializer baseClassSerializer =
        (CodeDomSerializer)manager.GetSerializer(
         typeof(AuthExtender).BaseType, typeof(CodeDomSerializer));
     
       object codeObject = baseClassSerializer.Serialize(manager, value);
       if (codeObject is CodeStatementCollection)
       {
        CodeStatementCollection statements = (CodeStatementCollection)codeObject;
     
        string commentText = "Setup the authorization for the controls on the form.";
        CodeCommentStatement comment = new CodeCommentStatement(commentText);
        statements.Add(comment);

        AuthExtender ext = value as AuthExtender;
        foreach (Control c in ext._roles.Keys)
        {
         string roles = ext._roles[c] as string;
         if (roles != null && roles != "")
         {
          CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression(
           // targetObject that contains the method to invoke.
           new CodePropertyReferenceExpression(
           new CodeThisReferenceBLOCKED EXPRESSION,
           manager.GetName(ext)
           ),
           // methodName indicates the method to invoke.
           "SetRoles",
           // parameters array contains the parameters for the method.
           new CodeExpression[]
           {
            new CodePropertyReferenceExpression(
             new CodeThisReferenceBLOCKED EXPRESSION,
             c.ID
            ),
            new CodePrimitiveExpression(
             ext._roles[c]
            )
           }
          );
          statements.Add(methodInvoke);
         }
        }
       }
       return codeObject;
      }
     }

  • Programmer Haiku

    Here's some programmer/general geeky haiku. Most of this can be attributed to my friend Ed Johnson:

  • New kid on the blog.

    Well, I'm still a n00b to the blogging community. I've wanted to get into it for a while now but just havn't found the time to get it all set up. Thanks to some prodding and general ridicule by my friends (Lance and Bob) I've finally got set up.