How to get current user privileges in MS Dynamics CRM on the server side

I am working on an MS CRM plugin and it should be able to determine if the current user has write access to the current object. I do not know how to approach this task.

It seems that the most convenient way to accomplish this task is not currently supported .

Is there any alternative in the MS CRM 2011 SDK except compiling a FetchXML request and analyzing its output?

+5
source share
3 answers

Here's what I came up with - this code checks to see if the current user has really granted privilege to the current record:

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

if , WriteAccess .

+9

Matt :

  • roleprivilege, privilege.privilegeid = roleprivilege.privilegeid
  • systemuserrole, systemuserrole.roleid = roleprivileges.roleid systemuserrole.systemuserid = (GUID )
  • , , 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 GUID)
-- WHERE Add whatever constraints on the Privilege entity that you need

, Fetch XML, LINQ to CRM, Query Expressions, OData.

+1

Check this one .

Its essence is as follows:

//Check if user is connected for on-prem or hosted

Guid userGuid;
if(Request.LogonUserIdentity.IsAuthenticated == true)
{
    WhoAmIRequest userRequest = new Microsoft.Crm.SdkTypeProxy.WhoAmIRequest();
    WhoAmIResponse user = (Microsoft.Crm.SdkTypeProxy.WhoAmIResponse)objCrmService.Execute    (userRequest);
    userGuid = user.UserId;
}
else //ifd
{
    userGuid = new Guid(Context.User.Identity.Name);
}
-2
source

All Articles