ClientTemplateId and customizing by user rights

1 Answer 110 Views
Grid ListView
Grzesiek
Top achievements
Rank 2
Iron
Grzesiek asked on 11 May 2022, 09:27 PM

Hey,

Let's say that I have a ListView with comments:


@(Html.Kendo().ListView<Adelante_ERP_GSBK.Models.ProjectComment>()
                    .Name("listView")
                    .TagName("div")
                    .ClientTemplateId("template")
                    .HtmlAttributes(new {style = "border: none;"})
                    .DataSource(dataSource => dataSource
                        .Ajax()
                        .Read(read => read.Action("GetComments", "ProjectComment", new {projectId = Model.Id}))
                    ))

Template


<script type="text/x-kendo-tmpl" id="template">
<div class="post">
    <div class="user-block" style="margin-bottom: 0;">
        <span class="username" style="display: inline; margin-left: 10px;">
            #:User.Name#
        </span>
        <span class="description" style="display: inline; margin-left: 5px">
            #:kendo.toString(CommentDate, "d")#
        </span>
        <a onclick="showInPopup('@Url.Action("AddOrEdit", "ProjectComment", null, Context.Request.Scheme)/#:Id#', 'Edit')" style="cursor: pointer;" title="Dodaj">
            <i class="fa-solid fa-pen-to-square"></i>
        </a>
        <a onclick="showInPopup('@Url.Action("Delete", "ProjectComment", null, Context.Request.Scheme)/#:Id#', 'Delete?')" style="cursor: pointer;" title="Usuń">
             <i class="fa-solid fa-xmark"></i>
        </a>
    </div>
    <div style="margin-left: 10px;">
        #:CommentContent#
    </div>
</div>
</script>

Everything works fine, but I don't want that all users always see edit/delete buttons.

Edit button should be visible only for author and administrator, delete only for administrator.

 

Does anyone have an idea how can I do this?

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 16 May 2022, 01:15 PM

Hi Grzesiek,

There are a couple of ways to hide the buttons from the template depending on where you store the information about the logged user. If you use a Razor Model to send this information to the View - define a JS function that parses the Model to Json. Then call this function in the Kendo Template:

@model UserModel

function isAdminOrAuthor() {
            var admin = @Html.Raw(Json.Serialize(@Model.Admin));
            var author = @Html.Raw(Json.Serialize(@Model.Author));
            
            if (admin || author) {
                return true;
            } else {
                return false;
            }
        }

<a class='#:isAdminOrAuthor()? ' ' :'hidden'#'  onclick="showInPopup('@Url.Action("AddOrEdit", "ProjectComment", null, Context.Request.Scheme)/#:Id#', 'Edit')" style="cursor: pointer;" title="Dodaj">
            <i class="fa-solid fa-pen-to-square"></i>
        </a>
<a class='#:isAdmin()? ' ' :'hidden'#' onclick="showInPopup('@Url.Action("Delete", "ProjectComment", null, Context.Request.Scheme)/#:Id#', 'Delete?')" style="cursor: pointer;" title="Usuń">
             <i class="fa-solid fa-xmark"></i>
        </a>

hidden{
     display:none;
}

Alternatively, if you store the information about the authority of the user in the browser's session you can access the session in the custom JavaScript function.

I hope the information above is useful.

Regards,
Stoyan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid ListView
Asked by
Grzesiek
Top achievements
Rank 2
Iron
Answers by
Stoyan
Telerik team
Share this question
or