Search in sources :

Example 21 with BadRequestException

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);
}
Also used : User(run.halo.app.model.entity.User) BadRequestException(run.halo.app.exception.BadRequestException) NonNull(org.springframework.lang.NonNull)

Example 22 with BadRequestException

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);
    }
}
Also used : ServiceException(run.halo.app.exception.ServiceException) ThemeUpdatedEvent(run.halo.app.event.theme.ThemeUpdatedEvent) BadRequestException(run.halo.app.exception.BadRequestException) NotFoundException(run.halo.app.exception.NotFoundException) ServiceException(run.halo.app.exception.ServiceException) ThemeNotFoundException(run.halo.app.exception.ThemeNotFoundException) ThemePropertyMissingException(run.halo.app.exception.ThemePropertyMissingException) ThemeNotSupportException(run.halo.app.exception.ThemeNotSupportException) ThemeUpdateException(run.halo.app.exception.ThemeUpdateException) IOException(java.io.IOException) ForbiddenException(run.halo.app.exception.ForbiddenException) BadRequestException(run.halo.app.exception.BadRequestException) ThemeProperty(run.halo.app.handler.theme.config.support.ThemeProperty) Transactional(org.springframework.transaction.annotation.Transactional)

Example 23 with BadRequestException

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;
}
Also used : User(run.halo.app.model.entity.User) BadRequestException(run.halo.app.exception.BadRequestException) UserUpdatedEvent(run.halo.app.event.user.UserUpdatedEvent) CacheLock(run.halo.app.cache.lock.CacheLock)

Example 24 with BadRequestException

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("安装完成!");
}
Also used : User(run.halo.app.model.entity.User) Category(run.halo.app.model.entity.Category) LogEvent(run.halo.app.event.logger.LogEvent) PostDetailVO(run.halo.app.model.vo.PostDetailVO) BadRequestException(run.halo.app.exception.BadRequestException) CacheLock(run.halo.app.cache.lock.CacheLock) PostMapping(org.springframework.web.bind.annotation.PostMapping) ApiOperation(io.swagger.annotations.ApiOperation) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 25 with BadRequestException

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("两步验证码验证异常");
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) BadRequestException(run.halo.app.exception.BadRequestException)

Aggregations

BadRequestException (run.halo.app.exception.BadRequestException)41 User (run.halo.app.model.entity.User)26 LogEvent (run.halo.app.event.logger.LogEvent)15 NonNull (org.springframework.lang.NonNull)12 NotFoundException (run.halo.app.exception.NotFoundException)9 IOException (java.io.IOException)6 CacheLock (run.halo.app.cache.lock.CacheLock)6 ForbiddenException (run.halo.app.exception.ForbiddenException)6 ServiceException (run.halo.app.exception.ServiceException)6 Authentication (run.halo.app.security.authentication.Authentication)6 ThemeUpdatedEvent (run.halo.app.event.theme.ThemeUpdatedEvent)5 Category (run.halo.app.model.entity.Category)5 Transactional (org.springframework.transaction.annotation.Transactional)4 ApiOperation (io.swagger.annotations.ApiOperation)3 Path (java.nio.file.Path)3 GeneralSecurityException (java.security.GeneralSecurityException)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 UserUpdatedEvent (run.halo.app.event.user.UserUpdatedEvent)3 ThemeNotFoundException (run.halo.app.exception.ThemeNotFoundException)3