Role permissions in mvc
In this we will discuss roles permissions
Table Structure
Model
public class Permissions { public int MainMenuID { get; set; } public bool CanAdd { get; set; } public bool CanEdit { get; set; } public bool CanDelete { get; set; } public bool CanRead { get; set; } public string RoleID { get; set; } }
controller
public class PermissionsController : BaseController { [HttpPost] public JsonResult AddPermissions(List<Permissions> perms) { if (perms.Count <= 0) { return Json(0, JsonRequestBehavior.AllowGet); } using (PermissionsRepository _repo = new PermissionsRepository()) { _repo.BulkInsert(perms); return Json(1, JsonRequestBehavior.AllowGet); } } }
repository
public class PermissionsRepository : Connection, IDbCommon<Permissions> { public bool BulkInsert(List<Permissions> permissions) { DataTable dt = new DataTable("Permissions"); dt = permissions.ToDataTable<Permissions>(); SqlBulkCopy blkcpy = new SqlBulkCopy(con); blkcpy.DestinationTableName = "Permissions"; blkcpy.ColumnMappings.Add("RoleID", "RoleID"); blkcpy.ColumnMappings.Add("MainMenuID", "MainMenuID"); blkcpy.ColumnMappings.Add("CanAdd", "CanAdd"); blkcpy.ColumnMappings.Add("CanEdit", "CanEdit"); blkcpy.ColumnMappings.Add("CanDelete", "CanDelete"); blkcpy.ColumnMappings.Add("CanRead", "CanRead"); try { OpenConnection(); blkcpy.WriteToServer(dt); return true; } catch (Exception ex) { // some error occured. Bubble it to caller and encapsulate Exception object throw new Exception("Permissions::Update::Error occured.", ex); } finally { // Close connection. CloseConnection(); blkcpy.Close(); } } }
view
@model IEnumerable<PermissionsModel> <table class="permissions table-hover" id="tblpermissions"> <thead> <tr class="bg-gray"> <th><i class="fa fa-files-o"></i> Module</th> <th><i class="fa fa-file-o"></i> Read</th> <th><i class="fa fa-pencil"></i> Edit</th> <th><i class="fa fa-trash"></i> Delete</th> <th><i class="fa fa-plus"></i> Add <span class="pull-right" style="padding-left:7px;padding-right:7px;">@Html.CheckBox("CheckAll",false,htmlAttributes:new {onclick="CheckAll(this)" }) All</span></th> </tr> </thead> <tbody> @foreach (var item in Model) { if (item.isParent.Value && item.ParentID == 0) { var childs = Model.Where(x => x.ParentID == item.MainMenuID).ToList(); <tr class="parent"> <td> @Html.Hidden("hdMainMenuID",item.MainMenuID) <a href="javascript:void(0)"> @if (item.havingImageClass.Value) { <i class="@item.ImageClass"></i> } <span>@item.ModuleName</span> <i class="fa fa-angle-right text-orange"></i> </a> </td> <td>@Html.EditorFor(x => item.CanRead)</td> <td>@Html.EditorFor(x => item.CanEdit)</td> <td>@Html.EditorFor(x => item.CanDelete)</td> <td>@Html.EditorFor(x => item.CanAdd)</td> </tr> foreach (var ch in childs) { <tr class="child"> <td> <i class="fa fa-angle-double-right text-orange"></i> @Html.Hidden("hdMainMenuID", ch.MainMenuID) @ch.NameOption </td> <td> @Html.EditorFor(x=>ch.CanRead) </td> <td> @Html.EditorFor(x => ch.CanEdit) </td> <td> @Html.EditorFor(x => ch.CanDelete) </td> <td> @Html.EditorFor(x => ch.CanAdd) </td> </tr> } } } </tbody> </table> <hr /> <div class="row"> <div class="col-lg-12"> <button type="button" class="btn btn-orange" onclick="SavePermissions()"><i class="fa fa-save"></i> Save changes</button> </div> </div> <script> function CheckAll(chk) { var checked = $(chk).is(":checked"); if(checked) { $("input[type='checkbox']").each(function () { $(this).attr('checked', 'checked'); }); } else { $("input[type='checkbox']").each(function () { $(this).removeAttr('checked'); }); } } function SavePermissions() { var roleid = $('#hdSelectedRoleID').val(); var arrperm = []; $("#tblpermissions tbody tr").each(function () { var tr = $(this); var cRead = $(tr).find('input[id*="_CanRead"]').is(":checked"); var cEdit = $(tr).find('input[id*="_CanEdit"]').is(":checked");; var cDelete = $(tr).find('input[id*="_CanDelete"]').is(":checked");; var cAdd = $(tr).find('input[id*="_CanAdd"]').is(":checked");; var mainMenuid = $(tr).find('#hdMainMenuID').val(); var objPer = { RoleID: roleid, MainMenuID: mainMenuid, CanRead: cRead, CanDelete: cDelete, CanEdit: cEdit, CanAdd:cAdd } arrperm.push(objPer); }); var urlr = "@Url.Action("AddPermissions", "Permissions")"; $.ajax({ url: urlr, type: "POST", contentType: "application/json; charset=utf-8", dataType: "JSON", async:false, data: JSON.stringify({perms:arrperm}), success: function (data) { if(data==1) { $.toast({ heading: 'success', text: 'Permissions saved successfully', position: 'top-right', loaderBg: '#ff6849', icon: 'success', hideAfter: 3500, stack: 6 }); } else { $.toast({ heading: 'Error', text: 'Error occured while saving permissions try again', position: 'top-right', loaderBg: '#ff6849', icon: 'error', hideAfter: 3500 }); } } }); } </script>
0 Comments