Search in sources :

Example 1 with CommentBlackList

use of run.halo.app.model.entity.CommentBlackList in project halo by ruibaby.

the class BackupServiceImpl method importData.

@Override
public void importData(MultipartFile file) throws IOException {
    String jsonContent = FileUtils.readString(file.getInputStream());
    ObjectMapper mapper = JsonUtils.createDefaultJsonMapper();
    TypeReference<HashMap<String, Object>> typeRef = new TypeReference<>() {
    };
    HashMap<String, Object> data = mapper.readValue(jsonContent, typeRef);
    String version = (String) Objects.requireNonNullElse(data.get("version"), "");
    if (!VersionUtil.hasSameMajorAndMinorVersion(HaloConst.HALO_VERSION, version)) {
        throw new BadRequestException("导入数据的主次版本号与当前系统版本号不匹配,不支持导入!");
    }
    List<Attachment> attachments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("attachments")), Attachment[].class));
    attachmentService.createInBatch(attachments);
    List<Category> categories = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("categories")), Category[].class));
    categoryService.createInBatch(categories);
    List<Tag> tags = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("tags")), Tag[].class));
    tagService.createInBatch(tags);
    List<CommentBlackList> commentBlackList = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("comment_black_list")), CommentBlackList[].class));
    commentBlackListService.createInBatch(commentBlackList);
    List<Journal> journals = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("journals")), Journal[].class));
    journalService.createInBatch(journals);
    List<JournalComment> journalComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("journal_comments")), JournalComment[].class));
    journalCommentService.createInBatch(journalComments);
    List<Link> links = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("links")), Link[].class));
    linkService.createInBatch(links);
    List<Log> logs = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("logs")), Log[].class));
    logService.createInBatch(logs);
    List<Menu> menus = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("menus")), Menu[].class));
    menuService.createInBatch(menus);
    List<Option> options = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("options")), Option[].class));
    optionService.createInBatch(options);
    eventPublisher.publishEvent(new OptionUpdatedEvent(this));
    List<Photo> photos = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("photos")), Photo[].class));
    photoService.createInBatch(photos);
    List<Post> posts = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("posts")), Post[].class));
    postService.createInBatch(posts);
    List<Content> contents = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("contents")), Content[].class));
    contentService.createInBatch(contents);
    List<ContentPatchLog> contentPatchLogs = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("content_patch_logs")), ContentPatchLog[].class));
    contentPatchLogService.createInBatch(contentPatchLogs);
    List<PostCategory> postCategories = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_categories")), PostCategory[].class));
    postCategoryService.createInBatch(postCategories);
    List<PostComment> postComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_comments")), PostComment[].class));
    postCommentService.createInBatch(postComments);
    List<PostMeta> postMetas = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_metas")), PostMeta[].class));
    postMetaService.createInBatch(postMetas);
    List<PostTag> postTags = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_tags")), PostTag[].class));
    postTagService.createInBatch(postTags);
    List<Sheet> sheets = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheets")), Sheet[].class));
    sheetService.createInBatch(sheets);
    List<SheetComment> sheetComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheet_comments")), SheetComment[].class));
    sheetCommentService.createInBatch(sheetComments);
    List<SheetMeta> sheetMetas = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheet_metas")), SheetMeta[].class));
    sheetMetaService.createInBatch(sheetMetas);
    List<ThemeSetting> themeSettings = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("theme_settings")), ThemeSetting[].class));
    themeSettingService.createInBatch(themeSettings);
    eventPublisher.publishEvent(new ThemeUpdatedEvent(this));
    List<User> users = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("user")), User[].class));
    if (users.size() > 0) {
        userService.create(users.get(0));
    }
}
Also used : SheetMeta(run.halo.app.model.entity.SheetMeta) HashMap(java.util.HashMap) JournalComment(run.halo.app.model.entity.JournalComment) Attachment(run.halo.app.model.entity.Attachment) Journal(run.halo.app.model.entity.Journal) Photo(run.halo.app.model.entity.Photo) ContentPatchLog(run.halo.app.model.entity.ContentPatchLog) PostComment(run.halo.app.model.entity.PostComment) PostMeta(run.halo.app.model.entity.PostMeta) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContentPatchLog(run.halo.app.model.entity.ContentPatchLog) Log(run.halo.app.model.entity.Log) Content(run.halo.app.model.entity.Content) ThemeUpdatedEvent(run.halo.app.event.theme.ThemeUpdatedEvent) Option(run.halo.app.model.entity.Option) CommentBlackList(run.halo.app.model.entity.CommentBlackList) Link(run.halo.app.model.entity.Link) SheetComment(run.halo.app.model.entity.SheetComment) Category(run.halo.app.model.entity.Category) PostCategory(run.halo.app.model.entity.PostCategory) User(run.halo.app.model.entity.User) OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) PostCategory(run.halo.app.model.entity.PostCategory) ThemeSetting(run.halo.app.model.entity.ThemeSetting) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Menu(run.halo.app.model.entity.Menu) PostTag(run.halo.app.model.entity.PostTag) Post(run.halo.app.model.entity.Post) BadRequestException(run.halo.app.exception.BadRequestException) PostTag(run.halo.app.model.entity.PostTag) Tag(run.halo.app.model.entity.Tag) Sheet(run.halo.app.model.entity.Sheet)

