use of com.tale.model.Users in project tale by otale.
the class AuthController method doLogin.
@Route(value = "login", method = HttpMethod.POST)
@JSON
public RestResponse doLogin(@QueryParam String username, @QueryParam String password, @QueryParam String remeber_me, Request request, Session session, Response response) {
Integer error_count = cache.get("login_error_count");
try {
error_count = null == error_count ? 0 : error_count;
if (null != error_count && error_count > 3) {
return RestResponse.fail("您输入密码已经错误超过3次,请10分钟后尝试");
}
Users user = usersService.login(username, password);
session.attribute(TaleConst.LOGIN_SESSION_KEY, user);
if (StringKit.isNotBlank(remeber_me)) {
TaleUtils.setCookie(response, user.getUid());
}
Users temp = new Users();
temp.setUid(user.getUid());
temp.setLogged(DateKit.getCurrentUnixTime());
usersService.update(temp);
LOGGER.info("登录成功:{}", JSONKit.toJSONString(request.querys()));
cache.set("login_error_count", 0);
logService.save(LogActions.LOGIN, JSONKit.toJSONString(request.querys()), request.address(), user.getUid());
} catch (Exception e) {
error_count += 1;
cache.set("login_error_count", error_count, 10 * 60);
String msg = "登录失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponse.fail(msg);
}
return RestResponse.ok();
}
use of com.tale.model.Users in project tale by otale.
the class IndexController method upPwd.
/**
* 修改密码
*/
@Route(value = "password", method = HttpMethod.POST)
@JSON
public RestResponse upPwd(@QueryParam String old_password, @QueryParam String password, Request request) {
Users users = this.user();
if (StringKit.isBlank(old_password) || StringKit.isBlank(password)) {
return RestResponse.fail("请确认信息输入完整");
}
if (!users.getPassword().equals(Tools.md5(users.getUsername() + old_password))) {
return RestResponse.fail("旧密码错误");
}
if (password.length() < 6 || password.length() > 14) {
return RestResponse.fail("请输入6-14位密码");
}
try {
Users temp = new Users();
temp.setUid(users.getUid());
String pwd = Tools.md5(users.getUsername() + password);
temp.setPassword(pwd);
usersService.update(temp);
logService.save(LogActions.UP_PWD, null, request.address(), this.getUid());
return RestResponse.ok();
} catch (Exception e) {
String msg = "密码修改失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponse.fail(msg);
}
}
use of com.tale.model.Users in project tale by otale.
the class UsersServiceImpl method login.
@Override
public Users login(String username, String password) {
if (StringKit.isBlank(username) || StringKit.isBlank(password)) {
throw new TipException("用户名和密码不能为空");
}
int count = activeRecord.count(new Take(Users.class).eq("username", username));
if (count < 1) {
throw new TipException("不存在该用户");
}
String pwd = Tools.md5(username, password);
Users users = activeRecord.one(new Take(Users.class).eq("username", username).eq("password", pwd));
if (null == users) {
throw new TipException("用户名或密码错误");
}
return users;
}
use of com.tale.model.Users in project tale by otale.
the class AttachController method upload.
/**
* 上传文件接口
*
* 返回格式
* @param request
* @return
*/
@Route(value = "upload", method = HttpMethod.POST)
@JSON
public RestResponse upload(Request request) {
LOGGER.info("UPLOAD DIR = {}", TaleUtils.upDir);
Users users = this.user();
Integer uid = users.getUid();
Map<String, FileItem> fileItemMap = request.fileItems();
Collection<FileItem> fileItems = fileItemMap.values();
List<Attach> errorFiles = new ArrayList<>();
List<Attach> urls = new ArrayList<>();
try {
fileItems.forEach((FileItem f) -> {
String fname = f.fileName();
if (f.file().length() / 1024 <= TaleConst.MAX_FILE_SIZE) {
String fkey = TaleUtils.getFileKey(fname);
String ftype = TaleUtils.isImage(f.file()) ? Types.IMAGE : Types.FILE;
String filePath = TaleUtils.upDir + fkey;
File file = new File(filePath);
try {
Tools.copyFileUsingFileChannels(f.file(), file);
f.file().delete();
} catch (IOException e) {
e.printStackTrace();
}
Attach attach = attachService.save(fname, fkey, ftype, uid);
urls.add(attach);
siteService.cleanCache(Types.C_STATISTICS);
} else {
errorFiles.add(new Attach(fname));
}
});
if (errorFiles.size() > 0) {
RestResponse restResponse = new RestResponse();
restResponse.setSuccess(false);
restResponse.setPayload(errorFiles);
return restResponse;
}
return RestResponse.ok(urls);
} catch (Exception e) {
String msg = "文件上传失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponse.fail(msg);
}
}
use of com.tale.model.Users in project tale by otale.
the class CommentController method index.
@Route(value = "", method = HttpMethod.GET)
public String index(@QueryParam(value = "page", defaultValue = "1") int page, @QueryParam(value = "limit", defaultValue = "15") int limit, Request request) {
Users users = this.user();
Paginator<Comments> commentsPaginator = commentsService.getComments(new Take(Comments.class).notEq("author_id", users.getUid()).page(page, limit, "coid desc"));
request.attribute("comments", commentsPaginator);
return "admin/comment_list";
}
Aggregations