How to enable/disable controls against a user's permission via Type Casting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanndeb
    New Member
    • May 2010
    • 33

    How to enable/disable controls against a user's permission via Type Casting

    I want to enable/disable controls of a asp.net page against a logged in user's permission. say 'admin' & 'hr' can change user's birth date text-box in a page but others will see the text-box as disabled.

    so i wrote a function like this
    Code:
    [Flags]
        public enum UserRoles
        {
            GroupLead = 0x2,
            WebAdmin = 0x4,
            Developers = 0x8,
            Hr = 0x10,
            Accounts = 0x20,
            ItInfra= 0x40
        }
      
        public static class UserPermission
        {
            public static bool IsPorpertySupport(int empId, UserRoles[] uRoles)
            {
                // inner logics to return true or false acc. as the user is in the specified roles(s) or not.
            }
        }
    And from page i'll call it like this

    Code:
    tblAddGrInfo.Visible = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name),new[] {UserRoles.Hr, UserRoles.WebAdmin});
    But now for many controls (like a textbox, a dropdownlist, a label, a grid etc.) in the page i thought of a function like

    Code:
    public static bool SetEnable(int empId,  System.Web.UI.Control[] ctrlCol, UserRoles uRole)
    {
         try
         {
            foreach (var c in ctrlCol)
               [I][B][U]c.Enabled = IsPorpertySupport(empId, new [] {uRole});[/U][/B][/I] // ERROR LINE
            return true;
          }
          catch (Exception)
          {
             return false;
          }
    }
    But the problem is a control don't have Enabled property... you have to cast it to its proper type...
    My question is how do i do that? Writing cases for each control (textbox, dropdownlist, grid, checkbox, radiobutton etc) is hard to do... Is there a shorter way?

    Currently i'm using it in page load like
    Code:
    var isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
    txtEmployeeDOB.Enabled = isUserHr; //TextBox
    txtEmployeePANNo.Enabled = isUserHr;
    txtEmployeePassportNo.Enabled = isUserHr;
    txtEmployeeFirstName.Enabled = isUserHr;
    txtEmployeeLastName.Enabled = isUserHr;
    txtEmployeeBankAccNo.Enabled = isUserHr;
    txtEmployeePFAccNo.Enabled = isUserHr;
    chkIsEmployeeActive.Enabled = isUserHr; //CheckBox
    ddlPaysleepDesg.Enabled = isUserHr; //DropDownList
    ddlWorkingDesg.Enabled = isUserHr; //DropDownList
    but if control increases it's going to be difficult...
    Last edited by Frinavale; Jun 2 '10, 01:24 PM. Reason: Fixed the code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    If I were you I would use Reflection.

    First thing you need to do is import the Reflection namespace:
    Code:
    using System.Reflection;
    Then you need to use reflection to retrieve all the properties of the control (Enabled is a property). To retrieve the properties for the control you will use the Control's Type and call the GetProperties() method. This method returns an array of PropertyInfo types. Once you have this array, you need to retrieve the PropertyInfo type that represents the Enabled property (if there is one). If you are able to retrieve the Enabled-PropertyInfo type then you can use it to set the value for your control.

    See the comments in the code for a more detailed explanation:
    Code:
    //retrieving whether or not the control should be enabled
    bool isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
    
    //looping through each control 
    foreach (var ctrl in ctrlCol)
    {
      //grabbing all of the properties for the control
      PropertyInfo[] properties = ctrl.GetType().GetProperties();
      
      //attempting to retrieve the Enabled property for the control
      PropertyInfo enabledProperty = Array.Find(properties, (pi) => string.Compare(pi.Name, "Enabled", true) == 0);
    
      //checking whether or not there was an Enabled property
      if (enabledProperty != null)
      {
        //if there is an enabled property, setting the
        //control's enabled property to the isUserHr 
        //which indicates whether or not the control should 
        //be enabled
        enabledProperty.SetValue(ctrl, isUserHr, null);
      }
    }
    -Frinny

    Comment

    • sanndeb
      New Member
      • May 2010
      • 33

      #3
      Originally posted by Frinavale
      If I were you I would use Reflection.

      First thing you need to do is import the Reflection namespace:
      Code:
      using System.Reflection;
      Then you need to use reflection to retrieve all the properties of the control (Enabled is a property). To retrieve the properties for the control you will use the Control's Type and call the GetProperties() method. This method returns an array of PropertyInfo types. Once you have this array, you need to retrieve the PropertyInfo type that represents the Enabled property (if there is one). If you are able to retrieve the Enabled-PropertyInfo type then you can use it to set the value for your control.

      See the comments in the code for a more detailed explanation:
      Code:
      //retrieving whether or not the control should be enabled
      bool isUserHr = UserPermission.IsPorpertySupport(Convert.ToInt16(User.Identity.Name), new[] { UserRoles.Hr });
      
      //looping through each control 
      foreach (var ctrl in ctrlCol)
      {
        //grabbing all of the properties for the control
        PropertyInfo[] properties = ctrl.GetType().GetProperties();
        
        //attempting to retrieve the Enabled property for the control
        PropertyInfo enabledProperty = Array.Find(properties, (pi) => string.Compare(pi.Name, "Enabled", true) == 0);
      
        //checking whether or not there was an Enabled property
        if (enabledProperty != null)
        {
          //if there is an enabled property, setting the
          //control's enabled property to the isUserHr 
          //which indicates whether or not the control should 
          //be enabled
          enabledProperty.SetValue(ctrl, isUserHr, null);
        }
      }
      -Frinny
      Thanks Mate... that was perfect ....

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        :) Glad I could help :)

        Comment

        Working...