Search in sources :

Example 1 with UserInfoEntity

use of com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity 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 UserInfoEntity

use of com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity 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 3 with UserInfoEntity

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

the class UserInfoServiceImpl method restPassword.

@Override
public boolean restPassword(Long id) {
    UserInfoEntity entity = super.getById(id);
    if (entity.getIsAdmin() == 1) {
        return false;
    }
    // 默认密码
    String password = DictUtils.getEntryValue(TYPE, DEFAULT_PASSWORD);
    if (StringUtils.isBlank(password)) {
        password = DEFAULT_PASSWORD_VALUE;
    }
    return accountService.updatePassword(id, password);
}
Also used : UserInfoEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity)

Example 4 with UserInfoEntity

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

the class UserInfoServiceImpl method updateById.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateById(@Nonnull Long id, @Nonnull UserInfoDTO dto) {
    UserInfoEntity entity = dto.convertTo();
    entity.setId(id);
    boolean result = this.updateById(entity);
    // 更新角色
    userRoleService.updateUserRole(id, dto.getRoleIds());
    // 更新岗位
    userPostService.updateUserPost(id, dto.getPostIds());
    return result;
}
Also used : UserInfoEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with UserInfoEntity

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

the class UserInfoServiceImpl method updateById.

@Override
public boolean updateById(UserInfoEntity entity) {
    UserInfoEntity e1 = super.getById(entity.getId());
    BeanUtil.copyProperties(entity, e1);
    return super.updateById(e1);
}
Also used : UserInfoEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity)

Aggregations

UserInfoEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity)7 BusinessException (com.hb0730.boot.admin.exceptions.BusinessException)2 UserAccountEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity)2 Transactional (org.springframework.transaction.annotation.Transactional)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 Log (com.hb0730.boot.admin.annotation.Log)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 UserInfoDTO (com.hb0730.boot.admin.project.system.user.model.dto.UserInfoDTO)1 UserRoleEntity (com.hb0730.boot.admin.project.system.user.model.entity.UserRoleEntity)1 List (java.util.List)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1