private IEnumerable<PermissionViewModel> GetPermissionViewModel() { List<PermissionViewModel> permissionViewModelList = new List<PermissionViewModel>(); PermissionViewModel permissionViewModel = null; foreach (Permission item in _facade.GetPermissions()) { permissionViewModel = new PermissionViewModel(); permissionViewModel.PermissionName = item.PermissionName; permissionViewModel.PermissionNameRead = item.PermissionName.Replace(" ", "") + "Read"; permissionViewModel.PermissionNameWrite = item.PermissionName.Replace(" ", "") + "Write"; permissionViewModel.PermissionNameDelete = item.PermissionName.Replace(" ", "") + "Delete"; permissionViewModel.ReadClass = "Enabled"; permissionViewModel.WriteClass = "Enabled"; permissionViewModel.DeleteClass = "Enabled"; if (!item.ReadApplicable) { permissionViewModel.ReadClass = "Disabled"; } if (!item.WriteApplicable) { permissionViewModel.WriteClass = "Disabled"; } if (!item.DeleteApplicable) { permissionViewModel.DeleteClass = "Disabled"; } permissionViewModelList.Add(permissionViewModel); } return permissionViewModelList; }
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; }
public IEnumerable<PermissionViewModel> GetUserPermission(string origin) { List<PermissionViewModel> permissionViewModelList = new List<PermissionViewModel>(); PermissionViewModel permissionViewModel = null; Role userRole = null; if (_fileManagerAuth.HasSession()) { string useremail = _fileManagerAuth.GetCurrentUserName(); string[] userRoles = _facade.GetRolesForUser(useremail); List<string> detailPermissions = null; List<Permission> permissionList = _facade.GetPermissions().ToList(); string userRoleStr = userRoles[0]; if (userRoleStr.Length > 0) { userRole = _facade.GetRoleByRoleName(userRoleStr); if (userRole.RoleName.Equals("Admin",StringComparison.OrdinalIgnoreCase)) { foreach (Permission permission in permissionList) { permissionViewModel = new PermissionViewModel(); permissionViewModel.PermissionName = permission.PermissionName; permissionViewModel.AllowRead = true; permissionViewModel.AllowWrite = true; permissionViewModel.AllowDelete = true; permissionViewModelList.Add(permissionViewModel); } } else { if (userRole != null) { if (origin == "resourcedetail") { detailPermissions = new List<string> { "Resource", "Download" }; } else if (origin == "category") { detailPermissions = new List<string> { "Category"}; } else if (origin == "users") { detailPermissions = new List<string> { "Users" }; } else if (origin == "createrole") { detailPermissions = new List<string> { "CreateRole" }; } else if (origin == "downloadhistory") { detailPermissions = new List<string> { "DownloadHistory" }; } string userRoleIdStr = userRole.RoleId.ToString(); List<RolePermission> rolePermissionList = _facade.GetRolePermissionByRoleId(userRoleIdStr).ToList(); foreach (var item in detailPermissions) { Permission permission = permissionList.Where(x => x.PermissionName == item).FirstOrDefault(); if (permission != null) { List<RolePermission> rolePermissionListLocal = rolePermissionList.Where(x => x.PermissionId == permission.Id).ToList(); foreach (RolePermission rolePermission in rolePermissionListLocal) { permissionViewModel = new PermissionViewModel(); permissionViewModel.PermissionName = permission.PermissionName; permissionViewModel.AllowRead = rolePermission.AllowRead; permissionViewModel.AllowWrite = rolePermission.AllowWrite; permissionViewModel.AllowDelete = rolePermission.AllowDelete; permissionViewModelList.Add(permissionViewModel); } } } } } } } return permissionViewModelList; }