locked
Need to retrieve a users privileges by name (CRM 4.0) RRS feed

  • Question

  • What I need to find out is if a given user has a privilege named 'prvCreateContract'. So if I have that name and a userid what's the fastest way to confirm if the user has that privilege?

    I can do it using sql but it involves 4 tables. Thats not a problem, but I can't help to wonder if the API provides a better/ faster option to to the same?

    /c

    Thursday, September 17, 2009 9:25 AM

Answers

  • Hi,

    you have to retrieve the privilege by name in order to use its id. See the quick 'hack' below

    private static bool CheckForPrivilege(CrmService service, string privilegeName, Guid userId)
    {
        RetrieveUserPrivilegesRequest retrievePrivilegeRequest = new RetrieveUserPrivilegesRequest
        {
            UserId = userId
        };
    
        RetrieveUserPrivilegesResponse retrieved = (RetrieveUserPrivilegesResponse)service.Execute(retrievePrivilegeRequest);
    
        QueryExpression queryExpression = new QueryExpression(EntityName.privilege.ToString())
        {
            ColumnSet = new AllColumns(),
            Criteria = new FilterExpression { FilterOperator = LogicalOperator.And }
        };
    
        queryExpression.Criteria.AddCondition("name", ConditionOperator.Equal, privilegeName);
    
        RetrieveMultipleRequest request = new RetrieveMultipleRequest
        {
            Query = queryExpression,
            ReturnDynamicEntities = true
        };
    
        RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);
    
        if (response.BusinessEntityCollection.BusinessEntities.Count == 1)
        {
            DynamicEntity privilege = (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];
            Guid privilegId = ((Key)privilege["privilegeid"]).Value;
    
            foreach (RolePrivilege rolePrivilege in retrieved.RolePrivileges)
            {
                if (rolePrivilege.PrivilegeId == privilegId)
                {
                    return true;
                }
            }
        }
    
        return false;
    }
    Thursday, September 17, 2009 12:24 PM

All replies

  • Hi,

    you can use the RetrieveUserPrivileges Message (http://msdn.microsoft.com/en-us/library/bb959501.aspx) for this task. With help of this message, you get all privileges which are assigned to a systemuser.

    You have to look for the id of the privilege 'prvCreateContract' in the RolePrivileges property http://msdn.microsoft.com/en-us/library/bb929389.aspx
    Thursday, September 17, 2009 9:44 AM
  • Using the RetrieveUserPrivilegesRequest only gives me the Guid of that users privileges and not the names of them.
    /c
    Thursday, September 17, 2009 11:43 AM
  • Hi,

    you have to retrieve the privilege by name in order to use its id. See the quick 'hack' below

    private static bool CheckForPrivilege(CrmService service, string privilegeName, Guid userId)
    {
        RetrieveUserPrivilegesRequest retrievePrivilegeRequest = new RetrieveUserPrivilegesRequest
        {
            UserId = userId
        };
    
        RetrieveUserPrivilegesResponse retrieved = (RetrieveUserPrivilegesResponse)service.Execute(retrievePrivilegeRequest);
    
        QueryExpression queryExpression = new QueryExpression(EntityName.privilege.ToString())
        {
            ColumnSet = new AllColumns(),
            Criteria = new FilterExpression { FilterOperator = LogicalOperator.And }
        };
    
        queryExpression.Criteria.AddCondition("name", ConditionOperator.Equal, privilegeName);
    
        RetrieveMultipleRequest request = new RetrieveMultipleRequest
        {
            Query = queryExpression,
            ReturnDynamicEntities = true
        };
    
        RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);
    
        if (response.BusinessEntityCollection.BusinessEntities.Count == 1)
        {
            DynamicEntity privilege = (DynamicEntity)response.BusinessEntityCollection.BusinessEntities[0];
            Guid privilegId = ((Key)privilege["privilegeid"]).Value;
    
            foreach (RolePrivilege rolePrivilege in retrieved.RolePrivileges)
            {
                if (rolePrivilege.PrivilegeId == privilegId)
                {
                    return true;
                }
            }
        }
    
        return false;
    }
    Thursday, September 17, 2009 12:24 PM
  • Ahhh now I understand :-) Thank you very much.

    /c
    Thursday, September 17, 2009 12:46 PM