X++ Code to remove the Role for single or multiple user [Dynamics AX 2012]




Removing a role to all or multiple users using X++ Code [Dynamics AX 2012]


static void RB_RemoveRoleAccessToUsers(Args _args)
{
    SecurityRole            role;
    SecurityUserRole    userRole;
    UserInfo                   userInfo;

    void removeFromSelectedUser(UserId  _userId, RecId  _recId)
    {
        fieldName                                                userId;
        SysSecTreeRoles                                     roleTree;
        SecurityUserRole                                     securityUserRole;
        OMUserRoleOrganization                       org;
        SecurityUserRoleCondition                     condition;
        SecuritySegregationOfDutiesConflict     conflict;
        RecId                                                        recId;

        userId  = _userId;
        recId   = _recId;

        ttsbegin;

        delete_from condition
                        exists join  securityUserRole
                        where  condition.SecurityUserRole == securityUserRole.RecId                                                               &&     securityUserRole.User == userId
                        &&     securityUserRole.SecurityRole == recId;


        while select OMInternalOrganization, SecurityRole from org
                   where org.User == userId && org.SecurityRole == recid
        {
               EePersonalDataAccessLogging::logUserRoleChange(org.SecurityRole,                org.omInternalOrganization, userid, AddRemove::Remove);
        }
     

        delete_from org where org.User == userId && org.SecurityRole == recId;

        delete_from conflict where conflict.User == userId && ((conflict.ExistingRole == recId) || (conflict.NewRole == recId));

     
        EePersonalDataAccessLogging::logUserRoleChange(recId, 0, userId, AddRemove::Remove);
 

        delete_from securityUserRole
               where    securityUserRole.User == userId                                                
                &&       securityUserRole.SecurityRole == recId;

        ttscommit;

    }
  // provide the role name to remove below
    select role where role.Name == "System administrator";
 
    // ensure that you have admin role to run this job
    while select userInfo where (userInfo.id != 'Admin'
        &&  userInfo.id != 'rbalakri')
    {
           removeFromSelectedUser(userInfo.id, role.RecId);
    }
    info("Removal process of role is complete.");
}

No comments:

Post a Comment