如何更新给定用户列表的角色? [英] How can I update the roles for a given list of users?

查看:54
本文介绍了如何更新给定用户列表的角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将整个用户负载从1个业务部门转移到新设置1.无论如何我都不是CRM专家,并且已经得知要转移需要存储的用户它们的现有角色,然后将它们移至新的BU,然后还原非特定于BU的角色,然后对其进行修改.用户的示例为:

I'm trying to move a whole load of users from 1 business unit to a newly setup 1. I'm not a CRM expert by any means, and I've been informed that to move the users we need to store their existing roles first, then move them to the new BU and then restore their roles that aren't BU specific and then modify the ones that are. An example for a user would be:

BU1
角色:
BU1Admin
BU1只读
合同

BU1
Roles:
BU1Admin
BU1Read-Only
Contracts

将其移动到:BU2
角色:
BU2Admin
BU2只读
合同

Move these to: BU2
Roles:
BU2Admin
BU2Read-Only
Contracts

因此,在此示例中,用户将需要将其管理员角色和只读角色修改为BU2,但合同保持不变.我最初的想法是使用以下方式构造一个查询以检索systemuserid以及角色名称:

So in this example the user would need to have their admin role and read-only role modified to BU2 but the contracts one stays the same. My initial thought was to construct a query to retrieve the systemuserid's along with the role names using something like this:

            Dictionary<Guid, string> UserRoles = new Dictionary<Guid, string>();

            QueryExpression query = new QueryExpression();
            query.EntityName = "systemuserroles";
            ColumnSet cols = new ColumnSet();
            cols.Attributes = new string[] { "systemuserid", "roleid" };
            query.ColumnSet = cols;

            LinkEntity le = new LinkEntity();
            le.LinkFromEntityName = "systemuserroles";
            le.LinkFromAttributeName = "roleid";
            le.LinkToEntityName = "role";
            le.LinkToAttributeName = "roleid";

            LinkEntity le2 = new LinkEntity();
            le2.LinkFromEntityName = "systemuserroles";
            le2.LinkFromAttributeName = "systemuserid";
            le2.LinkToEntityName = "systemuser";
            le2.LinkToAttributeName = "systemuserid";

            // Find only users in BU1
            ConditionExpression ce = new ConditionExpression();
            ce.AttributeName = "businessunitid";
            ce.Operator = ConditionOperator.Equal;
            ce.Values = new string[] { BU1 Guid };

            le2.LinkCriteria = new FilterExpression();
            le2.LinkCriteria.Conditions = new ConditionExpression[] { ce };

            query.LinkEntities = new LinkEntity[] { le, le2 };

            try
            {
                //This call doesn't work and fails saying RetrieveMultiple doesn't support entities of type systemuserroles.
                BusinessEntityCollection UserRolesCollection = crmService.RetrieveMultiple(query);
                foreach (BusinessEntity be in UserRolesCollection.BusinessEntities)
                {
                     //Do my stuff here     
                }
            }
            catch (SoapException se)
            {
                throw new Exception("Error occurred." + se.Detail);
            }

下一步,我要执行的操作是使用新角色更新用户.我什至不知道是否可以根据上述问题解决此问题.任何帮助将非常感激.我想知道DynamicEntity在阅读?

The next step, which I want to be able to do is then update the users with the new roles. I don't even know if this can be done based on the problems I'm having with the above. Any help would be much appreciated. I'm wondering if DynamicEntity would be of any use here after reading this?

更新:

UPDATE:

您似乎可以使用AssignUserRolesRole Request类在此处更新用户角色.但是,目前我仍然停留在检索位上.我想知道是否需要使用sql吗?

It looks like you can update a users roles using the AssignUserRolesRole Request class here. However, I'm still stuck on the retrieval bit at the moment as well. I'm wondering if I need to resort to sql?

推荐答案

我终于设法找到一种通过API进行操作的方法.本质上是2个查询,第一个查询获取所有用户的systemuserid,第二个查询获取每个systemuserid的角色ID.这将为我提供所有用户的当前角色.

I finally managed to find a way to do it through the API. It's essentially 2 queries, 1 to get the systemuserid of all the users and the second to get the roleids for each systemuserid. This will give me the current roles of all the users.

我创建了一个字典,将旧角色向导映射到新角色向导.

I create a dictionary that maps the old roles guids to the new roles guids.

然后,我更改他们的业务部门,从而摆脱了他们的所有角色,然后使用字典和以前存储的用户角色,可以将其映射到每个用户的新角色.我为处于类似情况的任何人提供了此代码:

I then change their business unit, which gets rid of all their roles, and using the dictionary and previously stored user roles I can then map to the new roles for each user. I came up with this code for anyone in a similar situation:

