public RolePermissionModel GetPermissionByRoleName(string roleName) { if (!IsAuthorize("read")) { return null; } RolePermissionModel rolePermissionModel = new RolePermissionModel(); List<PermissionViewModel> permissionViewModelList = new List<PermissionViewModel>(); PermissionViewModel permissionViewModel = null; if (_fileManagerAuth.HasSession()) { List<Permission> permissionList = _facade.GetPermissions().ToList(); if (roleName.Length > 0) { rolePermissionModel.RoleName = roleName; Role userRole = _facade.GetRoleByRoleName(roleName); string userRoleIdStr = userRole.RoleId.ToString(); List<RolePermission> rolePermissionList = _facade.GetRolePermissionByRoleId(userRoleIdStr).ToList(); foreach (RolePermission rolePermission in rolePermissionList) { Permission permission = permissionList.Where(x => x.Id == rolePermission.PermissionId).FirstOrDefault(); if (permission != null) { permissionViewModel = new PermissionViewModel(); permissionViewModel.PermissionName = permission.PermissionName; permissionViewModel.AllowRead = rolePermission.AllowRead; permissionViewModel.AllowWrite = rolePermission.AllowWrite; permissionViewModel.AllowDelete = rolePermission.AllowDelete; permissionViewModel.PermissionNameRead = permission.PermissionName.Replace(" ","") + "Read"; permissionViewModel.PermissionNameWrite = permission.PermissionName.Replace(" ", "") + "Write"; permissionViewModel.PermissionNameDelete = permission.PermissionName.Replace(" ", "") + "Delete"; if (permission.ReadApplicable) { permissionViewModel.ReadClass = rolePermission.AllowRead ? "Checked" : "UnChecked"; } else { permissionViewModel.ReadClass = "Disabled"; } if (permission.WriteApplicable) { permissionViewModel.WriteClass = rolePermission.AllowWrite ? "Checked" : "UnChecked"; } else { permissionViewModel.WriteClass = "Disabled"; } if (permission.DeleteApplicable) { permissionViewModel.DeleteClass = rolePermission.AllowDelete ? "Checked" : "UnChecked"; } else { permissionViewModel.DeleteClass = "Disabled"; } permissionViewModelList.Add(permissionViewModel); } } foreach (Permission item in permissionList) { PermissionViewModel permissionViewModellocal = permissionViewModelList.Where(x => x.PermissionName == item.PermissionName).FirstOrDefault(); if (permissionViewModellocal == null) { permissionViewModel = new PermissionViewModel(); permissionViewModel.PermissionName = item.PermissionName; permissionViewModel.AllowRead = false; permissionViewModel.AllowWrite = false; permissionViewModel.AllowDelete = false; permissionViewModel.PermissionNameRead = item.PermissionName.Replace(" ", "") + "Read"; permissionViewModel.PermissionNameWrite = item.PermissionName.Replace(" ", "") + "Write"; permissionViewModel.PermissionNameDelete = item.PermissionName.Replace(" ", "") + "Delete"; permissionViewModel.ReadClass = "UnChecked"; permissionViewModel.WriteClass ="UnChecked"; permissionViewModel.DeleteClass ="UnChecked"; permissionViewModelList.Add(permissionViewModel); } } } } rolePermissionModel.PermissionViewModelList = permissionViewModelList; return rolePermissionModel; }
// POST api/<controller> public RolePermissionModel Post(RoleViewModel roleViewModel) { if (!IsAuthorize("write")) { return null; } bool isSuccess = false; RolePermissionModel rolePermissionModel = new RolePermissionModel(); List<RolePermissionViewModel> rolePermissionViewModelList = new List<RolePermissionViewModel>(); List<Role> allRoles = _facade.GetRoles().ToList(); List<Permission> permissionList = null; Role role = allRoles.Where(x => x.RoleName == roleViewModel.UserRole).FirstOrDefault(); if (role == null) { _facade.CreateRole(roleViewModel.UserRole); allRoles = _facade.GetRoles().ToList(); role = allRoles.Where(x => x.RoleName == roleViewModel.UserRole).FirstOrDefault(); permissionList = _facade.GetPermissions().ToList(); if (roleViewModel.Userpermissions != null) { foreach (PermissionViewModel item in roleViewModel.Userpermissions) { Permission permission = permissionList.Where(x => x.PermissionName == item.PermissionName).FirstOrDefault(); if (permission != null) { _facade.AddRolePermission(new RolePermission() { RoleId = role.RoleId.ToString(), PermissionId = permission.Id, AllowRead = item.AllowRead, AllowWrite = item.AllowWrite, AllowDelete = item.AllowDelete }); } } isSuccess = true; } if (isSuccess) { rolePermissionViewModelList = GetRolePermissionViewModel(); } } rolePermissionModel.PermissionViewModelList = GetPermissionViewModel().ToList<PermissionViewModel>(); rolePermissionModel.RolePermissionViewModelList = rolePermissionViewModelList; return rolePermissionModel; }