Search in sources :

Example 1 with UserAccountEntity

use of com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity in project boot-admin by hb0730.

the class UserInfoServiceImpl method save.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean save(@Nonnull UserInfoDTO dto) {
    UserInfoEntity entity = dto.convertTo();
    // 1.账号是否存在
    String username = dto.getUsername();
    if (StringUtils.isBlank(username)) {
        throw new BusinessException("用户账号不为空");
    }
    UserAccountEntity account = accountService.findUserAccountByUsername(username);
    if (Objects.nonNull(account)) {
        throw new BusinessException("用户账号已存在,请重新设置");
    }
    boolean result = super.save(entity);
    // 保存 用户账号
    account = new UserAccountEntity();
    account.setUsername(username);
    // 默认密码
    String password = DictUtils.getEntryValue(TYPE, DEFAULT_PASSWORD);
    if (StringUtils.isBlank(password)) {
        password = DEFAULT_PASSWORD_VALUE;
    }
    account.setPassword(PasswordSecurityUtils.encode(SecurityUtils.getPasswordEncoder(), password));
    account.setUserId(entity.getId());
    accountService.save(account);
    // 保存 用户角色
    Collection<Long> roleIds = dto.getRoleIds();
    if (!CollectionUtils.isEmpty(roleIds)) {
        userRoleService.updateUserRole(entity.getId(), roleIds);
    }
    // 保存 用户岗位
    Collection<Long> postIds = dto.getPostIds();
    if (!CollectionUtils.isEmpty(postIds)) {
        userPostService.updateUserPost(entity.getId(), postIds);
    }
    return result;
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) UserAccountEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity) UserInfoEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UserAccountEntity

use of com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity in project boot-admin by hb0730.

the class UserAccountServiceImpl method updatePassword.

@Override
public boolean updatePassword(@Nonnull Long userId, @Nonnull String password) {
    LambdaQueryWrapper<UserAccountEntity> query = Wrappers.lambdaQuery();
    query.eq(UserAccountEntity::getUserId, userId);
    UserAccountEntity accountEntity = super.getOne(query, false);
    if (null == accountEntity) {
        throw new AccountException(ResponseStatusEnum.USER_NAME_NOT_FONT, "根据用户id查询不到账号信息");
    }
    password = PasswordSecurityUtils.encode(SecurityUtils.getPasswordEncoder(), password);
    accountEntity.setPassword(password);
    return super.updateById(accountEntity);
}
Also used : AccountException(com.hb0730.boot.admin.exceptions.AccountException) UserAccountEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity)

Example 3 with UserAccountEntity

use of com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity in project boot-admin by hb0730.

the class UserInfoServiceImpl method loadUserByUsername.

@Override
public UserDTO loadUserByUsername(String username) {
    UserAccountEntity accountEntity = accountService.findUserAccountByUsername(username);
    if (null == accountEntity) {
        return null;
    }
    Long userId = accountEntity.getUserId();
    UserInfoEntity entity = super.getById(userId);
    UserDTO user = BeanUtil.toBean(entity, UserDTO.class);
    assert user != null;
    user.setUsername(accountEntity.getUsername());
    user.setPassword(accountEntity.getPassword());
    // 用户角色
    Collection<Long> roleIds = userRoleService.findRoleByUserId(userId);
    if (CollectionUtils.isEmpty(roleIds)) {
        return user;
    }
    List<RoleEntity> roles = roleService.findEnabledRoleByIds(roleIds);
    if (CollectionUtils.isEmpty(roles)) {
        return user;
    }
    Map<Long, String> roleMap = roles.parallelStream().collect(Collectors.toMap(RoleEntity::getId, RoleEntity::getCode));
    user.setRoleIds(roleMap.keySet());
    user.setRole(roleMap.values());
    // 权限
    Map<Long, List<Long>> permission = rolePermissionService.findPermissionIdByRoleId(roleIds);
    if (CollectionUtils.isEmpty(permission)) {
        return user;
    }
    Set<Long> permissionIds = permission.values().stream().flatMap(List::stream).collect(Collectors.toSet());
    List<PermissionEntity> permissionEntities = permissionService.findEnabledPermissionByIds(permissionIds);
    if (CollectionUtils.isEmpty(permissionEntities)) {
        return user;
    }
    Map<Long, String> permissionMap = permissionEntities.parallelStream().collect(Collectors.toMap(PermissionEntity::getId, PermissionEntity::getPermission));
    user.setPermission(permissionMap.values());
    user.setPermissionIds(permissionMap.keySet());
    // 岗位
    List<Long> postIds = userPostService.findPostIdByUserIds(Collections.singletonList(userId));
    user.setPostIds(postIds);
    return user;
}
Also used : UserAccountEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity) UserDTO(com.hb0730.boot.admin.project.system.user.model.dto.UserDTO) UserInfoEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity) UserRoleEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserRoleEntity) RoleEntity(com.hb0730.boot.admin.project.system.role.model.entity.RoleEntity) List(java.util.List) PermissionEntity(com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)

Example 4 with UserAccountEntity

use of com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity in project boot-admin by hb0730.

the class UserAccountServiceImpl method save.

@Override
public boolean save(@NonNull UserAccountDTO dto) {
    ValidatorUtils.validate(dto);
    String username = dto.getUsername();
    Long userId = dto.getUserId();
    UserAccountEntity entity = userAccountByUserId(userId);
    if (null != entity) {
        throw new BusinessException("用户id已绑定账号,请勿重新绑定");
    }
    entity = findUserAccountByUsername(username);
    if (null != entity) {
        throw new BusinessException("用户账号已存在,请重新设置");
    }
    entity = dto.convertTo();
    return super.save(entity);
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) UserAccountEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity)

Aggregations

UserAccountEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity)4 BusinessException (com.hb0730.boot.admin.exceptions.BusinessException)2 UserInfoEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity)2 AccountException (com.hb0730.boot.admin.exceptions.AccountException)1 PermissionEntity (com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)1 RoleEntity (com.hb0730.boot.admin.project.system.role.model.entity.RoleEntity)1 UserDTO (com.hb0730.boot.admin.project.system.user.model.dto.UserDTO)1 UserRoleEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserRoleEntity)1 List (java.util.List)1 Transactional (org.springframework.transaction.annotation.Transactional)1