Example 2 with CommentBlackList

use of run.halo.app.model.entity.CommentBlackList in project halo by halo-dev.

the class CommentBlackListServiceImpl method commentsBanStatus.

@Override
public CommentViolationTypeEnum commentsBanStatus(String ipAddress) {
    /*
        N=后期可配置
        1. 获取评论次数;
        2. 判断N分钟内,是否超过规定的次数限制,超过后需要每隔N分钟才能再次评论;
        3. 如果在时隔N分钟内,还有多次评论,可被认定为恶意攻击者;
        4. 对恶意攻击者进行N分钟的封禁;
        */
    Optional<CommentBlackList> blackList = commentBlackListRepository.findByIpAddress(ipAddress);
    LocalDateTime now = LocalDateTime.now();
    Date endTime = new Date(DateTimeUtils.toEpochMilli(now));
    Integer banTime = optionService.getByPropertyOrDefault(CommentProperties.COMMENT_BAN_TIME, Integer.class, 10);
    Date startTime = new Date(DateTimeUtils.toEpochMilli(now.minusMinutes(banTime)));
    Integer range = optionService.getByPropertyOrDefault(CommentProperties.COMMENT_RANGE, Integer.class, 30);
    boolean isPresent = postCommentRepository.countByIpAndTime(ipAddress, startTime, endTime) >= range;
    if (isPresent && blackList.isPresent()) {
        update(now, blackList.get(), banTime);
        return CommentViolationTypeEnum.FREQUENTLY;
    } else if (isPresent) {
        CommentBlackList commentBlackList = CommentBlackList.builder().banTime(getBanTime(now, banTime)).ipAddress(ipAddress).build();
        super.create(commentBlackList);
        return CommentViolationTypeEnum.FREQUENTLY;
    }
    return CommentViolationTypeEnum.NORMAL;
}
Also used : LocalDateTime(java.time.LocalDateTime) CommentBlackList(run.halo.app.model.entity.CommentBlackList) Date(java.util.Date)

Example 3 with CommentBlackList

use of run.halo.app.model.entity.CommentBlackList in project halo-plugin-experimental by guqing.

the class CommentBlackListServiceImpl method commentsBanStatus.

@Override
public CommentViolationTypeEnum commentsBanStatus(String ipAddress) {
    /*
        N=后期可配置
        1. 获取评论次数;
        2. 判断N分钟内,是否超过规定的次数限制,超过后需要每隔N分钟才能再次评论;
        3. 如果在时隔N分钟内,还有多次评论,可被认定为恶意攻击者;
        4. 对恶意攻击者进行N分钟的封禁;
        */
    Optional<CommentBlackList> blackList = commentBlackListRepository.findByIpAddress(ipAddress);
    LocalDateTime now = LocalDateTime.now();
    Date endTime = new Date(DateTimeUtils.toEpochMilli(now));
    Integer banTime = optionService.getByPropertyOrDefault(CommentProperties.COMMENT_BAN_TIME, Integer.class, 10);
    Date startTime = new Date(DateTimeUtils.toEpochMilli(now.minusMinutes(banTime)));
    Integer range = optionService.getByPropertyOrDefault(CommentProperties.COMMENT_RANGE, Integer.class, 30);
    boolean isPresent = postCommentRepository.countByIpAndTime(ipAddress, startTime, endTime) >= range;
    if (isPresent && blackList.isPresent()) {
        update(now, blackList.get(), banTime);
        return CommentViolationTypeEnum.FREQUENTLY;
    } else if (isPresent) {
        CommentBlackList commentBlackList = CommentBlackList.builder().banTime(getBanTime(now, banTime)).ipAddress(ipAddress).build();
        super.create(commentBlackList);
        return CommentViolationTypeEnum.FREQUENTLY;
    }
    return CommentViolationTypeEnum.NORMAL;
}
Also used : LocalDateTime(java.time.LocalDateTime) CommentBlackList(run.halo.app.model.entity.CommentBlackList) Date(java.util.Date)

