use of run.halo.app.exception.BadRequestException in project halo by ruibaby.
the class AdminServiceImpl method refreshToken.
@Override
@NonNull
public AuthToken refreshToken(@NonNull String refreshToken) {
Assert.hasText(refreshToken, "Refresh token must not be blank");
Integer userId = cacheStore.getAny(SecurityUtils.buildTokenRefreshKey(refreshToken), Integer.class).orElseThrow(() -> new BadRequestException("登录状态已失效,请重新登录").setErrorData(refreshToken));
// Get user info
User user = userService.getById(userId);
// Remove all token
cacheStore.getAny(SecurityUtils.buildAccessTokenKey(user), String.class).ifPresent(accessToken -> cacheStore.delete(SecurityUtils.buildTokenAccessKey(accessToken)));
cacheStore.delete(SecurityUtils.buildTokenRefreshKey(refreshToken));
cacheStore.delete(SecurityUtils.buildAccessTokenKey(user));
cacheStore.delete(SecurityUtils.buildRefreshTokenKey(user));
return buildAuthToken(user);
}
use of run.halo.app.exception.BadRequestException in project halo by ruibaby.
the class ThemeServiceImpl method deleteTheme.
@Transactional
@Override
public void deleteTheme(@NonNull String themeId, @NonNull Boolean deleteSettings) {
// Get the theme property
ThemeProperty themeProperty = getThemeOfNonNullBy(themeId);
if (themeId.equals(getActivatedThemeId())) {
// Prevent to delete the activated theme
throw new BadRequestException("无法删除正在使用的主题!").setErrorData(themeId);
}
try {
// Delete the folder
FileUtils.deleteFolder(Paths.get(themeProperty.getThemePath()));
if (deleteSettings) {
// Delete theme settings
themeSettingRepository.deleteByThemeId(themeId);
}
// Delete theme cache
eventPublisher.publishEvent(new ThemeUpdatedEvent(this));
} catch (Exception e) {
throw new ServiceException("主题删除失败", e).setErrorData(themeId);
}
}
use of run.halo.app.exception.BadRequestException in project halo by ruibaby.
the class UserServiceImpl method create.
@Override
@CacheLock
public User create(User user) {
// Check user
if (count() != 0) {
throw new BadRequestException("当前博客已有用户");
}
User createdUser = super.create(user);
eventPublisher.publishEvent(new UserUpdatedEvent(this, createdUser.getId()));
return createdUser;
}
use of run.halo.app.exception.BadRequestException in project halo by ruibaby.
the class InstallController method installBlog.
@PostMapping
@ResponseBody
@CacheLock
@ApiOperation("Initializes the blog")
public BaseResponse<String> installBlog(@RequestBody InstallParam installParam) {
// Validate manually
ValidationUtils.validate(installParam, CreateCheck.class);
// Check is installed
boolean isInstalled = optionService.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false);
if (isInstalled) {
throw new BadRequestException("该博客已初始化,不能再次安装!");
}
// Initialize settings
initSettings(installParam);
// Create default user
User user = createUser(installParam);
// Create default category
Category category = createDefaultCategoryIfAbsent();
// Create default post
PostDetailVO post = createDefaultPostIfAbsent(category);
// Create default sheet
createDefaultSheet();
// Create default postComment
createDefaultComment(post);
// Create default menu
createDefaultMenu();
eventPublisher.publishEvent(new LogEvent(this, user.getId().toString(), LogType.BLOG_INITIALIZED, "博客已成功初始化"));
return BaseResponse.ok("安装完成!");
}
use of run.halo.app.exception.BadRequestException in project halo by ruibaby.
the class TwoFactorAuthUtils method validateTFACode.
public static void validateTFACode(String tfaKey, String tfaCode) {
try {
int validCode = Integer.parseInt(tfaCode);
boolean result = TimeBasedOneTimePasswordUtil.validateCurrentNumber(tfaKey, validCode, VALID_TFA_WINDOW_MILLIS);
if (!result) {
throw new BadRequestException("两步验证码验证错误,请确认时间是否同步");
}
} catch (NumberFormatException e) {
throw new BadRequestException("两步验证码请输入数字");
} catch (GeneralSecurityException e) {
throw new BadRequestException("两步验证码验证异常");
}
}
Aggregations