Search in sources :

Example 6 with SysUserToken

use of com.publiccms.entities.sys.SysUserToken in project PublicCMS-preview by sanluan.

the class LoginController method login.

/**
 * @param username
 * @param password
 * @param returnUrl
 * @param request
 * @param response
 * @param model
 * @return view name
 */
@RequestMapping(value = "doLogin", method = RequestMethod.POST)
public String login(String username, String password, String returnUrl, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
    SysSite site = getSite(request);
    if (CommonUtils.empty(returnUrl)) {
        returnUrl = site.getDynamicPath();
    }
    Map<String, String> config = configComponent.getConfigData(site.getId(), Config.CONFIG_CODE_SITE);
    String loginPath = config.get(LoginConfigComponent.CONFIG_LOGIN_PATH);
    if (CommonUtils.empty(loginPath)) {
        loginPath = site.getDynamicPath();
    }
    username = StringUtils.trim(username);
    password = StringUtils.trim(password);
    if (ControllerUtils.verifyNotEmpty("username", username, model) || ControllerUtils.verifyNotEmpty("password", password, model)) {
        return REDIRECT + loginPath;
    } else {
        SysUser user;
        if (verifyNotEMail(username)) {
            user = service.findByName(site.getId(), username);
        } else {
            user = service.findByEmail(site.getId(), username);
        }
        String ip = RequestUtils.getIpAddress(request);
        if (ControllerUtils.verifyNotExist("username", user, model) || ControllerUtils.verifyNotEquals("password", VerificationUtils.md5Encode(password), user.getPassword(), model) || verifyNotEnablie(user, model)) {
            Long userId = null;
            if (null != user) {
                userId = user.getId();
            }
            logLoginService.save(new LogLogin(site.getId(), username, userId, ip, LogLoginService.CHANNEL_WEB, false, CommonUtils.getDate(), password));
            return REDIRECT + loginPath;
        } else {
            user.setPassword(null);
            setUserToSession(request.getSession(), user);
            String authToken = UUID.randomUUID().toString();
            addLoginStatus(user, authToken, request, response);
            sysUserTokenService.save(new SysUserToken(authToken, site.getId(), user.getId(), LogLoginService.CHANNEL_WEB, CommonUtils.getDate(), ip));
            service.updateLoginStatus(user.getId(), ip);
            logLoginService.save(new LogLogin(site.getId(), username, user.getId(), ip, LogLoginService.CHANNEL_WEB, true, CommonUtils.getDate(), null));
            return REDIRECT + returnUrl;
        }
    }
}
Also used : SysUserToken(com.publiccms.entities.sys.SysUserToken) SysUser(com.publiccms.entities.sys.SysUser) LogLogin(com.publiccms.entities.log.LogLogin) SysSite(com.publiccms.entities.sys.SysSite) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with SysUserToken

use of com.publiccms.entities.sys.SysUserToken in project PublicCMS-preview by sanluan.

the class OauthController method callback.

/**
 * @param channel
 * @param state
 * @param code
 * @param request
 * @param session
 * @param response
 * @param model
 * @return view name
 */
