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.
You could certanly do somthing similar to the other thread but the logic would exist in may places:
<div mce_keep="true"> <div mce_keep="true">On the Default.aspx page.</div></div>
<div mce_keep="true"> <div mce_keep="true">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.</div></div>
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.
ASP.NET Dynamic Data
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
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.
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.
ASP.NET Dynamic DataMetadataAttributes
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
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.
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!
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
tlanier
Member
42 Points
71 Posts
Role Based Logic at the Table Level
May 10, 2008 07:41 AM|LINK
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
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Role Based Logic at the Table Level
May 10, 2008 09:26 AM|LINK
Hi Tommy
You could certanly do somthing similar to the other thread but the logic would exist in may places:
You would then need code to travers the relationship get its metadata to see if it was hidden.</div></div>
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.
ASP.NET Dynamic Data
Always seeking an elegant solution.
tlanier
Member
42 Points
71 Posts
Re: Role Based Logic at the Table Level
May 10, 2008 12:51 PM|LINK
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
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Role Based Logic at the Table Level
May 10, 2008 01:02 PM|LINK
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; } }ASP.NET Dynamic Data Metadata Attributes
Always seeking an elegant solution.
tlanier
Member
42 Points
71 Posts
Re: Role Based Logic at the Table Level
May 10, 2008 01:57 PM|LINK
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 nullI guess I need to get an ASP.Net book out and read.davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Role Based Logic at the Table Level
May 10, 2008 02:38 PM|LINK
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! [:)]
David
tlanier
Member
42 Points
71 Posts
Re: Role Based Logic at the Table Level
May 10, 2008 02:58 PM|LINK
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
akp_gst
Member
16 Points
8 Posts
Re: Role Based Logic at the Table Level
Jun 06, 2008 05:12 AM|LINK
Awadhesh(akp)
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Role Based Logic at the Table Level
Jun 06, 2008 06:48 AM|LINK
Hi have a look at my blog post series here that sould show you what you want.
Dynamic Data Metadata Attributes Roles Permissions
Always seeking an elegant solution.
akp_gst
Member
16 Points
8 Posts
Re: Role Based Logic at the Table Level
Jun 06, 2008 12:11 PM|LINK
Awadhesh(akp)