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;


    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)

        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);;

No comments

Powered by Blogger.