use of com.publiccms.entities.sys.SysUserToken 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();
}
use of com.publiccms.entities.sys.SysUserToken in project PublicCMS-preview by sanluan.
the class AbstractTemplateDirective method getUser.
/**
* @param handler
* @return user
* @throws Exception
*/
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;
}
use of com.publiccms.entities.sys.SysUserToken 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);
}
use of com.publiccms.entities.sys.SysUserToken in project PublicCMS-preview by sanluan.
the class SysUserTokenAdminController method delete.
/**
* @param authToken
* @param request
* @param session
* @param model
* @return view name
*/
@RequestMapping("delete")
public String delete(String authToken, HttpServletRequest request, HttpSession session, ModelMap model) {
SysSite site = getSite(request);
SysUserToken entity = service.getEntity(authToken);
Long userId = getAdminFromSession(session).getId();
if (null != entity) {
if (ControllerUtils.verifyNotEquals("siteId", userId, entity.getUserId(), model)) {
return TEMPLATE_ERROR;
}
service.delete(authToken);
logOperateService.save(new LogOperate(site.getId(), userId, LogLoginService.CHANNEL_WEB_MANAGER, "delete.usertoken", RequestUtils.getIpAddress(request), CommonUtils.getDate(), JsonUtils.getString(entity)));
}
return TEMPLATE_DONE;
}
use of com.publiccms.entities.sys.SysUserToken in project PublicCMS-preview by sanluan.
the class LoginController method register.
/**
* @param entity
* @param repassword
* @param returnUrl
* @param channel
* @param openId
* @param request
* @param response
* @param model
* @return view name
*/
@RequestMapping(value = "doRegister", method = RequestMethod.POST)
public String register(SysUser entity, String repassword, String returnUrl, String channel, String openId, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
SysSite site = getSite(request);
if (CommonUtils.empty(returnUrl)) {
returnUrl = site.getDynamicPath();
}
entity.setName(StringUtils.trim(entity.getName()));
entity.setNickName(StringUtils.trim(entity.getNickName()));
entity.setPassword(StringUtils.trim(entity.getPassword()));
repassword = StringUtils.trim(repassword);
if (ControllerUtils.verifyNotEmpty("username", entity.getName(), model) || ControllerUtils.verifyNotEmpty("nickname", entity.getNickName(), model) || ControllerUtils.verifyNotEmpty("password", entity.getPassword(), model) || verifyNotUserName("username", entity.getName(), model) || verifyNotNickName("nickname", entity.getNickName(), model) || ControllerUtils.verifyNotEquals("repassword", entity.getPassword(), repassword, model) || ControllerUtils.verifyHasExist("username", service.findByName(site.getId(), entity.getName()), model) || ControllerUtils.verifyHasExist("nickname", service.findByNickName(site.getId(), entity.getNickName()), model)) {
model.addAttribute("name", entity.getName());
model.addAttribute("nickname", entity.getNickName());
return REDIRECT + returnUrl;
} else {
String ip = RequestUtils.getIpAddress(request);
entity.setPassword(VerificationUtils.md5Encode(entity.getPassword()));
entity.setLastLoginIp(ip);
entity.setSiteId(site.getId());
service.save(entity);
entity.setPassword(null);
setUserToSession(request.getSession(), entity);
String authToken = UUID.randomUUID().toString();
addLoginStatus(entity, authToken, request, response);
sysUserTokenService.save(new SysUserToken(authToken, site.getId(), entity.getId(), LogLoginService.CHANNEL_WEB, CommonUtils.getDate(), ip));
if (null != channel && null != openId) {
String oauthToken = new StringBuilder(channel).append(DOT).append(site.getId()).append(DOT).append(openId).toString();
sysUserTokenService.save(new SysUserToken(oauthToken, site.getId(), entity.getId(), channel, CommonUtils.getDate(), ip));
}
return REDIRECT + returnUrl;
}
}
Aggregations