How to copy roles from one user to another in Dynamics AX2012

Updated: 19.02.2014

Today we would like to share How to copy roles from one user to another in Dynamics AX2012.

Code example:

    #static void assignRolesFromUserToUser(Args _args)
    #{
    #    #define.fromUserId('Kudryavc')
    #    #define.toUserId('ax12test')
    #
    #    SecurityRole            securityRole;
    #    SecurityUserRole        securityUserRoleFrom,securityUserRoleTo;
    #    ;
    #    while select securityUserRoleFrom
    #        where   securityUserRoleFrom.User               == #fromUserId
    #            &&  securityUserRoleFrom.AssignmentStatus   == RoleAssignmentStatus::Enabled
    #        join securityRole
    #        where   securityRole.RecId                      == securityUserRoleFrom.SecurityRole
    #    {
    #        select securityUserRoleTo
    #            where securityUserRoleTo.SecurityRole == securityUserRoleFrom.SecurityRole &&
    #                securityUserRoleTo.User == #toUserId;
    #
    #        if (!securityUserRoleTo || securityUserRoleTo.AssignmentStatus != RoleAssignmentStatus::Enabled)
    #        {
    #            SysSecHelper::log('Adding ' + securityRole.Name);
    #
    #            securityUserRoleTo.User = #toUserId;
    #            securityUserRoleTo.SecurityRole = securityRole.RecId;
    #            securityUserRoleTo.AssignmentMode = RoleAssignmentMode::Manual;
    #            securityUserRoleTo.AssignmentStatus = RoleAssignmentStatus::Enabled;
    #
    #            if (SecuritySegregationOfDuties::assignUserToRole(securityUserRoleTo, null) )
    #            {
    #                EePersonalDataAccessLogging::logUserRoleChange(securityRole.RecId,0, #toUserId, AddRemove::Add);
    #            }
    #            else
    #            {
    #                SysSecHelper::log('Skipping ' + securityRole.Name);
    #            }
    #        }
    #        else
    #        {
    #            SysSecHelper::log('Skipping ' + securityRole.Name);
    #        }
    #    }
    #
    #}


 

 

Other blog posts

24.05.2018
Disclaimer
08.05.2018
We apply OKR methodology in Neti since 2014. We started from a quarterly planning, but then understood that no ambitious goals can be achieved per quarter. So we switched to semi-annual intervals....
11.04.2018
As you know, Microsoft recently has launched Dynamics 365 Business Central. And we are proud to announce we have launched intracompany training for sales and development departments:Why do we need it...

Subscribe for blog updates

Get updated with our blog new articles via your email!


Subscribe now