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

后端 未结 2 1250
时光取名叫无心
时光取名叫无心 2020-12-11 19:22

I\'m working on MS CRM plugin, and it should be able to determine whether the current user has write access to the current entity. I don\'t know how to approach this task.

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 20:15

    Here is what I have come up with — this code will check, does current user has given privilege on current record:

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

    The code inside if statement will be executed if user has WriteAccess to current record.

提交回复
热议问题