private void UpdateUsers()
{
    //Create mapping of old guid to new guid.
    Dictionary<Guid, Guid> LookupRoleGuids = new Dictionary<Guid, Guid>();
    //Add guid mapping
    LookupRoleGuids.Add(new Guid(Old Guid), new Guid(New Guid));

    QueryExpression query = new QueryExpression();
    query.EntityName = "systemuser";
    query.ColumnSet = new AllColumns();

    ConditionExpression ce2 = new ConditionExpression();
    ce2.AttributeName = "systemuserid";
    ce2.Operator = ConditionOperator.Equal;
    ce2.Values = new string[] { User Id }; //Can alter to retrieve for a BU

    FilterExpression filter = new FilterExpression();
    filter.Conditions = new ConditionExpression[] { ce2 };
    query.Criteria = filter;

    try
    {
        BusinessEntityCollection UserCollection = crmService.RetrieveMultiple(query);

        //store the roles of the Users.
        StoreUserRoles(UserCollection);

        foreach (BusinessEntity be in UserCollection.BusinessEntities)
        {
            //Update users BU.
            Guid newBu = new Guid(New BU Guid);
            systemuser su = (systemuser)be;
            SetBusinessSystemUserRequest setBUreq = new SetBusinessSystemUserRequest();
            setBUreq.BusinessId = newBu;
            setBUreq.UserId = su.systemuserid.Value;
            SecurityPrincipal assignee = new SecurityPrincipal();
            assignee.PrincipalId = new Guid(su.systemuserid.Value.ToString());
            setBUreq.ReassignPrincipal = assignee;
            SetBusinessSystemUserResponse assigned = (SetBusinessSystemUserResponse)crmService.Execute(setBUreq);

            //Get users existing roles
            if (UserRolesList.Count() > 0)
            {
                UserRoles ur = UserRolesList.Where(x => x.Value == su.systemuserid.Value)
                                            .Select(x => x).First();

                //Get new role guids based on mapping
                Guid[] roleguids = LookupRoleGuids.Where(x => ur.Roles.Contains(x.Key))
                                                  .Select(x => x.Value)
                                                  .ToArray();

                //Assign new roles 
                AssignUserRolesRoleRequest addRoles = new AssignUserRolesRoleRequest();
                addRoles.UserId = su.systemuserid.Value;
                addRoles.RoleIds = roleguids;
                crmService.Execute(addRoles);
            }
        }
    }
    catch (Exception ex)
    {
        throw new Exception("Error occurred updating users BU or assigning new roles. Error: " + ex.Message);
    }
}

public void StoreUserRoles(BusinessEntityCollection UserRolesCollection)
{
    UserRolesList = new List<UserRoles>(); 

    foreach (BusinessEntity be in UserRolesCollection.BusinessEntities)
    {
        systemuser u = (systemuser)be;
        UserRoles ur = new UserRoles();
        ur.Username = u.domainname;
        ur.Value = u.systemuserid.Value;
        UserRolesList.Add(ur);
    }

    AddRolesToList(ref UserRolesList);
}

private void AddRolesToList(ref List<UserRoles> URList)
{
    //Get all roles for a given user guid
    QueryExpression query = new QueryExpression();
    query.EntityName = "role";
    ColumnSet cols = new ColumnSet();
    cols.Attributes = new string[] { "roleid" };
    query.ColumnSet = cols;

    LinkEntity le = new LinkEntity();
    le.LinkFromEntityName = "role";
    le.LinkToEntityName = "systemuserroles";
    le.LinkFromAttributeName = "roleid";
    le.LinkToAttributeName = "roleid";

    LinkEntity le2 = new LinkEntity();
    le2.LinkFromEntityName = "systemuserroles";
    le2.LinkToEntityName = "systemuser";
    le2.LinkFromAttributeName = "systemuserid";
    le2.LinkToAttributeName = "systemuserid";

    foreach(UserRoles ur in URList)
    {
        //loop through the list of userroles and alter the conditional expression with the user's guid.
        ConditionExpression ce = new ConditionExpression();
        ce.AttributeName = "systemuserid";
        ce.Operator = ConditionOperator.Equal;
        ce.Values = new string[] { ur.Value.ToString() };

        le2.LinkCriteria = new FilterExpression();
        le2.LinkCriteria.Conditions = new ConditionExpression[] { ce };

        le.LinkEntities = new LinkEntity[] { le2 };
        query.LinkEntities = new LinkEntity[] { le };

        try
        {
            BusinessEntityCollection RoleGuidsCollection = crmService.RetrieveMultiple(query);
            foreach (BusinessEntity be in RoleGuidsCollection.BusinessEntities)
            {
                role r = (role)be;
                ur.Roles.Add(r.roleid.Value);
            }
        }
        catch (SoapException se)
        {
            throw new Exception("Error occurred retrieving Role Ids for " + ur.Username + " (" + ur.Value + "). " + se.Detail.InnerXml);
        }
        catch (Exception ex)
        {
            throw new Exception("Error occurred retrieving Role Guids for " + ur.Username + " (" + ur.Value + "). " + ex.Message);
        }
    }
}

要存储每个用户的角色,我创建了一个名为UserRoles的类:

To store the roles for each user I've created a class called UserRoles:

class UserRoles
{
    public string Username { get; set; }
    public Guid Value { get; set; }
    public List<Guid> Roles { get; set; }

    public UserRoles()
    {
        Roles = new List<Guid>();
    }
}

注意::此操作尚未经过全面测试,但确实可以满足我的要求,这使我可以将用户转移到其他BU,并根据他们的旧设置为其设置新角色一个.

NOTE: This has not been fully tested, but does seem to do what I want, which is allow me to move a user to a different BU and set new roles for them based on their old ones.

这篇关于如何更新给定用户列表的角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