| | |
| | | package com.dji.sample.manage.service.impl; |
| | | |
| | | import com.auth0.jwt.JWT; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.dji.sample.common.model.CustomClaim; |
| | | import com.dji.sample.common.model.Pagination; |
| | | import com.dji.sample.common.model.PaginationData; |
| | | import com.dji.sample.common.model.ResponseResult; |
| | | import com.dji.sample.common.util.JwtUtil; |
| | | import com.dji.sample.component.mqtt.config.MqttConfiguration; |
| | | import com.dji.sample.manage.dao.IUserMapper; |
| | | import com.dji.sample.manage.model.dto.UserDTO; |
| | | import com.dji.sample.manage.model.dto.UserListDTO; |
| | | import com.dji.sample.manage.model.dto.WorkspaceDTO; |
| | | import com.dji.sample.manage.model.entity.UserEntity; |
| | | import com.dji.sample.manage.model.enums.UserTypeEnum; |
| | | import com.dji.sample.manage.service.IUserService; |
| | | import com.dji.sample.manage.service.IWorkspaceService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.time.Instant; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.util.List; |
| | | import java.util.Optional; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @Transactional |
| | |
| | | } |
| | | |
| | | @Override |
| | | public ResponseResult userLogin(String username, String password) { |
| | | public ResponseResult userLogin(String username, String password, Integer flag) { |
| | | // check user |
| | | UserEntity userEntity = this.getUserByUsername(username); |
| | | if (userEntity == null) { |
| | |
| | | .message("invalid username") |
| | | .build(); |
| | | } |
| | | if (flag.intValue() != userEntity.getUserType().intValue()) { |
| | | return ResponseResult.error("The account type does not match."); |
| | | } |
| | | if (!password.equals(userEntity.getPassword())) { |
| | | return ResponseResult.builder() |
| | | .code(HttpStatus.UNAUTHORIZED.value()) |
| | |
| | | .build(); |
| | | } |
| | | |
| | | Optional<WorkspaceDTO> workspaceOpt = workspaceService.getWorkspaceById(userEntity.getWorkspaceId()); |
| | | Optional<WorkspaceDTO> workspaceOpt = workspaceService.getWorkspaceByWorkspaceId(userEntity.getWorkspaceId()); |
| | | if (workspaceOpt.isEmpty()) { |
| | | return ResponseResult.builder() |
| | | .code(HttpStatus.UNAUTHORIZED.value()) |
| | |
| | | return Optional.of(user); |
| | | } |
| | | |
| | | @Override |
| | | public PaginationData<UserListDTO> getUsersByWorkspaceId(long page, long pageSize, String workspaceId) { |
| | | Page<UserEntity> userEntityPage = mapper.selectPage( |
| | | new Page<>(page, pageSize), |
| | | new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getWorkspaceId, workspaceId)); |
| | | |
| | | List<UserListDTO> usersList = userEntityPage.getRecords() |
| | | .stream() |
| | | .map(this::entity2UserListDTO) |
| | | .collect(Collectors.toList()); |
| | | return new PaginationData<>(usersList, new Pagination(userEntityPage)); |
| | | } |
| | | |
| | | @Override |
| | | public Boolean updateUser(String workspaceId, String userId, UserListDTO user) { |
| | | UserEntity userEntity = mapper.selectOne( |
| | | new LambdaQueryWrapper<UserEntity>() |
| | | .eq(UserEntity::getUserId, userId) |
| | | .eq(UserEntity::getWorkspaceId, workspaceId)); |
| | | if (userEntity == null) { |
| | | return false; |
| | | } |
| | | userEntity.setMqttUsername(user.getMqttUsername()); |
| | | userEntity.setMqttPassword(user.getMqttPassword()); |
| | | userEntity.setUpdateTime(System.currentTimeMillis()); |
| | | int id = mapper.update(userEntity, new LambdaUpdateWrapper<UserEntity>() |
| | | .eq(UserEntity::getUserId, userId) |
| | | .eq(UserEntity::getWorkspaceId, workspaceId)); |
| | | |
| | | return id > 0; |
| | | } |
| | | |
| | | /** |
| | | * Convert database entity objects into user data transfer object. |
| | | * @param entity |
| | | * @return |
| | | */ |
| | | private UserListDTO entity2UserListDTO(UserEntity entity) { |
| | | UserListDTO.UserListDTOBuilder builder = UserListDTO.builder(); |
| | | if (entity != null) { |
| | | builder.userId(entity.getUserId()) |
| | | .username(entity.getUsername()) |
| | | .mqttUsername(entity.getMqttUsername()) |
| | | .mqttPassword(entity.getMqttPassword()) |
| | | .userType(UserTypeEnum.find(entity.getUserType()).getDesc()) |
| | | .createTime(LocalDateTime.ofInstant( |
| | | Instant.ofEpochMilli(entity.getCreateTime()), ZoneId.systemDefault())); |
| | | Optional<WorkspaceDTO> workspaceOpt = workspaceService.getWorkspaceByWorkspaceId(entity.getWorkspaceId()); |
| | | workspaceOpt.ifPresent(workspace -> builder.workspaceName(workspace.getWorkspaceName())); |
| | | } |
| | | |
| | | return builder.build(); |
| | | } |
| | | |
| | | /** |
| | | * Query a user by username. |
| | | * @param username |
| | |
| | | .eq("username", username)); |
| | | } |
| | | |
| | | /** |
| | | * Convert database entity objects into user data transfer object. |
| | | * @param entity |
| | | * @return |
| | | */ |
| | | private UserDTO entityConvertToDTO(UserEntity entity) { |
| | | if (entity == null) { |
| | | return new UserDTO(); |