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;
}
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());
}
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;
}
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());
}
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!");
}
Aggregations