@RequestMapping(value = "callback/{channel}")
public String callback(@PathVariable("channel") String channel, String state, String code, HttpServletRequest request, HttpSession session, HttpServletResponse response, ModelMap model) {
    Oauth oauthComponent = oauthChannelMap.get(channel);
    SysSite site = getSite(request);
    Cookie stateCookie = RequestUtils.getCookie(request.getCookies(), STATE_COOKIE_NAME);
    if (null != oauthComponent && oauthComponent.enabled(site.getId()) && null != stateCookie && null != state && state.equals(stateCookie.getValue())) {
        try {
            OauthAccess oauthAccess = oauthComponent.getOpenId(site.getId(), code);
            if (null != oauthAccess && null != oauthAccess.getOpenId()) {
                Cookie cookie = RequestUtils.getCookie(request.getCookies(), RETURN_URL);
                String returnUrl = site.getDynamicPath();
                if (null != cookie && null != cookie.getValue()) {
                    returnUrl = cookie.getValue();
                }
                SysUserToken entity = sysUserTokenService.getEntity(oauthAccess.getOpenId());
                if (null != entity) {
                    if (entity.getChannel().equals(channel)) {
                        setUserToSession(session, sysUserService.getEntity(entity.getUserId()));
                        return REDIRECT + returnUrl;
                    }
                } else {
                    SysUser user = getUserFromSession(session);
                    if (null == user) {
                        OauthUser oauthUser = oauthComponent.getUserInfo(site.getId(), oauthAccess);
                        Map<String, String> config = configComponent.getConfigData(site.getId(), AbstractOauth.CONFIG_CODE);
                        if (null != oauthUser && CommonUtils.notEmpty(config) && CommonUtils.notEmpty(config.get(LoginConfigComponent.CONFIG_REGISTER_URL))) {
                            model.addAttribute("nickname", oauthUser.getNickname());
                            model.addAttribute("openId", oauthUser.getOpenId());
                            model.addAttribute("avatar", oauthUser.getAvatar());
                            model.addAttribute("gender", oauthUser.getGender());
                            model.addAttribute("channel", channel);
                            model.addAttribute("returnUrl", returnUrl);
                            return REDIRECT + config.get(LoginConfigComponent.CONFIG_REGISTER_URL);
                        }
                    } else {
                        String authToken = new StringBuilder(channel).append(DOT).append(site.getId()).append(DOT).append(oauthAccess.getOpenId()).toString();
                        entity = new SysUserToken(authToken, site.getId(), user.getId(), channel, CommonUtils.getDate(), RequestUtils.getIpAddress(request));
                        sysUserTokenService.save(entity);
                        setUserToSession(session, user);
                        return REDIRECT + returnUrl;
                    }
                }
            }
        } catch (IOException e) {
            log.error(e);
        }
    }
    return REDIRECT + site.getDynamicPath();
}
Also used : AbstractOauth(com.publiccms.common.base.oauth.AbstractOauth) Oauth(com.publiccms.common.api.oauth.Oauth) Cookie(javax.servlet.http.Cookie) OauthAccess(com.publiccms.view.pojo.oauth.OauthAccess) SysUserToken(com.publiccms.entities.sys.SysUserToken) SysUser(com.publiccms.entities.sys.SysUser) IOException(java.io.IOException) SysSite(com.publiccms.entities.sys.SysSite) OauthUser(com.publiccms.view.pojo.oauth.OauthUser) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with SysUserToken

use of com.publiccms.entities.sys.SysUserToken in project PublicCMS-preview by sanluan.

the class AutoLoginDirective method execute.

@Override
public void execute(RenderHandler handler, SysApp app, SysUser user) throws IOException, Exception {
    String uuid = handler.getString("uuid");
    boolean result = false;
    if (CommonUtils.notEmpty(uuid)) {
        SysSite site = getSite(handler);
        SysAppClientId sysAppClientId = new SysAppClientId(site.getId(), app.getChannel(), uuid);
        SysAppClient appClient = appClientService.getEntity(sysAppClientId);
        if (null != appClient && CommonUtils.notEmpty(appClient.getUserId())) {
            user = service.getEntity(appClient.getUserId());
            if (null != user && !user.isDisabled()) {
                String authToken = UUID.randomUUID().toString();
                String ip = RequestUtils.getIpAddress(handler.getRequest());
                sysUserTokenService.save(new SysUserToken(authToken, site.getId(), user.getId(), app.getChannel(), CommonUtils.getDate(), ip));
                service.updateLoginStatus(user.getId(), ip);
                logLoginService.save(new LogLogin(site.getId(), uuid, user.getId(), ip, app.getChannel(), true, CommonUtils.getDate(), null));
                user.setPassword(null);
                result = true;
                handler.put("authToken", authToken).put("user", user);
            }
        }
    }
    handler.put("result", result);
}
Also used : SysUserToken(com.publiccms.entities.sys.SysUserToken) LogLogin(com.publiccms.entities.log.LogLogin) SysAppClient(com.publiccms.entities.sys.SysAppClient) SysAppClientId(com.publiccms.entities.sys.SysAppClientId) SysSite(com.publiccms.entities.sys.SysSite)

