Inject userSession in EntityListener not working

Inject userSession in EntityListener not working

Screen Shot 2017-10-16 at 12.22.35 PM

Screen Shot 2017-10-16 at 12.22.47 PM

Hi,
try injecting UserSessionSource instead, and use checkCurrentUserSession() method before trying to get it.

See the following helper method, that first checks if a valid session is present for current thread, and then additionally check that the session IS NOT the system session or the anonymous user’s one.
If these checks fail, the method returns null, otherwise the current UserSession


    @Inject
    protected UserSessionSource userSessionSource;

    protected UserSession getUserSession() {
        if (userSessionSource.checkCurrentUserSession()) {
            final UserSession userSession = userSessionSource.getUserSession();
            if (!userSession.isSystem() && !userSession.getId().equals(globalConfig.getAnonymousSessionId())) {
                return userSession;
            }
        }
        return null;
    }

Bye,
Paolo

UserSession itself can be injected only to screen controllers. Other beans can obtain UserSession only via UserSessionSource. This is because instances of controllers always belong to a session, while generic beans are singletons and serve all users.

Thank you guys!