Hiding a column in a table based on role in MVC

In some of my tables in the index view, some roles may see more of the table than others.

So that means I have to hide some columns

I did it fast and dirty:

<% bool codesZichtbaar = Roles.IsUserInRole("Admin") || Roles.IsUserInRole("Algemeen Beheer") || Roles.IsUserInRole("Secretariaat");  %>


<table>
    <tr>
        <th>
        </th>
        <th>
            Adres
        </th>
        <th>
            Instellingsnr.
        </th>
        <% if (codesZichtbaar) { %>
        <th>
            Codes
        </th>
        <%} %>
        <th>
            IKON
        </th>
        <th>
            Datum Van-Tot
        </th>
        <th>
        </th>
    </tr>
    <% foreach (var item in Model) { %>
    <tr class="inst<%: item.actieveSchool?"active":"inactive" %>">
        <td>
            <% Html.RenderPartial("buttons", new MVC2_NASTEST.Models.ButtonDetail() { RouteValues = new { id = item.Inst_ID }, edit = false, details = true, delete = false, takeOptionalsFromUrl = true }); %>
        </td>
        <td>
            <%: item.INST_NAAM%><br />
            <%: item.STRAAT%><br />
            <%: item.POSTCODE%>
            <%: item.GEMEENTE%>
        </td>
        <td>
            <%: item.DOSSNR%>
        </td>
        <% if (codesZichtbaar) { %>
        <td>
            Gebruiker:
            <%: item.Inst_Gebruikersnaam%><br />
            C:
            <%: item.Inst_Concode%><br />
            D:
            <%: item.Inst_DirCode%>
        </td>
        <%} %>
        <td>
            T:
            <%: item.INST_TYPE%><br />
            Inst_REF:
            <%: item.INST_REF%><br />
            Loc_REF:
            <%: item.INST_LOC_REF%><br />
            Loc_Nr:
            <%: item.INST_LOCNR%>
        </td>
        <td>
            <%: String.Format("{0:d}", item.DATUM_VAN)%>
            -
            <%: String.Format("{0:d}", item.DATUM_TOT)%>
        </td>
        <td>
            <a href='<%= Url.Action("Links", "Instelling", MVC2_NASTEST.RouteValues.MergeRouteValues(MVC2_NASTEST.RouteValues.optionalParamters(Request.QueryString), new {id=item.Inst_ID})) %>'
                title="Linken">
                <img src="<%= Url.Content("~/img/link.png") %>" alt="Link" width="16" /></a>
        </td>
    </tr>
    <% } %>
</table>

but this is MVC, my code is dry, what is the right way to do this? or, which is a cleaner way to do this.

+3
source share
1 answer

I would write Html for this helper. It might look something like this:

public bool IsInRole(this HtmlHelper instance, params string[] roles)
{
    var user = html.ViewContext.HttpContext.User;
    foreach(var role in roles)
    {
        if(user.IsInRole(role))
            return true;
    }

    return false;
}

This function will return true if the user is in any of the provided roles. You would use it as follows:

<tr>
    <td>First - Visible to all</td>
    <% if(Html.IsInRole("Admin", "Algemeen Beheer", "Secretariaat")) { %>
        <td>Only for privileged users</td>
    <% } %>
    <td>Last - Visible to all
</tr>

This is an easy, clean and reusable way to do it.

+4
source

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


All Articles