Dynamics CRM 365 如何判断用户是否有某个实体的查询权限


1.C#代码

// 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)
{
    ...
    ...
    ...
}

2.SQL

检索实体特权

加入实体角色特权,其中 privilege.privilegeid = roleprivilege.privilegeid

加入实体 systemuserrole,其中 systemuserrole.roleid = roleprivileges.roleid 和 systemuserrole.systemuserid =(相关用户的 GUID)

然后遍历权限或查找权限 where 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