2015-09-21 117 views
0

我們希望在MS CRM Dynamics中實施「記錄級安全性」。在用戶案例實體,我們有一個OptionSet具有以下值,即Optionset有很多值的,下面只是簡單的數值:MS Dynamics中的記錄級安全性

  • Category 1
  • Category 2

我們要限制1類用戶僅查看第1類案件並限制第2類用戶僅查看第2類案件。

我到目前爲止做了什麼?

我在想這應該可以通過檢索插件,但是在我編寫我的代碼之後..我發現當我試圖打開一個案例記錄時,檢索插件觸發5次。它也不會拋出我的自定義錯誤。

public void Execute(IServiceProvider serviceProvider) 
    { 
     ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 
     IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 
     IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
     IOrganizationService service = factory.CreateOrganizationService(context.UserId); 

     tracer.Trace("context.Depth = " + context.Depth); 

     if (context.Depth > 1) 
      return; 

     tracer.Trace("context.Stage = " + context.Stage); 

     tracer.Trace("context.MessageName = " + context.MessageName); 


     EntityReference entityReference = (EntityReference)context.InputParameters["Target"]; 

     tracer.Trace("entityReferencee = " + entityReference.LogicalName); 

     if (context.OutputParameters != null && context.OutputParameters.Contains("BusinessEntity")) 
     { 
      if (context.OutputParameters["BusinessEntity"] is Entity) 
      { 
       Entity entity = (Entity)context.OutputParameters["BusinessEntity"]; 

       tracer.Trace("entity.LogicalName = " + entity.LogicalName); 

       context.OutputParameters["BusinessEntity"] = null; 

       throw new Exception("You can not view this record."); 

      } 
      else 
      { 
       tracer.Trace("BusinessEntity entity is not an entity."); 
      } 
     } 
     else 
     { 
      tracer.Trace("BusinessEntity entity is null"); 
     } 
    } 

這是怎樣的插件被註冊: enter image description here

錯誤: 日誌文件的enter image description here

詳細如下:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.Web.HttpUnhandledException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #CF526D62Detail:
-2147220970 System.Web.HttpUnhandledException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #CF526D62
2015-09-21T12:33:00.6154994Z -2147220956 Unexpected exception from plug-in (Execute): RestrictUserAccess.Case: System.Exception: You can not view this record. 2015-09-21T12:33:00.6154994Z

[RestrictUserAccess: RestrictUserAccess.Case] [c8860cb6-4260-e511-80ea-3863bb3600d8: RestrictUserAccess.Case: Retrieve of incident]

context.Depth = 1 context.Stage = 40 context.MessageName = Retrieve entityReferencee = incident entity.LogicalName = incident

+0

您的檢索插件的代碼?你是如何註冊你的插件的? –

+1

日誌文件('下載日誌文件')說什麼?另外,在一個側面說明,這是訪問團隊(應該是)的意圖,你有沒有考慮過這種方法? – Alex

+0

您爲哪一步註冊了您的插件?你能分享堆棧跟蹤嗎? –

回答

1

您的代碼確實拋例外,但CRM平臺將其視爲一種例外意外的錯誤。 (剛剛閱讀日誌詳細信息。)

當您需要發出功能錯誤信號時,必須拋出InvalidPluginExecutionException

系統本身可能多次檢索相同的案例記錄。同樣,您的網頁表單或功能區上的腳本也可以負責檢索相同的記錄,例如,何時需要評估記錄狀態。

因此,在檢索案例記錄時拋出異常可能不是一個有用的解決方案。另一種方法是通過從Entity.Attributes集合中刪除它們來清除所有(或所有敏感的)檢索字段。

+1

我也嘗試過InvalidPluginExecutionException ..我們決定使用Access Team來實現這個功能。 –