Search in sources :

Example 1 with ValidatorException

use of com.blade.exception.ValidatorException in project tale by otale.

the class SiteService method initSite.

/**
 * 初始化站点
 *
 * @param users 用户
 */
public void initSite(Users users) {
    String pwd = EncryptKit.md5(users.getUsername() + users.getPassword());
    users.setPassword(pwd);
    users.setScreenName(users.getUsername());
    users.setCreated(DateKit.nowUnix());
    Integer uid = users.save().asInt();
    try {
        String cp = SiteService.class.getClassLoader().getResource("").getPath();
        File lock = new File(cp + "install.lock");
        lock.createNewFile();
        TaleConst.INSTALLED = Boolean.TRUE;
        new Logs("初始化站点", null, "", uid).save();
    } catch (Exception e) {
        throw new ValidatorException("初始化站点失败");
    }
}
Also used : ValidatorException(com.blade.exception.ValidatorException) File(java.io.File) ValidatorException(com.blade.exception.ValidatorException)

Example 2 with ValidatorException

use of com.blade.exception.ValidatorException in project tale by otale.

the class CommonValidator method valid.

public static void valid(Contents param) {
    Validators.notEmpty().test(param.getTitle()).throwIfInvalid("文章标题");
    Validators.lessThan(MAX_TITLE_COUNT).test(param.getTitle()).throwIfInvalid("文章标题");
    Validators.notEmpty().test(param.getContent()).throwIfInvalid("文章内容");
    Validators.lessThan(MAX_TEXT_COUNT).test(param.getContent()).throwIfInvalid("文章内容");
    if (StringKit.isNotEmpty(param.getSlug())) {
        if (param.getSlug().length() < 2) {
            throw new ValidatorException("路径太短了");
        }
        if (!TaleUtils.isPath(param.getSlug())) {
            throw new ValidatorException("您输入的路径不合法");
        }
        Contents temp = select().from(Contents.class).where(Contents::getType, param.getType()).and(Contents::getSlug, param.getSlug()).one();
        if (null != temp && !temp.getCid().equals(param.getCid())) {
            throw new ValidatorException("该路径已经存在,请重新输入");
        }
    } else {
        param.setSlug(null);
    }
}
Also used : ValidatorException(com.blade.exception.ValidatorException) Contents(com.tale.model.entity.Contents)

Example 3 with ValidatorException

use of com.blade.exception.ValidatorException in project tale by otale.

the class CommentsService method saveComment.

/**
 * 保存评论
 *
 * @param comments
 */
public void saveComment(Comments comments) {
    comments.setAuthor(TaleUtils.cleanXSS(comments.getAuthor()));
    comments.setContent(TaleUtils.cleanXSS(comments.getContent()));
    comments.setAuthor(EmojiParser.parseToAliases(comments.getAuthor()));
    comments.setContent(EmojiParser.parseToAliases(comments.getContent()));
    Contents contents = select().from(Contents.class).byId(comments.getCid());
    if (null == contents) {
        throw new ValidatorException("不存在的文章");
    }
    try {
        comments.setOwnerId(contents.getAuthorId());
        comments.setAuthorId(null == comments.getAuthorId() ? 0 : comments.getAuthorId());
        comments.setCreated(DateKit.nowUnix());
        comments.setParent(null == comments.getCoid() ? 0 : comments.getCoid());
        comments.setCoid(null);
        comments.save();
        new Contents().set(Contents::getCommentsNum, contents.getCommentsNum() + 1).updateById(contents.getCid());
    } catch (Exception e) {
        throw e;
    }
}
Also used : Contents(com.tale.model.entity.Contents) ValidatorException(com.blade.exception.ValidatorException) ValidatorException(com.blade.exception.ValidatorException)

Example 4 with ValidatorException

use of com.blade.exception.ValidatorException in project tale by otale.

the class MetasService method saveMeta.

/**
 * 保存项目
 *
 * @param type
 * @param name
 * @param mid
 */
public void saveMeta(String type, String name, Integer mid) {
    if (StringKit.isEmpty(type) || StringKit.isEmpty(name)) {
        return;
    }
    Metas metas = select().from(Metas.class).where(Metas::getType, type).and(Metas::getName, name).one();
    if (null != metas) {
        throw new ValidatorException("已经存在该项");
    } else {
        metas = new Metas();
        metas.setName(name);
        if (null != mid) {
            metas.updateById(mid);
        } else {
            metas.setType(type);
            metas.save();
        }
    }
}
Also used : Metas(com.tale.model.entity.Metas) ValidatorException(com.blade.exception.ValidatorException)

Example 5 with ValidatorException

use of com.blade.exception.ValidatorException in project tale by otale.

the class AuthController method doLogin.

@SysLog("登录后台")
@PostRoute("login")
public RestResponse<?> doLogin(LoginParam loginParam, RouteContext context) {
    CommonValidator.valid(loginParam);
    Integer errorCount = cache.get(LOGIN_ERROR_COUNT);
    try {
        errorCount = null == errorCount ? 0 : errorCount;
        if (errorCount > 3) {
            return RestResponse.fail("您输入密码已经错误超过3次,请10分钟后尝试");
        }
        long count = new Users().where("username", loginParam.getUsername()).count();
        if (count < 1) {
            errorCount += 1;
            return RestResponse.fail("不存在该用户");
        }
        String pwd = EncryptKit.md5(loginParam.getUsername(), loginParam.getPassword());
        Users user = select().from(Users.class).where(Users::getUsername, loginParam.getUsername()).and(Users::getPassword, pwd).one();
        if (null == user) {
            errorCount += 1;
            return RestResponse.fail("用户名或密码错误");
        }
        context.session().attribute(TaleConst.LOGIN_SESSION_KEY, user);
        if (StringKit.isNotBlank(loginParam.getRememberMe())) {
            TaleUtils.setCookie(context, user.getUid());
        }
        Users temp = new Users();
        temp.setLogged(DateKit.nowUnix());
        temp.updateById(user.getUid());
        log.info("登录成功:{}", loginParam.getUsername());
        cache.set(LOGIN_ERROR_COUNT, 0);
        return RestResponse.ok();
    } catch (Exception e) {
        errorCount += 1;
        cache.set(LOGIN_ERROR_COUNT, errorCount, 10 * 60);
        String msg = "登录失败";
        if (e instanceof ValidatorException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : ValidatorException(com.blade.exception.ValidatorException) Users(com.tale.model.entity.Users) ValidatorException(com.blade.exception.ValidatorException) PostRoute(com.blade.mvc.annotation.PostRoute) SysLog(com.tale.annotation.SysLog)

Aggregations

ValidatorException (com.blade.exception.ValidatorException)5 Contents (com.tale.model.entity.Contents)2 PostRoute (com.blade.mvc.annotation.PostRoute)1 SysLog (com.tale.annotation.SysLog)1 Metas (com.tale.model.entity.Metas)1 Users (com.tale.model.entity.Users)1 File (java.io.File)1