AX 2012: Automatically log off the user sessions

Hello guys J

Today I thought of sharing the code where the user AX sessions which doesn’t belong to admin role will automatically shut down if it’s running for more than 8 hours.

The same can be pasted in batch class to be run as batchable.


public void autoUserLogOff()
{
    SecurityUserRole        securityUserRole;
    SecurityRole            securityRole;
    container               usersList;
    SysClientSessions       sysUserSession;
    utcDateTime             dateTime, dateTimeLocal;
    TimeOfDay               totalHours;
    SysUsersTerminate       usersTerminate;
    SysUserInfo             userInfo;
    str                     timeValue;

    #define.AOTName('-SYSADMIN-')
    #define.Admin('Admin')

    dateTime = DateTimeUtil::newDateTime(systemDateGet(), timeNow());

    while select Id from userInfo
        where  userInfo.Id != #Admin
    {
        select firstOnly RecId from  securityUserRole where securityUserRole.User  == userInfo.Id
            exists  join securityRole where securityRole.RecId                     == securityUserRole.SecurityRole
                    && securityRole.AotName                                        == #AOTName;

        if (securityUserRole.RecId)
            continue;

        while select * from sysUserSession
            where sysUserSession.userId == userInfo.Id
            && sysUserSession.sessionType == SessionType::GUI
        {
            dateTimeLocal = DateTimeUtil::applyTimeZoneOffset(sysUserSession.LoginDateTime, DateTimeUtil::getUserPreferredTimeZone());

            totalHours = int642int(DateTimeUtil::getDifference(dateTime, dateTimeLocal));

            timeValue = conPeek(str2con(time2StrHM(totalHours), ":"),1);

            if( str2int(timeValue) >= 8)
            {
                usersList += [[sysUserSession.userId, sysUserSession.SessionId, sysUserSession.LoginDateTime]];
            }
        }
    }

    if (conLen(usersList) > 1)
    {
       usersTerminate = SysUsersTerminate::newUsersList(usersList);
       usersTerminate.run();
    }
}

No comments

The Moment Tokens, Vectors, and Models Click, AI Starts Making Sense

  You've probably heard these terms dozens of times by now. They're showing up in AI discussions, Copilot demos, Azure OpenAI conver...

Powered by Blogger.