Search in sources :

Example 1 with LogLogin

use of com.publiccms.entities.log.LogLogin in project PublicCMS-preview by sanluan.

the class LoginAdminController method login.

/**
 * @param username
 * @param password
 * @param returnUrl
 * @param request
 * @param session
 * @param response
 * @param model
 * @return view name
 */
@RequestMapping(value = "login", method = RequestMethod.POST)
public String login(String username, String password, String returnUrl, HttpServletRequest request, HttpSession session, HttpServletResponse response, ModelMap model) {
    SysSite site = getSite(request);
    username = StringUtils.trim(username);
    password = StringUtils.trim(password);
    if (ControllerUtils.verifyNotEmpty("username", username, model) || ControllerUtils.verifyNotEmpty("password", password, model)) {
        model.addAttribute("username", username);
        model.addAttribute("returnUrl", returnUrl);
        return "login";
    }
    String ip = RequestUtils.getIpAddress(request);
    SysUser user = service.findByName(site.getId(), username);
    if (ControllerUtils.verifyNotExist("username", user, model) || ControllerUtils.verifyNotEquals("password", VerificationUtils.md5Encode(password), user.getPassword(), model) || verifyNotAdmin(user, model) || verifyNotEnablie(user, model)) {
        model.addAttribute("username", username);
        model.addAttribute("returnUrl", returnUrl);
        Long userId = null;
        if (null != user) {
            userId = user.getId();
        }
        logLoginService.save(new LogLogin(site.getId(), username, userId, ip, LogLoginService.CHANNEL_WEB_MANAGER, false, CommonUtils.getDate(), password));
        return "login";
    }
    setAdminToSession(session, user);
    service.updateLoginStatus(user.getId(), ip);
    String authToken = UUID.randomUUID().toString();
    sysUserTokenService.save(new SysUserToken(authToken, site.getId(), user.getId(), LogLoginService.CHANNEL_WEB_MANAGER, CommonUtils.getDate(), ip));
    try {
        StringBuilder sb = new StringBuilder();
        sb.append(user.getId()).append(CommonConstants.getCookiesUserSplit()).append(authToken).append(CommonConstants.getCookiesUserSplit()).append(user.isSuperuserAccess()).append(CommonConstants.getCookiesUserSplit()).append(URLEncoder.encode(user.getNickName(), DEFAULT_CHARSET_NAME));
        RequestUtils.addCookie(request.getContextPath(), response, CommonConstants.getCookiesAdmin(), sb.toString(), Integer.MAX_VALUE, null);
    } catch (UnsupportedEncodingException e) {
        log.error(e.getMessage(), e);
    }
    logLoginService.save(new LogLogin(site.getId(), username, user.getId(), ip, LogLoginService.CHANNEL_WEB_MANAGER, true, CommonUtils.getDate(), null));
    if (CommonUtils.notEmpty(returnUrl)) {
        return REDIRECT + returnUrl;
    }
    return REDIRECT + CommonConstants.getDefaultPage();
}
Also used : SysUserToken(com.publiccms.entities.sys.SysUserToken) SysUser(com.publiccms.entities.sys.SysUser) LogLogin(com.publiccms.entities.log.LogLogin) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SysSite(com.publiccms.entities.sys.SysSite) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with LogLogin

use of com.publiccms.entities.log.LogLogin in project PublicCMS-preview by sanluan.

the class LoginDirective method execute.

@Override
public void execute(RenderHandler handler, SysApp app, SysUser user) throws IOException, Exception {
    String username = StringUtils.trim(handler.getString("username"));
    String password = StringUtils.trim(handler.getString("password"));
    boolean result = false;
    if (CommonUtils.notEmpty(username) && CommonUtils.notEmpty(password)) {
        SysSite site = getSite(handler);
        if (AbstractController.verifyNotEMail(username)) {
            user = service.findByName(site.getId(), username);
        } else {
            user = service.findByEmail(site.getId(), username);
        }
        String ip = RequestUtils.getIpAddress(handler.getRequest());
        if (null != user && !user.isDisabled() && user.getPassword().equals(VerificationUtils.md5Encode(password))) {
            String authToken = UUID.randomUUID().toString();
            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(), username, user.getId(), ip, app.getChannel(), true, CommonUtils.getDate(), null));
            user.setPassword(null);
            result = true;
            handler.put("authToken", authToken).put("user", user);
        } else {
            LogLogin log = new LogLogin();
            log.setSiteId(site.getId());
            log.setName(username);
            log.setErrorPassword(password);
            log.setIp(ip);
            log.setChannel(app.getChannel());
            logLoginService.save(log);
        }
    }
    handler.put("result", result);
}
Also used : SysUserToken(com.publiccms.entities.sys.SysUserToken) LogLogin(com.publiccms.entities.log.LogLogin) SysSite(com.publiccms.entities.sys.SysSite)

Example 3 with LogLogin

use of com.publiccms.entities.log.LogLogin 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 4 with LogLogin

use of com.publiccms.entities.log.LogLogin 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 5 with LogLogin

use of com.publiccms.entities.log.LogLogin 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)

Aggregations

LogLogin (com.publiccms.entities.log.LogLogin)5 SysUserToken (com.publiccms.entities.sys.SysUserToken)5 SysSite (com.publiccms.entities.sys.SysSite)4 SysUser (com.publiccms.entities.sys.SysUser)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 SysAppClient (com.publiccms.entities.sys.SysAppClient)1 SysAppClientId (com.publiccms.entities.sys.SysAppClientId)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Cookie (javax.servlet.http.Cookie)1