Object Oriented way to handle access

Last post 07-03-2009 9:34 AM by vivek_iit. 4 replies.

Sort Posts:

  • Object Oriented way to handle access

    07-03-2009, 2:43 AM
    • Member
      20 point Member
    • piyushc
    • Member since 10-15-2004, 9:52 AM
    • Posts 7

    Hello All,

    I have this question since quite some time now.

    I have a project in which I have few user roles.

    Depending on the user role, I need to show/hide some part of the page and even set properties of the controls.

    This has led to n number of if-else statements in my aspx page which does not smell good.

    What is the way to handle such scenarios using Object Oriented techniques?

    Help is appreciated.

    Thanks, Piyush

  • Re: Object Oriented way to handle access

    07-03-2009, 4:52 AM
    Answer
    • Contributor
      5,110 point Contributor
    • RickNZ
    • Member since 01-01-2009, 8:43 AM
    • Nelson, New Zealand
    • Posts 856

    The ideal object oriented way is that each object knows how to display itself, including in response to the current role, etc.

    You might consider things like creating some user controls that know how to respond to the current role.  If the actions involve multiple pages, you might be able to implement some of the logic in a master page or in a common page base class.  If it involves all controls of a given type, you can use control adapters or tag mapping.

  • Re: Object Oriented way to handle access

    07-03-2009, 5:40 AM
    • Member
      262 point Member
    • v.vivek
    • Member since 06-25-2009, 10:50 AM
    • Posts 52

    this are my sugession that may be useful for ur application.

    create usercontrol (Nevigation control) on the basis of different role.

    on login store role type in session .

    dynamic bind specfic usecontrol based on role.

    Thanks..
    Vivek Vishal

    Please Mark As Answer If This Post Help You To Find Your Solution.
  • Re: Object Oriented way to handle access

    07-03-2009, 9:06 AM
    Answer
    • Contributor
      3,196 point Contributor
    • DigiMortal
    • Member since 01-10-2007, 7:22 PM
    • Tallinn, Estonia
    • Posts 562

    I think that it is bes for you if you separate the pages for different roles. User controls should be able to function without knowing who and how is using them - they just get their data and get all the orders from parent controls or pages. Maybe today your code behind the pages is not very large and complex to handle but it may happen soon. Usually all specific roles add their own little differences and it is better if you consider this right now than later when there is a lot of code that needs to be moved and refactored.

    Don't forget to mark solution providing post as "Answered".
    It helps others to find correct solutions!

    Also visit my ASP.NET blog!
  • Re: Object Oriented way to handle access

    07-03-2009, 9:34 AM
    Answer
    • All-Star
      17,710 point All-Star
    • vivek_iit
    • Member since 06-18-2006, 2:13 PM
    • New Delhi
    • Posts 3,171
    • TrustedFriends-MVPs

    piyushc:

    Depending on the user role, I need to show/hide some part of the page and even set properties of the controls.

    This has led to n number of if-else statements in my aspx page which does not smell good.

    You need to create a Permissions utility class, with a method like : bool HasAccess(ActionEnum action, User user)

    You can divide your system objects into Actions, like ShowHeader, ShowFooter etc.

    Each user will have its own permission set, and this utility function will check the user's role and associate it with the relevant permission to return true or false based on if the user has permission to perform that action or not.

    Hope this helps,

    Vivek

Page 1 of 1 (5 items)