/**
   * Setup common variables used by almost all templates.
   *
   * @param context SimpleHash The context to use
   * @param jforumContext JForumContext
   */
  public void prepareTemplateContext(SimpleHash context, ForumContext jforumContext) {
    RequestContext request = JForumExecutionContext.getRequest();

    context.put("karmaEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED));
    context.put("dateTimeFormat", SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
    context.put("autoLoginEnabled", SystemGlobals.getBoolValue(ConfigKeys.AUTO_LOGIN_ENABLED));
    context.put(
        "sso", ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE)));
    context.put("contextPath", request.getContextPath());
    context.put("serverName", request.getServerName());
    context.put("templateName", SystemGlobals.getValue(ConfigKeys.TEMPLATE_DIR));
    context.put("extension", SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
    context.put("serverPort", Integer.toString(request.getServerPort()));
    context.put("I18n", I18n.getInstance());
    context.put("version", SystemGlobals.getValue(ConfigKeys.VERSION));
    context.put("forumTitle", SystemGlobals.getValue(ConfigKeys.FORUM_PAGE_TITLE));
    context.put("pageTitle", SystemGlobals.getValue(ConfigKeys.FORUM_PAGE_TITLE));
    context.put("metaKeywords", SystemGlobals.getValue(ConfigKeys.FORUM_PAGE_METATAG_KEYWORDS));
    context.put(
        "metaDescription", SystemGlobals.getValue(ConfigKeys.FORUM_PAGE_METATAG_DESCRIPTION));
    context.put("forumLink", SystemGlobals.getValue(ConfigKeys.FORUM_LINK));
    context.put("homepageLink", SystemGlobals.getValue(ConfigKeys.HOMEPAGE_LINK));
    context.put("encoding", SystemGlobals.getValue(ConfigKeys.ENCODING));
    context.put(
        "bookmarksEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_BOOKMARKS_ENABLED));
    context.put(
        "canAccessModerationLog",
        SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_LOG));
    context.put("JForumContext", jforumContext);
    context.put("timestamp", new Long(System.currentTimeMillis()));
  }
  /**
   * Do a refresh in the user's session. This method will update the last visit time for the current
   * user, as well checking for authentication if the session is new or the SSO user has changed
   */
  public void refreshSession() {

    LOG.trace("refreshSession");
    UserSession userSession = SessionFacade.getUserSession();
    RequestContext request = JForumExecutionContext.getRequest();

    if (userSession == null) {
      userSession = new UserSession();
      userSession.registerBasicInfo();
      userSession.setSessionId(request.getSessionContext().getId());
      userSession.setIp(request.getRemoteAddr());
      SessionFacade.makeUnlogged();

      if (!JForumExecutionContext.getForumContext().isBot()) {
        // Non-SSO authentications can use auto login
        if (!ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
          if (SystemGlobals.getBoolValue(ConfigKeys.AUTO_LOGIN_ENABLED)) {
            this.checkAutoLogin(userSession);
          } else {
            userSession.makeAnonymous();
          }
        } else {
          this.checkSSO(userSession);
        }
      }

      SessionFacade.add(userSession);
    } else if (ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
      SSO sso;

      try {
        sso =
            (SSO)
                Class.forName(SystemGlobals.getValue(ConfigKeys.SSO_IMPLEMENTATION)).newInstance();
      } catch (Exception e) {
        throw new ForumException(e);
      }

      // If SSO, then check if the session is valid
      if (!sso.isSessionValid(userSession, request)) {
        SessionFacade.remove(userSession.getSessionId());
        refreshSession();
      }
    } else {
      SessionFacade.getUserSession().updateSessionTime();
    }
  }