Thursday, October 5, 2017

Moving security roles from one layer to another layer while keeping users assigned to their roles in AX 2012

//Create New Table

TABLE #ST_SecurityRolesToUpdate

FIELD #IsUpdated
FIELD #NewID
FIELD #OldId
FIELD #RoleName

Index- Idx - Property - Allow Duplicate to No
NewId
OldId
RoleName


//Job to Get Current Security Role list to migrate new layer

static void ST_GetCurSecurityRoleIds(Args _args)
{
    ST_SecurityRolesToUpdate       securityRolesToUpdate;
    SecurityUserRole                securityUserRole; //Use only if wants to take against User in AX
    SecurityRole                    securityRole;
    ;
    while select securityRole
    {
        securityRolesToUpdate.OldId = securityRole.RecId;
        securityRolesToUpdate.RoleName = securityRole.AotName;
        securityRolesToUpdate.insert();
    }
}

Note:
--> In your target layer, right-click on the role and choose Duplicate. The newly created duplicate will have a name like CopyOfXYZ.
--> Go into the layer of the object - where it originally existed and delete the object
--> Go back to the target  layer and remove the CopyOf from the name of the duplicate object created   This will ensure that the name of the new object will be the same as the old object name.
--> Then run new Job

//Update security Role to new

static void ST_UserSecurityRoleUpdate(Args _args)
{
    ST_SecurityRolesToUpdate       securityRolesToUpdate;
    SecurityUserRole                securityUserRole, newUserRole;
    SecurityRole                    securityRole;
    OMUserRoleOrganization          omUserRoleOrg, newUserRoleOrg;
    CompanyInfo                     companyInfo;
    int64                           oldId;

    while select forUpdate securityRolesToUpdate join securityRole
        where securityRole.aotname == securityRolesToUpdate.RoleName
    {
        ttsBegin;
        securityRolesToUpdate.Newid = securityRole.RecId;
        securityRolesToUpdate.update();
        ttsCommit;
    }

    while select forupdate securityRolesToUpdate
        where securityRolesToUpdate.oldId != securityRolesToUpdate.newid &&
        securityRolesToUpdate.IsUpdated == NoYes::No &&
        securityRolesToUpdate.NewID != 0 && securityRolesToUpdate.OldId != 0
    {
        while select securityUserRole
            join securityRole
            where securityUserRole.SecurityRole == securityRolesToUpdate.OldId &&
                securityrole.AotName == securityRolesToUpdate.RoleName
        {
            oldId = securityUserRole.SecurityRole;
            newUserRole.User = securityUserRole.User;
            newUserRole.SecurityRole = securityRolesToUpdate.NewId;
            newUserRole.AssignmentMode = securityUserRole.AssignmentMode;
            newUserRole.AssignmentStatus = securityUserRole.AssignmentStatus;
            SecuritySegregationOfDuties::assignUserToRole(newUserRole);
        }

        ttsBegin;
        securityRolesToUpdate.IsUpdated = NoYes::Yes;
        securityRolesToUpdate.update();
        ttsCommit;

        while select omUserRoleOrg
            join companyInfo
            where omUserRoleOrg.OMInternalOrganization == companyInfo.RecId &&
            omUserRoleOrg.SecurityRole == oldId
        {
            ttsBegin;

            newUserRoleOrg.User = omUserRoleOrg.User;
            newUserRoleOrg.SecurityRole = securityRolesToUpdate.NewId;
            newUserRoleOrg.OMInternalOrganization = omUserRoleOrg.OMInternalOrganization;
            newUserRoleOrg.SecurityRoleAssignmentRule = omUserRoleOrg.SecurityRoleAssignmentRule;
            newUserRoleOrg.OMHierarchyType = 0;

            EePersonalDataAccessLogging::logUserRoleChange(newUserRoleOrg.SecurityRole,
                newUserRoleOrg.omInternalOrganization,
                newUserRoleOrg.User,
                AddRemove::Add);

            newUserRoleOrg.insert();

            ttsCommit;
        }

    }

    info("Done");
}

No comments:

Post a Comment

Convert Call stack to readable format in D365FO X++

//Input --container _xppCallStack = xSession::xppCallStack();  Public static str POL_formatXppCallStack(container _xppCallStack, int _skipFr...