Search in sources :

Example 1 with Authentication

use of run.halo.app.security.authentication.Authentication in project halo by halo-dev.

the class BaseCommentServiceImpl method create.

@Override
@NonNull
@Transactional(rollbackFor = Exception.class)
public COMMENT create(@NonNull COMMENT comment) {
    Assert.notNull(comment, "Domain must not be null");
    // Check post id
    if (!ServiceUtils.isEmptyId(comment.getPostId())) {
        validateTarget(comment.getPostId());
    }
    // Check parent id
    if (!ServiceUtils.isEmptyId(comment.getParentId())) {
        mustExistById(comment.getParentId());
    }
    // Check user login status and set this field
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    // Set some default values
    if (comment.getIpAddress() == null) {
        comment.setIpAddress(ServletUtils.getRequestIp());
    }
    if (comment.getUserAgent() == null) {
        comment.setUserAgent(ServletUtils.getHeaderIgnoreCase(HttpHeaders.USER_AGENT));
    }
    if (comment.getGravatarMd5() == null) {
        comment.setGravatarMd5(DigestUtils.md5Hex(Optional.ofNullable(comment.getEmail()).orElse("")));
    }
    if (StringUtils.isNotEmpty(comment.getAuthorUrl())) {
        comment.setAuthorUrl(HaloUtils.normalizeUrl(comment.getAuthorUrl()));
    }
    if (authentication != null) {
        // Comment of blogger
        comment.setIsAdmin(true);
        comment.setStatus(CommentStatus.PUBLISHED);
    } else {
        // Comment of guest
        // Handle comment status
        Boolean needAudit = optionService.getByPropertyOrDefault(CommentProperties.NEW_NEED_CHECK, Boolean.class, true);
        comment.setStatus(needAudit ? CommentStatus.AUDITING : CommentStatus.PUBLISHED);
    }
    // Create comment
    COMMENT createdComment = super.create(comment);
    if (ServiceUtils.isEmptyId(createdComment.getParentId())) {
        if (authentication == null) {
            // New comment of guest
            eventPublisher.publishEvent(new CommentNewEvent(this, createdComment.getId()));
        }
    } else {
        // Reply comment
        eventPublisher.publishEvent(new CommentReplyEvent(this, createdComment.getId()));
    }
    return createdComment;
}
Also used : CommentReplyEvent(run.halo.app.event.comment.CommentReplyEvent) Authentication(run.halo.app.security.authentication.Authentication) CommentNewEvent(run.halo.app.event.comment.CommentNewEvent) NonNull(org.springframework.lang.NonNull) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Authentication

use of run.halo.app.security.authentication.Authentication in project halo by halo-dev.

the class BaseCommentServiceImpl method createBy.

@Override
@NonNull
@Transactional(rollbackFor = Exception.class)
public COMMENT createBy(@NonNull BaseCommentParam<COMMENT> commentParam) {
    Assert.notNull(commentParam, "Comment param must not be null");
    // Check user login status and set this field
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        // Blogger comment
        User user = authentication.getDetail().getUser();
        commentParam.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname());
        commentParam.setEmail(user.getEmail());
        commentParam.setAuthorUrl(optionService.getByPropertyOrDefault(BlogProperties.BLOG_URL, String.class, null));
    }
    // Validate the comment param manually
    ValidationUtils.validate(commentParam);
    if (authentication == null) {
        // Check email
        if (userService.getByEmail(commentParam.getEmail()).isPresent()) {
            throw new BadRequestException("不能使用博主的邮箱,如果您是博主,请登录管理端进行回复。");
        }
    }
    // Convert to comment
    return create(commentParam.convertTo());
}
Also used : User(run.halo.app.model.entity.User) Authentication(run.halo.app.security.authentication.Authentication) BadRequestException(run.halo.app.exception.BadRequestException) NonNull(org.springframework.lang.NonNull) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with Authentication

use of run.halo.app.security.authentication.Authentication in project halo by ruibaby.

the class BaseCommentServiceImpl method create.

