簡體   English   中英

如何在服務器端的MS Dynamics CRM中獲得當前的用戶特權

[英]How to get current user privileges in MS Dynamics CRM on server side

我正在使用MS CRM插件,它應該能夠確定當前用戶是否具有對當前實體的寫權限。 我不知道如何完成這項任務。

似乎目前尚不支持以最用戶友好的方式完成此任務。

除了組成FetchXML查詢並解析其輸出之外,MS CRM 2011 SDK中還有其他選擇嗎?

這是我想出的—這段代碼將檢查當前用戶是否對當前記錄賦予了特權:

// Requesting user's access rights to current record
var principalAccessRequest = new RetrievePrincipalAccessRequest
{
    Principal = new EntityReference("systemuser", localContext.PluginExecutionContext.UserId),
    Target = new EntityReference(localContext.PluginExecutionContext.PrimaryEntityName, localContext.PluginExecutionContext.PrimaryEntityId)
};

// Response will contain AccessRights mask, like AccessRights.WriteAccess | AccessRights.ReadAccess | ...
var principalAccessResponse = (RetrievePrincipalAccessResponse)localContext.OrganizationService.Execute(principalAccessRequest);

if ((principalAccessResponse.AccessRights & AccessRights.WriteAccess) != AccessRights.None)
{
    ...
    ...
    ...
}

如果用戶對當前記錄具有WriteAccess ,則將執行if語句中的代碼。

根據馬特的答案:

  1. 檢索實體特權
  2. 加入實體roleprivilege,其中privilege.privilegeid = roleprivilege.privilegeid
  3. 加入實體systemuserrole,其中systemuserrole.roleid = roleprivileges.roleid和systemuserrole.systemuserid =(相關用戶的GUID)
  4. 然后要么遍歷特權,要么尋找特權,其中privilege.name =“ prvReadMyEntityName”

您只需要執行聯接並添加您關心的where子句。 這是等效的SQL:

SELECT Privilege.*
FROM Privilege
INNER JOIN RolePrivilege ON Privilege.PrivilegeId = RolePrivilege.PrivilegeId
INNER JOIN SystemUserRole ON SystemUserRole.RoleId = RolePrivileges.RoleId AND SystemUserRole.SystemUserId = (user's GUID)
-- WHERE Add whatever constraints on the Privilege entity that you need

您可以使用Fetch XML,LINQ to CRM,Query Expressions甚至OData來執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM