Search in sources :

Example 1 with UserRepository

use of org.b3log.symphony.repository.UserRepository in project symphony by b3log.

the class SymphonyServletListener method resolveSkinDir.

/**
 * Resolve skin (template) for the specified HTTP servlet request.
 *
 * @param request the specified HTTP servlet request
 */
private void resolveSkinDir(final HttpServletRequest request) {
    Stopwatchs.start("Resolve skin");
    request.setAttribute(Keys.TEMAPLTE_DIR_NAME, (Boolean) request.getAttribute(Common.IS_MOBILE) ? "mobile" : "classic");
    try {
        final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);
        final UserRepository userRepository = beanManager.getReference(UserRepository.class);
        final OptionRepository optionRepository = beanManager.getReference(OptionRepository.class);
        final JSONObject optionLang = optionRepository.get(Option.ID_C_MISC_LANGUAGE);
        final String optionLangValue = optionLang.optString(Option.OPTION_VALUE);
        if ("0".equals(optionLangValue)) {
            Locales.setLocale(request.getLocale());
        } else {
            Locales.setLocale(Locales.getLocale(optionLangValue));
        }
        JSONObject user = userQueryService.getCurrentUser(request);
        if (null == user) {
            final Cookie[] cookies = request.getCookies();
            if (null == cookies || 0 == cookies.length) {
                return;
            }
            try {
                for (final Cookie cookie : cookies) {
                    if (!Sessions.COOKIE_NAME.equals(cookie.getName())) {
                        continue;
                    }
                    final String value = Crypts.decryptByAES(cookie.getValue(), Symphonys.get("cookie.secret"));
                    if (StringUtils.isBlank(value)) {
                        break;
                    }
                    final JSONObject cookieJSONObject = new JSONObject(value);
                    final String userId = cookieJSONObject.optString(Keys.OBJECT_ID);
                    if (Strings.isEmptyOrNull(userId)) {
                        break;
                    }
                    user = userRepository.get(userId);
                    if (null == user) {
                        return;
                    } else {
                        break;
                    }
                }
            } catch (final Exception e) {
                LOGGER.log(Level.ERROR, "Read cookie failed", e);
            }
            if (null == user) {
                return;
            }
        }
        request.setAttribute(Keys.TEMAPLTE_DIR_NAME, (Boolean) request.getAttribute(Common.IS_MOBILE) ? user.optString(UserExt.USER_MOBILE_SKIN) : user.optString(UserExt.USER_SKIN));
        request.setAttribute(UserExt.USER_AVATAR_VIEW_MODE, user.optInt(UserExt.USER_AVATAR_VIEW_MODE));
        request.setAttribute(User.USER, user);
        final Locale locale = Locales.getLocale(user.optString(UserExt.USER_LANGUAGE));
        Locales.setLocale(locale);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Resolves skin failed", e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : Cookie(javax.servlet.http.Cookie) Locale(java.util.Locale) UserRepository(org.b3log.symphony.repository.UserRepository) JSONObject(org.json.JSONObject) UserQueryService(org.b3log.symphony.service.UserQueryService) OptionRepository(org.b3log.symphony.repository.OptionRepository) ServiceException(org.b3log.latke.service.ServiceException)

Example 2 with UserRepository

use of org.b3log.symphony.repository.UserRepository in project symphony by b3log.

the class UserChannel method updateUserOnlineFlag.

private void updateUserOnlineFlag(final String userId, final boolean online) {
    final LatkeBeanManager beanManager = LatkeBeanManagerImpl.getInstance();
    final UserRepository userRepository = beanManager.getReference(UserRepository.class);
    final Transaction transaction = userRepository.beginTransaction();
    try {
        final JSONObject user = userRepository.get(userId);
        user.put(UserExt.USER_ONLINE_FLAG, online);
        user.put(UserExt.USER_LATEST_LOGIN_TIME, System.currentTimeMillis());
        userRepository.update(userId, user);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Update user error", e);
    } finally {
        JdbcRepository.dispose();
    }
}
Also used : UserRepository(org.b3log.symphony.repository.UserRepository) Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Aggregations

UserRepository (org.b3log.symphony.repository.UserRepository)2 JSONObject (org.json.JSONObject)2 Locale (java.util.Locale)1 Cookie (javax.servlet.http.Cookie)1 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)1 Transaction (org.b3log.latke.repository.Transaction)1 ServiceException (org.b3log.latke.service.ServiceException)1 OptionRepository (org.b3log.symphony.repository.OptionRepository)1 UserQueryService (org.b3log.symphony.service.UserQueryService)1