ASP.NET MVC - mapping form value by role

I'm looking for the perfect way for my input forms to appear as a text box (editable), label (read-only), or hidden (without access) based on roles. I know that I could have a different view for each type of role, but I was hoping there would be some new kindness that would prevent me from making 80 views.

+2
source share
3 answers

In fact, it all depends on where you want to set the security-related metadata. What do you want to do? Beautify your look models with attributes? Use dynamic data metadata classes? Free ala StructureMap / FluentNhibernate configuration?

MVC Preview 2 InputBuilder lostechies.com - UIHint UIHint :

public class RoleUI : UIHintAttribute
{
    public RoleUI( string roles ) : base("","")
    {
        if( HttpContext.WhereverTheRoleStuffIs == "Admin" ) 
        //could be Session["CurrentUser"] too
        {
            this.UIHint = "Input";
        }

        this.UIHint = "Label";
    }

}

, :

public class AwesomeModel
{
    [RoleUI("Admin")]
    public string FirstName { get; set; }
}

, , , , .

+3

, . .

, readonly, , , - .

0

Html, , , .

Actionlinks, Html.ActionLinkSecured

Namespace System.Web.Mvc.Html
{
public static class HtmlHelperExtensions
{
    public static string ActionLinkSecured(this HtmlHelper htmlHelper, string linkText, string action, string controller, object routeValues, bool showDisabled)
    {
      //check if user is logged in or whatever you wanna check
      //if ok
      return htmlHelper.ActionLink(...);
      //else
      return linkText
    }
}

You can have whatever you want ... text, text field, disabled text field ... :)

0
source

Source: https://habr.com/ru/post/1772587/


All Articles