[英]How to get current user privileges in MS Dynamics CRM on server side
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. 我正在使用MS CRM插件,它应该能够确定当前用户是否具有对当前实体的写权限。 I don't know how to approach this task.
我不知道如何完成这项任务。
It seems that the most user-friendly way accomplish this task is currently unsupported . 似乎目前尚不支持以最用户友好的方式完成此任务。
Is there any alternative in MS CRM 2011 SDK, except composing a FetchXML query and parsing its output? 除了组成FetchXML查询并解析其输出之外,MS CRM 2011 SDK中还有其他选择吗?
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. 如果用户对当前记录具有
WriteAccess
,则将执行if
语句中的代码。
According to Matt's Answer: 根据马特的答案:
You have just have to perform the joins and add the where clause you care about. 您只需要执行联接并添加您关心的where子句。 Here is the Equivalent SQL:
这是等效的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
You can perform this using Fetch XML, or LINQ to CRM, or Query Expressions, or even OData. 您可以使用Fetch XML,LINQ to CRM,Query Expressions甚至OData来执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.