如何控制SSRS報表中數據的訪問權限
1 建立用戶與維度屬性值間的關係表,refAccountDim,該表存儲了用戶能訪問的維度的屬性值,也即用戶權限
2 SSRS中建立帶有賬號參數的共享數據集,該數據集根據傳入的賬號從refAccountDim表中查詢該賬號的權限並返回,如區域
2.1 數據集輸入參數爲accountId
2.2 數據集需關聯refAccountDim表,以獲取用戶的數據權限
2.3 輸出必須是維度屬性值的表達方式,如”[日期].[年份].&[2016]”
3 在SSRS報表中,將【2】的數據集添加到報表的數據集中
3.1 此時報表參數會自動添加accountId參數
3.2 在參數中,將accountId移動到第一個參數(因爲ssrs的參數是按順序獲取的)
4 將【3】的數據集作爲相應的參數的【可用值】和【默認值來源】,這樣就實現了通過參數數據源的控制來控制對數據權限的控制
5 在web中使用ReportView加載SSRS報表,並向SSRS報表傳遞accountId參數
6 完成,當用戶訪問報表時,報表獲取到用戶的id,通過id查詢到用戶的相應維度的權限,然後通過該權限來限定用戶的查詢條件,通過查詢條件來控制用戶的數據訪問權限
private void loadReport()
{
string reportName = Request["reportName"];
if (!string.IsNullOrEmpty(reportName))
{
string accountId = getAccountIdFromCookie().ToString();
//設置服務端報表的基本參數
rptView.ServerReport.ReportServerUrl = new Uri("http://192.168.10.166/ReportServer");
rptView.ServerReport.ReportPath = "/BI-BNSY-SSRS/" + reportName;
rptView.ServerReport.ReportServerCredentials = new CustomReportCredentials("administrator", "Allcity#123", "");
rptView.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
/*
* 此種參數傳遞方式會導致沒有該參數的報表報錯
*
//獲取要傳遞的賬號信息
List<ReportParameter> rps = new List<ReportParameter>() {
new ReportParameter("accountId", accountId)
};
//設置服務端報表的參數
rptView.ServerReport.SetParameters(rps);
*/
/*
*/
//獲取服務端報表所需的參數
ReportParameterInfoCollection rptParamInfos = rptView.ServerReport.GetParameters();
//將用戶id輸入到服務端報表參數中
List<ReportParameter> rptParams = new List<ReportParameter>();
ReportParameter rptParam;
foreach (ReportParameterInfo item in rptParamInfos)
{
if (item.Name.ToLower() == "accountid")
{
rptParam = new ReportParameter();
rptParam.Name = item.Name;
rptParam.Values.Clear();
//當前用戶
rptParam.Values.Add(accountId);
rptParams.Add(rptParam);
}
}
//設置服務端報表的參數
rptView.ServerReport.SetParameters(rptParams);
//刷新報表
rptView.ServerReport.Refresh();
}
}