Thursday, October 3, 2013

Copy User Groups from one User to another

I'm doing more AX administration work than usual lately.  When a new employee arrives, the typical statement is "give them the same security as _____" (some existing user).  The code below will copy user groups from one user to another.

I select the New user row and click Copy User Groups (the new button I created)




That opens a dialog with the To User populated.

And a infolog displays the results:

So, here's the code.

In your new button:
//NDP 10/3/13
void clicked()
{
    ;
    if (securityHelper::copyUserGroups("",UserInfo.id))
    {
        //refresh the list panel on the user group tab
        listPanel.fill(true);
    }
}

Create a new class called "securityHelper" with the static method below.  You could also put this code in the SysUserInfo form if you like.
//NDP 10/3/13
static boolean copyUserGroups(UserId _fromUserId = '', UserId _toUserId = '')
{
    UserGroupList       userGroupList, userGroupDupeCheck, userGroupListInsert;
    userInfo            userInfo;
    UserId              fromUserId, toUserId;
    int                 i;
    boolean             okToRun, deleteGroupAssignmentFirst;
    dialog              dialog;
    dialogField         fromUser, toUser, deleteFirst;
    FormStringControl   formStringControl;
    FormGroupControl    formGroupControl;
    ;

    dialog = new Dialog("Copy User Groups");
    fromUser = dialog.addField(TypeId(UserId),"Copy Groups From User");
    toUser = dialog.addField(TypeId(UserId),"Copy Groups To User");
    deleteFirst = dialog.addField(TypeId(NoYesId),"Delete existing To User groups before copy?");

    if (_fromUserId)
    {
        fromUser.value(_fromUserId);
    }
    if (_toUserId)
    {
        toUser.value(_toUserId);
    }

    formStringControl = fromUser.fieldControl();
    formStringControl.mandatory(true);
    formStringControl = toUser.fieldControl();
    formStringControl.mandatory(true);

    if (dialog.run())
    {
        fromUserId = fromUser.value();
        toUserId = toUser.value();

        if (fromUserId == '' || toUserId == '')
        {
            error("User Group Copy Cancelled: Please enter From and To User Ids");
            return false;
        }

        select firstonly recid from userInfo
            where userInfo.Id == fromUserId;

        if (userInfo.RecId == 0)
        {
            error(strfmt("User %1 not found",fromUserId));
            return false;
        }

        select firstonly recid from userInfo
            where userInfo.Id == toUserId;

        if (userInfo.RecId == 0)
        {
            error(strfmt("User %1 not found",toUserId));
            return false;
        }

        if (deleteFirst.value() == NoYes::Yes)
        {
            delete_from userGroupList
                where userGroupList.userId == toUserId;

            info(strfmt("Existing groups were deleted for %1",toUserId));
        }

        while select userGroupList
            where userGroupList.UserId == fromUserId
        {
            select firstonly recid from userGroupDupeCheck
                where userGroupDupeCheck.userId == toUserId
                && userGroupDupeCheck.groupId == userGroupList.groupId;

            if (userGroupDupeCheck.RecId == 0)
            {
                userGroupListInsert.clear();
                userGroupListInsert.initValue();
                userGroupListInsert.groupId = userGroupList.groupId;
                userGroupListInsert.userId = toUserId;
                userGroupListInsert.insert();
                i++;
                info(userGroupListInsert.groupId);
            }
        }
        info(strfmt("%1 groups copied from %2 to %3",i,fromUserId, toUserId));
        return true;
    }
    else
    {
        info("User Group Copy Cancelled");
        return false;
    }
}

The code validates a number of things...but it does allow you to take yourself out of the admin group.  So be careful of that!

Enjoy.

No comments:

Post a Comment