// Retrive policies for a specific report item. protected Policy[] GetPoliciesForItem(string path) { RSClient rs = null; Boolean val = true; Policy[] curPolicy = null; try { rs = new RSClient(); // Sets Url and credentials for the endpoint. rs.Url = String.Format("https://{0}:443/ReportServer/ReportService2010.asmx", ConfigurationManager.AppSettings["RSSERVER_NAME"]); NetworkCredential cred = new NetworkCredential( ConfigurationManager.AppSettings["RSUSERNAME"], ConfigurationManager.AppSettings["RSPASSWORD"], ConfigurationManager.AppSettings["RSSERVER_NAME"]); rs.Credentials = cred; rs.LogonUser(cred.UserName, cred.Password, cred.Domain); // Checks if the user is authenticated if (rs.CheckAuthenticated()) { curPolicy = new Policy[1]; curPolicy = rs.GetPolicies(path, out val); } // Returns the Policy (users and associated roles) for the specified report item path. return(curPolicy); } catch (SoapException ex) { if (ex.Message.Contains("The permissions granted to user")) { lblException.Text = string.Format("The permissions granted to user '{0}' are insufficient for modifying permissions on the report item path: {1}", ConfigurationManager.AppSettings["RSUSERNAME"].ToString(), path); } return(null); } catch (Exception Ex) { lblException.Text = Ex.Message; return(null); } finally { if (rs != null) { rs.Dispose(); } } }
// Saves the users-permissions private void SavePolicies() { try { //Retrieve the table from the session object. DataTable dt = (DataTable)Session["ItemPermissions"]; int i = 0; int j; int rolesindex = 0; int usersindex = 0; bool dbroundtrip = false; // Identify the users with atleast one role selected. foreach (GridViewRow gvr in gvItemPermissions.Rows) { rolesindex = 0; usersindex++; for (int cells = 2; cells < gvr.Cells.Count; cells++) { if (((CheckBox)(gvr.Cells[cells].Controls[0])).Checked) { rolesindex++; } } if (rolesindex == 0) { usersindex--; } } // Based on number of users, create an array of policies. Policy[] newPolicy = new Policy[gvItemPermissions.Rows.Count]; if (usersindex < gvItemPermissions.Rows.Count) { dbroundtrip = true; } // Update users, and the corresponding permissions. foreach (GridViewRow gvr in gvItemPermissions.Rows) { Policy currentPolicy = new Policy(); currentPolicy.GroupUserName = gvr.Cells[1].Text.ToString(); rolesindex = 0; for (int cells = 2; cells < gvr.Cells.Count; cells++) { Role currentRole = new Role(); if (((CheckBox)(gvr.Cells[cells].Controls[0])).Checked) { rolesindex++; } } if (rolesindex != 0) { Role[] roles = new Role[rolesindex]; j = 0; for (int cells = 2; cells < gvr.Cells.Count; cells++) { Role currentRole = new Role(); if (((CheckBox)(gvr.Cells[cells].Controls[0])).Checked) { currentRole.Name = ((System.Web.UI.WebControls.DataControlFieldCell)(gvr.Cells[cells])).ContainingField.ToString(); roles[j] = currentRole; j++; } } currentPolicy.Roles = roles; newPolicy[i] = currentPolicy; i++; } // Let the user be associated with at least a single role. Do not allow deleting users. else { lblInfo.Text = string.Format("At least a single role must be selected for the user '{0}'.", currentPolicy.GroupUserName); ModalPopupExtender1.Show(); return; } } // Below code snippet reloads the users, and associated roles by making a round trip to the report server catalog db. RSClient rs = null; try { rs = new RSClient(); // Sets Url and credentials for the endpoint. rs.Url = String.Format("https://{0}:443/ReportServer/ReportService2010.asmx", ConfigurationManager.AppSettings["RSSERVER_NAME"]); NetworkCredential cred = new NetworkCredential( ConfigurationManager.AppSettings["RSUSERNAME"], ConfigurationManager.AppSettings["RSPASSWORD"], ConfigurationManager.AppSettings["RSSERVER_NAME"]); rs.Credentials = cred; rs.LogonUser(cred.UserName, cred.Password, cred.Domain); if (rs.CheckAuthenticated()) { // Update policies to the Report Server database rs.SetPolicies(Session["Path"].ToString(), newPolicy); if (dbroundtrip == true) { gvListChildren.DataSource = rs.ListChildren("/", true); gvListChildren.DataBind(); } } } catch { if (rs != null) { rs.Dispose(); } throw; } if (rs != null) { rs.Dispose(); } } catch { throw; } }