Example 4 with CommentBlackList

use of run.halo.app.model.entity.CommentBlackList in project halo by ruibaby.

the class CommentBlackListServiceImpl method commentsBanStatus.

@Override
public CommentViolationTypeEnum commentsBanStatus(String ipAddress) {
    /*
        N=后期可配置
        1. 获取评论次数;
        2. 判断N分钟内,是否超过规定的次数限制,超过后需要每隔N分钟才能再次评论;
        3. 如果在时隔N分钟内,还有多次评论,可被认定为恶意攻击者;
        4. 对恶意攻击者进行N分钟的封禁;
        */
    Optional<CommentBlackList> blackList = commentBlackListRepository.findByIpAddress(ipAddress);
    LocalDateTime now = LocalDateTime.now();
    Date endTime = new Date(DateTimeUtils.toEpochMilli(now));
    Integer banTime = optionService.getByPropertyOrDefault(CommentProperties.COMMENT_BAN_TIME, Integer.class, 10);
    Date startTime = new Date(DateTimeUtils.toEpochMilli(now.minusMinutes(banTime)));
    Integer range = optionService.getByPropertyOrDefault(CommentProperties.COMMENT_RANGE, Integer.class, 30);
    boolean isPresent = postCommentRepository.countByIpAndTime(ipAddress, startTime, endTime) >= range;
    if (isPresent && blackList.isPresent()) {
        update(now, blackList.get(), banTime);
        return CommentViolationTypeEnum.FREQUENTLY;
    } else if (isPresent) {
        CommentBlackList commentBlackList = CommentBlackList.builder().banTime(getBanTime(now, banTime)).ipAddress(ipAddress).build();
        super.create(commentBlackList);
        return CommentViolationTypeEnum.FREQUENTLY;
    }
    return CommentViolationTypeEnum.NORMAL;
}
Also used : LocalDateTime(java.time.LocalDateTime) CommentBlackList(run.halo.app.model.entity.CommentBlackList) Date(java.util.Date)

Example 5 with CommentBlackList

use of run.halo.app.model.entity.CommentBlackList in project halo by halo-dev.

the class BackupServiceImpl method importData.

