Role Based Logic at the Table Level

Last post 06-12-2008 4:41 AM by akp_gst. 17 replies.

Sort Posts:

  • Role Based Logic at the Table Level

    05-10-2008, 3:41 AM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    Based on our previous threads I now understand how we can create an IAutoFieldGenerator class that contains a GenerateFields method which can be used to either make fields "Hidden" or "Readonly" based on the role of the current user for each of the different views: List, Edit, Details, and Insert. For GridView we use "GridView1.ColumnsGenerator" and for DetailsView we use "DetailsView1.RowGenerator".

    Now, how can be do something similar at the table level? Is there maybe an IAutoTableGenerator class somewhere?

    For instance, based on the user's role I want to define the following attributes: "Hidden", "AllowInserts", "AllowEdits", "AllowDeletes", "AllowDetails", "AllowSelect". I want to put logic in the default pages that implement these basic features.

    Tommy

     

  • Re: Role Based Logic at the Table Level

    05-10-2008, 5:26 AM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 1:11 PM
    • Newton-le-Willows, UK
    • Posts 655

    Hi Tommy

    You could certanly do somthing similar to the other thread but the logic would exist in may places:

    1. On the Default.aspx page.
    2. In the code for the IAutoColumnGenerator code the make sure that hidden tables had no link column shown.
      You would then need code to travers the relationship get its metadata to see if it was hidden.
    3. On each of the List, Edit, Details, ListDetails, and Insert pages to disable the Edit, Insert and Delete links/buttons

    This was definitley something I was going to look into so I'll get on and have a look and post you what I find.

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
  • Re: Role Based Logic at the Table Level

    05-10-2008, 8:51 AM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    One question is where are the attributes for the tables?

    MetaModel.Default.VisibleTables is used to get a list of visible tables of type <MetaTable>, but Intellisense doesn't should any properties or methods for that type.

    Tommy

     

  • Re: Role Based Logic at the Table Level

    05-10-2008, 9:02 AM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 1:11 PM
    • Newton-le-Willows, UK
    • Posts 655

    I found the attribute for the table on the table:

    System.ComponentModel.AttributeCollection a1 = _table.Attributes;

    this collection can then be accessed via the same method that the column attributes were:P>

    public static class FilteredFieldsManagerHelper
    {
        public static List GetPermissionsAttributes(this System.ComponentModel.AttributeCollection attributes, String role)
        {
            List permissions = new List();
            foreach (Attribute attribute in attributes)
            {
                if (attribute.GetType() == typeof(PermissionsAttribute))
                {
                    if(((PermissionsAttribute)attribute).Role == role)
                    permissions.Add(((PermissionsAttribute)attribute).Permission);
                }
            }
            return permissions;
        }
    } 
    The above extension method asumes the fix for multiple attributes of the same type on at class level (Table level) 
    if your still going down the path of seperate attributes for each permission:
    • HiddenPermissionAttribute
    • AllowInsertsPermissionAttribute
    • AllowEditsPermissionAttribute
    • AllowDeletesPermissionAttribute
    • AllowDetailsPermissionAttribute
    • AllowSelectPermissionAttribute
    Then you could create an extension method to extract the list of roles for each permission attribute.
    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
  • Re: Role Based Logic at the Table Level

    05-10-2008, 9:57 AM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    Steve,

    This is going to work perfectly. I was brain dead eariler and was looking for the properties under the MetaTable class rather than looking at the properties of an instance of the MetaTable class.

    While searching for this, I went ahead and updated my Intellisense as mentioned in a eariler post. I should have done this before anyway.

    From the install files, I copied the following to: \Windows\Microsoft.NET\Framework\v2.0.50727

    04/02/2008  11:12 AM            57,344 System.ComponentModel.DataAnnotations.dll
    04/02/2008  11:12 AM            81,920 System.Web.Abstractions.dll
    03/14/2008  07:44 PM         5,136,384 System.Web.dll
    04/04/2008  01:47 PM            32,768 System.Web.DynamicData.Design.dll
    04/04/2008  01:47 PM           221,184 System.Web.DynamicData.dll
    04/02/2008  11:12 AM           335,872 System.Web.Extensions.Design.dll
    04/04/2008  01:47 PM         1,294,336 System.Web.Extensions.dll
    04/02/2008  11:12 AM            61,440 System.Web.Routing.dll

    Now we can add logic similar to what is currently in the Page_Load such as:

    if (table.IsReadOnly) {
         GridView1.Columns.RemoveAt(0);
         InsertHyperLink.Visible =
    false;
    }

    Unfortunantly, doing this for the edit link does not work.

    EditHyperLink.Visible = false;   // doesn't work

    I'm sure there's some trick to get access to the EditHyperLink control.

    HyperLink link = (HyperLink)FindControl("EditHyperLink");    // doesn't work either, returns null
    
    I guess I need to get an ASP.Net book out and read.
    Tommy

     

  • Re: Role Based Logic at the Table Level

    05-10-2008, 10:38 AM
    Answer
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 8:31 AM
    • Redmond, WA
    • Posts 923

    Hi Tommy,

    Try this: on the GridView tag om list.aspx, add:

    OnRowCreated="OnGridViewRowCreated"

    Then in the code behind, add:

        protected void OnGridViewRowCreated(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                var editLink = (HyperLink)e.Row.FindControl("EditHyperLink");
                if ([your condition]) {
                    editLink.Visible = false;
                }
            }
        }
    
      

    It's really great that you and Steve are feeding on each other to build this.  I can't wait to see the end result you you guys end up with! Smile

    David

  • Re: Role Based Logic at the Table Level

    05-10-2008, 10:58 AM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    David,

    Yes, that worked perfectly!

    What's really amazing is how Dynamic Data leads an inexperienced ASP.Net programmer like myself to do some amazing things so quickly with such logical and eligant solutions!

    Thanks,

    Tommy

  • Re: Role Based Logic at the Table Level

    06-06-2008, 1:12 AM
    • Loading...
    • akp_gst
    • Joined on 06-05-2008, 4:27 AM
    • India (Mumbai)
    • Posts 8
    i want to use dynamic data in my project, so i am trying the test our requirement using this framework. one big problem is user right and access permission. i want to configure the user right on the page level. i read this code but sorry,i not able to understand where i write this code in the application. pls help me (i am new in this area.) akp
    Thanks & Regards
    Awadhesh(akp)
  • Re: Role Based Logic at the Table Level

    06-06-2008, 2:48 AM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 1:11 PM
    • Newton-le-Willows, UK
    • Posts 655

    Hi have a look at my blog post series here that sould show you what you want.

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
  • Re: Role Based Logic at the Table Level

    06-06-2008, 8:11 AM
    • Loading...
    • akp_gst
    • Joined on 06-05-2008, 4:27 AM
    • India (Mumbai)
    • Posts 8
    Thanks sir, if i face any problem then i will tell you.
    Thanks & Regards
    Awadhesh(akp)
  • Re: Role Based Logic at the Table Level

    06-10-2008, 1:06 AM
    • Loading...
    • akp_gst
    • Joined on 06-05-2008, 4:27 AM
    • India (Mumbai)
    • Posts 8
    Hi, This sample is very help full in my requirement. but is this possible we can set role and right run time instead of hard coding role in the class file. If you have any idea please help me.
    Thanks & Regards
    Awadhesh(akp)
  • Re: Role Based Logic at the Table Level

    06-10-2008, 2:27 AM
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 8:31 AM
    • Redmond, WA
    • Posts 923

    You can do this in code by using the custom metadata provider demonstrated in the blog post.

    David

  • Re: Role Based Logic at the Table Level

    06-10-2008, 10:05 AM
    • Loading...
    • akp_gst
    • Joined on 06-05-2008, 4:27 AM
    • India (Mumbai)
    • Posts 8

    Hi, could we create multilingual project using dynamic data. if yes please give any sample code. i tried it but not able to create.

    Thanks & Regards
    Awadhesh(akp)
  • Re: Role Based Logic at the Table Level

    06-11-2008, 12:55 AM
    • Loading...
    • akp_gst
    • Joined on 06-05-2008, 4:27 AM
    • India (Mumbai)
    • Posts 8
    Hi, could we store or set these right using database?

    [MetadataType(typeof(OrderMetadata))]
    [TablePermissions(TablePermissionsAttribute.Permissions.DenyInserts, "Accounts")]
    public partial class Order
    {
    }
    public class OrderMetadata
    {
    [FieldPermissions(FieldPermissionsAttribute.Permissions.DenyEdit, "Sales", "Production")]
    [FieldPermissions(FieldPermissionsAttribute.Permissions.DenyRead, "Accounts")]
    public Object OrderDate { get; set; }
    [FieldPermissions(FieldPermissionsAttribute.Permissions.DenyEdit, "Accounts","Sales", "Production")]
    [FieldPermissions(FieldPermissionsAttribute.Permissions.DenyRead, "Accounts")]
    public Object RequiredDate { get; set; }
    // entities
    [HideInFilter]
    public Object Employee { get; set; }
    public Object Shipper { get; set; }
    }
    Thanks & Regards
    Awadhesh(akp)
  • Re: Role Based Logic at the Table Level

    06-11-2008, 2:32 AM
    • Loading...
    • sjnaughton
    • Joined on 04-29-2008, 1:11 PM
    • Newton-le-Willows, UK
    • Posts 655

    I would sugest that you look at Marcin's post regarding Dynamic Data samples: Custom metadata providers which shows how to get the metadata from else where.

    Steve

    Seeking the elegant solution.
    [Oh! If olny I colud tpye!Confused]
    c# Bits blog
Page 1 of 2 (18 items) 1 2 Next >