这是我的代码:
RetrievePrincipalAccessRequest request = new RetrievePrincipalAccessRequest();
request.Principal = new EntityReference("systemuser", new Guid("GUID of system user record."));
// record for which we want to check the access
request.Target = new EntityReference("account", new Guid("GUID of account record."));
try
{
RetrievePrincipalAccessResponse responce = (RetrievePrincipalAccessResponse)organisationservice.Execute(request);
}
catch (Exception ex)
{
Console.WriteLine("error" + ex.Message.ToString());
}
取出“可以访问|可以编辑|可以读取”系统用户的权限和 a 安全主体可以是用户或团队但不是安全角色,因此有任何C#代码可以使用安全角色检索权限。
我有动态CRM Online实例。
谢谢: - )
答案 0 :(得分:3)
是的,如果您要检索安全角色的所有权限,可以使用RetrieveRolePrivilegesRoleRequest,如下所示:
var roleRequest = new RetrieveRolePrivilegesRoleRequest { RoleId = new Guid("securityRoleId") };
var roleReponse = service.Execute(roleRequest);
将"securityRoleId"
替换为securityrole.Id.ToString()
,将service
替换为organisationservice
。
roleResponse.RolePrivileges
将包含一长串特权ID,这些特权ID本身并不是很有用。您可以获得所有这些权限的名称,如下所示:
var privilegeQuery = new QueryExpression { EntityName = "privilege", ColumnSet = new ColumnSet(true) };
var filter = new FilterExpression(LogicalOperator.Or);
foreach (var p in roleResponse.RolePrivileges)
filter.AddCondition("privilegeid", ConditionOperator.Equal, p.PrivilegeId);
privilegeQuery.Criteria = filter;
var privileges = service.RetrieveMultiple(privilegeQuery);
现在,您将拥有存储在privileges
中的给定安全角色的所有权限的所有列(属性)。
我想/希望能回答你的问题。但是,为了更进一步,您可以按照他们引用的实体过滤您的privileges
:
var contactPrivileges = privileges.Entities.ToList()
.Where(p => p.GetAttributeValue<string>("name").ToLower()
.Contains("contact"))
.ToList();
答案 1 :(得分:0)
我找到了另一个解决方案:
QueryExpression query = new QueryExpression
{
EntityName = "role"
};
//Get all Security Roles
EntityCollection Securityroles = organisationservice.RetrieveMultiple(query);
//Get all Privileges
RetrievePrivilegeSetRequest requestp = new RetrievePrivilegeSetRequest();
RetrievePrivilegeSetResponse responsep = (RetrievePrivilegeSetResponse)organisationservice.Execute(requestp);
foreach (Entity securityrole in Securityroles.Entities)
{
//Get record from RolePrivilege Mapping
RetrieveRolePrivilegesRoleRequest req = new RetrieveRolePrivilegesRoleRequest();
req.RoleId = new Guid(securityrole.Id.ToString());
RetrieveRolePrivilegesRoleResponse response = (RetrieveRolePrivilegesRoleResponse)organisationservice.Execute(req);
foreach (RolePrivilege priv in response.RolePrivileges)
{
var privile = responsep.EntityCollection.Entities.Where(a => a.Id == priv.PrivilegeId).ToArray();
}
}
答案 2 :(得分:0)
对于那些在Dave Clark代码上出错的人,请替换以下代码
var roleRequest = new RetrieveRolePrivilegesRoleRequest { RoleId = new Guid(roleId.ToString()) };
var roleReponse = service.Execute(roleRequest);
与
RetrieveRolePrivilegesRoleRequest roleRequest = new RetrieveRolePrivilegesRoleRequest { RoleId = new Guid(roleId.ToString()) };
RetrieveRolePrivilegesRoleResponse roleResponse = (RetrieveRolePrivilegesRoleResponse)_service.Execute(roleRequest);