@Override
@NonNull
@Transactional(rollbackFor = Exception.class)
public COMMENT create(@NonNull COMMENT comment) {
    Assert.notNull(comment, "Domain must not be null");
    // Check post id
    if (!ServiceUtils.isEmptyId(comment.getPostId())) {
        validateTarget(comment.getPostId());
    }
    // Check parent id
    if (!ServiceUtils.isEmptyId(comment.getParentId())) {
        mustExistById(comment.getParentId());
    }
    // Check user login status and set this field
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    // Set some default values
    if (comment.getIpAddress() == null) {
        comment.setIpAddress(ServletUtils.getRequestIp());
    }
    if (comment.getUserAgent() == null) {
        comment.setUserAgent(ServletUtils.getHeaderIgnoreCase(HttpHeaders.USER_AGENT));
    }
    if (comment.getGravatarMd5() == null) {
        comment.setGravatarMd5(DigestUtils.md5Hex(Optional.ofNullable(comment.getEmail()).orElse("")));
    }
    if (StringUtils.isNotEmpty(comment.getAuthorUrl())) {
        comment.setAuthorUrl(HaloUtils.normalizeUrl(comment.getAuthorUrl()));
    }
    if (authentication != null) {
        // Comment of blogger
        comment.setIsAdmin(true);
        comment.setStatus(CommentStatus.PUBLISHED);
    } else {
        // Comment of guest
        // Handle comment status
        Boolean needAudit = optionService.getByPropertyOrDefault(CommentProperties.NEW_NEED_CHECK, Boolean.class, true);
        comment.setStatus(needAudit ? CommentStatus.AUDITING : CommentStatus.PUBLISHED);
    }
    // Create comment
    COMMENT createdComment = super.create(comment);
    if (ServiceUtils.isEmptyId(createdComment.getParentId())) {
        if (authentication == null) {
            // New comment of guest
            eventPublisher.publishEvent(new CommentNewEvent(this, createdComment.getId()));
        }
    } else {
        // Reply comment
        eventPublisher.publishEvent(new CommentReplyEvent(this, createdComment.getId()));
    }
    return createdComment;
}
Also used : CommentReplyEvent(run.halo.app.event.comment.CommentReplyEvent) Authentication(run.halo.app.security.authentication.Authentication) CommentNewEvent(run.halo.app.event.comment.CommentNewEvent) NonNull(org.springframework.lang.NonNull) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Authentication

use of run.halo.app.security.authentication.Authentication in project halo by ruibaby.

the class BaseCommentServiceImpl method createBy.

@Override
@NonNull
@Transactional(rollbackFor = Exception.class)
public COMMENT createBy(@NonNull BaseCommentParam<COMMENT> commentParam) {
    Assert.notNull(commentParam, "Comment param must not be null");
    // Check user login status and set this field
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        // Blogger comment
        User user = authentication.getDetail().getUser();
        commentParam.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname());
        commentParam.setEmail(user.getEmail());
        commentParam.setAuthorUrl(optionService.getByPropertyOrDefault(BlogProperties.BLOG_URL, String.class, null));
    }
    // Validate the comment param manually
    ValidationUtils.validate(commentParam);
    if (authentication == null) {
        // Check email
        if (userService.getByEmail(commentParam.getEmail()).isPresent()) {
            throw new BadRequestException("不能使用博主的邮箱,如果您是博主,请登录管理端进行回复。");
        }
    }
    // Convert to comment
    return create(commentParam.convertTo());
}
Also used : User(run.halo.app.model.entity.User) Authentication(run.halo.app.security.authentication.Authentication) BadRequestException(run.halo.app.exception.BadRequestException) NonNull(org.springframework.lang.NonNull) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Authentication

use of run.halo.app.security.authentication.Authentication in project halo by ruibaby.

the class AdminServiceImpl method clearToken.

@Override
public void clearToken() {
    // Check if the current is logging in
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null) {
        throw new BadRequestException("您尚未登录,因此无法注销");
    }
    // Get current user
    User user = authentication.getDetail().getUser();
    // Clear access token
    cacheStore.getAny(SecurityUtils.buildAccessTokenKey(user), String.class).ifPresent(accessToken -> {
        // Delete token
        cacheStore.delete(SecurityUtils.buildTokenAccessKey(accessToken));
        cacheStore.delete(SecurityUtils.buildAccessTokenKey(user));
    });
    // Clear refresh token
    cacheStore.getAny(SecurityUtils.buildRefreshTokenKey(user), String.class).ifPresent(refreshToken -> {
        cacheStore.delete(SecurityUtils.buildTokenRefreshKey(refreshToken));
        cacheStore.delete(SecurityUtils.buildRefreshTokenKey(user));
    });
    eventPublisher.publishEvent(new LogEvent(this, user.getUsername(), LogType.LOGGED_OUT, user.getNickname()));
    log.info("You have been logged out, looking forward to your next visit!");
}
Also used : User(run.halo.app.model.entity.User) LogEvent(run.halo.app.event.logger.LogEvent) Authentication(run.halo.app.security.authentication.Authentication) BadRequestException(run.halo.app.exception.BadRequestException)

Aggregations

Authentication (run.halo.app.security.authentication.Authentication)12 NonNull (org.springframework.lang.NonNull)6 BadRequestException (run.halo.app.exception.BadRequestException)6 User (run.halo.app.model.entity.User)6 Transactional (org.springframework.transaction.annotation.Transactional)4 Nullable (org.springframework.lang.Nullable)3 CommentNewEvent (run.halo.app.event.comment.CommentNewEvent)3 CommentReplyEvent (run.halo.app.event.comment.CommentReplyEvent)3 LogEvent (run.halo.app.event.logger.LogEvent)3 AuthenticationException (run.halo.app.exception.AuthenticationException)3 UserDetail (run.halo.app.security.support.UserDetail)3