Search in sources :

Example 6 with Users

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();
}
Also used : Users(com.tale.model.Users) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 7 with Users

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);
    }
}
Also used : Users(com.tale.model.Users) TipException(com.tale.exception.TipException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 8 with Users

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;
}
Also used : Take(com.blade.jdbc.core.Take) Users(com.tale.model.Users) TipException(com.tale.exception.TipException)

Example 9 with 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);
    }
}
Also used : Attach(com.tale.model.Attach) RestResponse(com.blade.mvc.view.RestResponse) ArrayList(java.util.ArrayList) Users(com.tale.model.Users) IOException(java.io.IOException) TipException(com.tale.exception.TipException) IOException(java.io.IOException) TipException(com.tale.exception.TipException) FileItem(com.blade.mvc.multipart.FileItem) File(java.io.File) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 10 with Users

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";
}
Also used : Take(com.blade.jdbc.core.Take) Comments(com.tale.model.Comments) Users(com.tale.model.Users) Route(com.blade.mvc.annotation.Route)

Aggregations

Users (com.tale.model.Users)14 TipException (com.tale.exception.TipException)10 Route (com.blade.mvc.annotation.Route)7 JSON (com.blade.mvc.annotation.JSON)6 Contents (com.tale.model.Contents)4 Take (com.blade.jdbc.core.Take)2 Comments (com.tale.model.Comments)2 Config (com.blade.kit.base.Config)1 Session (com.blade.mvc.http.wrapper.Session)1 FileItem (com.blade.mvc.multipart.FileItem)1 RestResponse (com.blade.mvc.view.RestResponse)1 Attach (com.tale.model.Attach)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1