Example 9 with SysUserToken

use of com.publiccms.entities.sys.SysUserToken in project PublicCMS-preview by sanluan.

the class WebContextInterceptor method initUser.

protected SysUser initUser(SysUser user, String channel, String cookiesName, SysSite site, HttpServletRequest request, HttpServletResponse response) {
    response.addHeader(CommonConstants.getXPowered(), CmsVersion.getVersion());
    String contextPath = request.getContextPath();
    if (null == user) {
        Cookie userCookie = RequestUtils.getCookie(request.getCookies(), cookiesName);
        if (null != userCookie && StringUtils.isNotBlank(userCookie.getValue())) {
            String value = userCookie.getValue();
            if (null != value) {
                String[] userData = value.split(CommonConstants.getCookiesUserSplit());
                if (userData.length > 1) {
                    try {
                        Long userId = Long.parseLong(userData[0]);
                        SysUserToken userToken = sysUserTokenService.getEntity(userData[1]);
                        if (null != userToken && null != site && !site.isDisabled() && site.getId() == userToken.getSiteId() && userId == userToken.getUserId() && channel.equals(userToken.getChannel()) && null != (user = sysUserService.getEntity(userId)) && !user.isDisabled()) {
                            user.setPassword(null);
                            String ip = RequestUtils.getIpAddress(request);
                            sysUserService.updateLoginStatus(user.getId(), ip);
                            logLoginService.save(new LogLogin(site.getId(), user.getName(), user.getId(), ip, channel, true, CommonUtils.getDate(), null));
                        } else {
                            user = null;
                            if (null != userToken) {
                                sysUserTokenService.delete(userToken.getAuthToken());
                            }
                            RequestUtils.cancleCookie(contextPath, response, cookiesName, null);
                        }
                    } catch (NumberFormatException e) {
                        RequestUtils.cancleCookie(contextPath, response, cookiesName, null);
                    }
                } else {
                    RequestUtils.cancleCookie(contextPath, response, cookiesName, null);
                }
            }
        }
    }
    return user;
}
Also used : Cookie(javax.servlet.http.Cookie) SysUserToken(com.publiccms.entities.sys.SysUserToken) LogLogin(com.publiccms.entities.log.LogLogin)

Example 10 with SysUserToken

use of com.publiccms.entities.sys.SysUserToken in project PublicCMS-preview by sanluan.

the class AbstractAppDirective method getUser.

protected SysUser getUser(RenderHandler handler) throws Exception {
    String authToken = handler.getString("authToken");
    Long authUserId = handler.getLong("authUserId");
    if (CommonUtils.notEmpty(authToken) && null != authUserId) {
        SysUserToken sysUserToken = sysUserTokenService.getEntity(authToken);
        if (null != sysUserToken && sysUserToken.getUserId() == authUserId) {
            return sysUserService.getEntity(sysUserToken.getUserId());
        }
    }
    return null;
}
Also used : SysUserToken(com.publiccms.entities.sys.SysUserToken)

Aggregations

SysUserToken (com.publiccms.entities.sys.SysUserToken)10 SysSite (com.publiccms.entities.sys.SysSite)7 LogLogin (com.publiccms.entities.log.LogLogin)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 SysUser (com.publiccms.entities.sys.SysUser)3 Cookie (javax.servlet.http.Cookie)2 Oauth (com.publiccms.common.api.oauth.Oauth)1 AbstractOauth (com.publiccms.common.base.oauth.AbstractOauth)1 LogOperate (com.publiccms.entities.log.LogOperate)1 SysAppClient (com.publiccms.entities.sys.SysAppClient)1 SysAppClientId (com.publiccms.entities.sys.SysAppClientId)1 OauthAccess (com.publiccms.view.pojo.oauth.OauthAccess)1 OauthUser (com.publiccms.view.pojo.oauth.OauthUser)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1