@Override
public void importData(MultipartFile file) throws IOException {
    String jsonContent = FileUtils.readString(file.getInputStream());
    ObjectMapper mapper = JsonUtils.createDefaultJsonMapper();
    TypeReference<HashMap<String, Object>> typeRef = new TypeReference<>() {
    };
    HashMap<String, Object> data = mapper.readValue(jsonContent, typeRef);
    String version = (String) Objects.requireNonNullElse(data.get("version"), "");
    if (!VersionUtil.hasSameMajorAndMinorVersion(HaloConst.HALO_VERSION, version)) {
        throw new BadRequestException("导入数据的主次版本号与当前系统版本号不匹配,不支持导入!");
    }
    List<Attachment> attachments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("attachments")), Attachment[].class));
    attachmentService.createInBatch(attachments);
    List<Category> categories = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("categories")), Category[].class));
    categoryService.createInBatch(categories);
    List<Tag> tags = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("tags")), Tag[].class));
    tagService.createInBatch(tags);
    List<CommentBlackList> commentBlackList = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("comment_black_list")), CommentBlackList[].class));
    commentBlackListService.createInBatch(commentBlackList);
    List<Journal> journals = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("journals")), Journal[].class));
    journalService.createInBatch(journals);
    List<JournalComment> journalComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("journal_comments")), JournalComment[].class));
    journalCommentService.createInBatch(journalComments);
    List<Link> links = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("links")), Link[].class));
    linkService.createInBatch(links);
    List<Log> logs = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("logs")), Log[].class));
    logService.createInBatch(logs);
    List<Menu> menus = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("menus")), Menu[].class));
    menuService.createInBatch(menus);
    List<Option> options = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("options")), Option[].class));
    optionService.createInBatch(options);
    eventPublisher.publishEvent(new OptionUpdatedEvent(this));
    List<Photo> photos = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("photos")), Photo[].class));
    photoService.createInBatch(photos);
    List<Post> posts = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("posts")), Post[].class));
    postService.createInBatch(posts);
    List<Content> contents = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("contents")), Content[].class));
    contentService.createInBatch(contents);
    List<ContentPatchLog> contentPatchLogs = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("content_patch_logs")), ContentPatchLog[].class));
    contentPatchLogService.createInBatch(contentPatchLogs);
    List<PostCategory> postCategories = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_categories")), PostCategory[].class));
    postCategoryService.createInBatch(postCategories);
    List<PostComment> postComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_comments")), PostComment[].class));
    postCommentService.createInBatch(postComments);
    List<PostMeta> postMetas = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_metas")), PostMeta[].class));
    postMetaService.createInBatch(postMetas);
    List<PostTag> postTags = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_tags")), PostTag[].class));
    postTagService.createInBatch(postTags);
    List<Sheet> sheets = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheets")), Sheet[].class));
    sheetService.createInBatch(sheets);
    List<SheetComment> sheetComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheet_comments")), SheetComment[].class));
    sheetCommentService.createInBatch(sheetComments);
    List<SheetMeta> sheetMetas = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheet_metas")), SheetMeta[].class));
    sheetMetaService.createInBatch(sheetMetas);
    List<ThemeSetting> themeSettings = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("theme_settings")), ThemeSetting[].class));
    themeSettingService.createInBatch(themeSettings);
    eventPublisher.publishEvent(new ThemeUpdatedEvent(this));
    List<User> users = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("user")), User[].class));
    if (users.size() > 0) {
        userService.create(users.get(0));
    }
}
Also used : SheetMeta(run.halo.app.model.entity.SheetMeta) HashMap(java.util.HashMap) JournalComment(run.halo.app.model.entity.JournalComment) Attachment(run.halo.app.model.entity.Attachment) Journal(run.halo.app.model.entity.Journal) Photo(run.halo.app.model.entity.Photo) ContentPatchLog(run.halo.app.model.entity.ContentPatchLog) PostComment(run.halo.app.model.entity.PostComment) PostMeta(run.halo.app.model.entity.PostMeta) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContentPatchLog(run.halo.app.model.entity.ContentPatchLog) Log(run.halo.app.model.entity.Log) Content(run.halo.app.model.entity.Content) ThemeUpdatedEvent(run.halo.app.event.theme.ThemeUpdatedEvent) Option(run.halo.app.model.entity.Option) CommentBlackList(run.halo.app.model.entity.CommentBlackList) Link(run.halo.app.model.entity.Link) SheetComment(run.halo.app.model.entity.SheetComment) Category(run.halo.app.model.entity.Category) PostCategory(run.halo.app.model.entity.PostCategory) User(run.halo.app.model.entity.User) OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) PostCategory(run.halo.app.model.entity.PostCategory) ThemeSetting(run.halo.app.model.entity.ThemeSetting) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Menu(run.halo.app.model.entity.Menu) PostTag(run.halo.app.model.entity.PostTag) Post(run.halo.app.model.entity.Post) BadRequestException(run.halo.app.exception.BadRequestException) PostTag(run.halo.app.model.entity.PostTag) Tag(run.halo.app.model.entity.Tag) Sheet(run.halo.app.model.entity.Sheet)

Aggregations

CommentBlackList (run.halo.app.model.entity.CommentBlackList)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 LocalDateTime (java.time.LocalDateTime)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 OptionUpdatedEvent (run.halo.app.event.options.OptionUpdatedEvent)3 ThemeUpdatedEvent (run.halo.app.event.theme.ThemeUpdatedEvent)3 Attachment (run.halo.app.model.entity.Attachment)3 Category (run.halo.app.model.entity.Category)3 Journal (run.halo.app.model.entity.Journal)3 JournalComment (run.halo.app.model.entity.JournalComment)3 Link (run.halo.app.model.entity.Link)3 Log (run.halo.app.model.entity.Log)3 Menu (run.halo.app.model.entity.Menu)3 Option (run.halo.app.model.entity.Option)3 Photo (run.halo.app.model.entity.Photo)3 Post (run.halo.app.model.entity.Post)3 PostCategory (run.halo.app.model.entity.PostCategory)3 PostComment (run.halo.app.model.entity.PostComment)3