|
|
|
@@ -1,9 +1,20 @@ |
|
|
|
package vn.azteam.tpf.web.rest; |
|
|
|
|
|
|
|
import com.codahale.metrics.annotation.Timed; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import io.github.jhipster.web.util.ResponseUtil; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.data.domain.Page; |
|
|
|
import org.springframework.data.domain.PageImpl; |
|
|
|
import org.springframework.data.domain.Pageable; |
|
|
|
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.security.access.prepost.PreAuthorize; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import vn.azteam.tpf.config.Constants; |
|
|
|
import vn.azteam.tpf.domain.TBDetailUser; |
|
|
|
@@ -12,30 +23,18 @@ import vn.azteam.tpf.repository.UserRepository; |
|
|
|
import vn.azteam.tpf.repository.search.UserSearchRepository; |
|
|
|
import vn.azteam.tpf.security.AuthoritiesConstants; |
|
|
|
import vn.azteam.tpf.service.*; |
|
|
|
import vn.azteam.tpf.service.dto.TBRoleDTO; |
|
|
|
import vn.azteam.tpf.service.dto.UserDTO; |
|
|
|
import vn.azteam.tpf.service.mapper.UserMapper; |
|
|
|
import vn.azteam.tpf.service.util.UserRoleUtil; |
|
|
|
import vn.azteam.tpf.web.rest.errors.*; |
|
|
|
import vn.azteam.tpf.web.rest.util.HeaderUtil; |
|
|
|
import vn.azteam.tpf.web.rest.util.PaginationUtil; |
|
|
|
import com.codahale.metrics.annotation.Timed; |
|
|
|
import io.github.jhipster.web.util.ResponseUtil; |
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.data.domain.Page; |
|
|
|
import org.springframework.data.domain.Pageable; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.security.access.prepost.PreAuthorize; |
|
|
|
import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
import javax.validation.Valid; |
|
|
|
import java.net.URI; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.util.*; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -120,7 +119,7 @@ public class UserResource { |
|
|
|
* |
|
|
|
* @param userDTO the user to create |
|
|
|
* @return the ResponseEntity with status 201 (Created) and with body the new user, or with status 400 (Bad Request) if the login or email is already in use |
|
|
|
* @throws URISyntaxException if the Location URI syntax is incorrect |
|
|
|
* @throws URISyntaxException if the Location URI syntax is incorrect |
|
|
|
* @throws BadRequestAlertException 400 (Bad Request) if the login or email is already in use |
|
|
|
*/ |
|
|
|
@PostMapping("/users") |
|
|
|
@@ -135,13 +134,13 @@ public class UserResource { |
|
|
|
throw new LoginAlreadyUsedException(); |
|
|
|
} else if (userRepository.findOneByEmailIgnoreCase(userDTO.getEmail()).isPresent()) { |
|
|
|
throw new EmailAlreadyUsedException(); |
|
|
|
} else if(tbDetailUserQueryService.findOneByPhone(userDTO.getPhone()).isPresent()) { |
|
|
|
} else if (tbDetailUserQueryService.findOneByPhone(userDTO.getPhone()).isPresent()) { |
|
|
|
throw new PhoneAlreadyUsedException(); |
|
|
|
} else { |
|
|
|
User newUser = userService.createUser(userDTO); |
|
|
|
mailService.sendCreationEmail(newUser); |
|
|
|
return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin())) |
|
|
|
.headers(HeaderUtil.createAlert( "userManagement.created", newUser.getLogin())) |
|
|
|
.headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin())) |
|
|
|
.body(newUser); |
|
|
|
} |
|
|
|
} |
|
|
|
@@ -167,7 +166,7 @@ public class UserResource { |
|
|
|
throw new LoginAlreadyUsedException(); |
|
|
|
} |
|
|
|
Optional<TBDetailUser> existingTBDetailUser = tbDetailUserQueryService.findOneByPhone(userDTO.getPhone()); |
|
|
|
if(existingTBDetailUser.isPresent() && (!existingTBDetailUser.get().getUser().getId().equals(userDTO.getId()))){ |
|
|
|
if (existingTBDetailUser.isPresent() && (!existingTBDetailUser.get().getUser().getId().equals(userDTO.getId()))) { |
|
|
|
throw new PhoneAlreadyUsedException(); |
|
|
|
} |
|
|
|
Optional<UserDTO> updatedUser = userService.updateUser(userDTO); |
|
|
|
@@ -197,7 +196,7 @@ public class UserResource { |
|
|
|
throw new LoginAlreadyUsedException(); |
|
|
|
} |
|
|
|
Optional<TBDetailUser> existingTBDetailUser = tbDetailUserQueryService.findOneByPhone(userDTO.getPhone()); |
|
|
|
if(existingTBDetailUser.isPresent() && (!existingTBDetailUser.get().getUser().getId().equals(userDTO.getId()))){ |
|
|
|
if (existingTBDetailUser.isPresent() && (!existingTBDetailUser.get().getUser().getId().equals(userDTO.getId()))) { |
|
|
|
throw new PhoneAlreadyUsedException(); |
|
|
|
} |
|
|
|
Optional<UserDTO> updatedUser = userService.updateUser(userDTO); |
|
|
|
@@ -213,13 +212,14 @@ public class UserResource { |
|
|
|
* @throws EmailAlreadyUsedException 400 (Bad Request) if the email is already in use |
|
|
|
* @throws LoginAlreadyUsedException 400 (Bad Request) if the login is already in use |
|
|
|
*/ |
|
|
|
@PutMapping("/update-my-profile") |
|
|
|
@PutMapping("/update-my-profile") |
|
|
|
@Timed |
|
|
|
public ResponseEntity<UserDTO> updateMyProfile(@Valid @RequestBody UserDTO userDTO) { |
|
|
|
return updateUser(userDTO); |
|
|
|
} |
|
|
|
|
|
|
|
/**F |
|
|
|
/** |
|
|
|
* F |
|
|
|
* GET /users : get all users. |
|
|
|
* |
|
|
|
* @param pageable the pagination information |
|
|
|
@@ -227,25 +227,39 @@ public class UserResource { |
|
|
|
*/ |
|
|
|
@GetMapping("/users") |
|
|
|
@Timed |
|
|
|
public ResponseEntity<List<UserDTO>> getAllUsers(Pageable pageable) { |
|
|
|
public ResponseEntity<List<UserDTO>> getAllUsersPagination(Pageable pageable) { |
|
|
|
UserDTO currentUser = userService.getCurrentUserDTO().get(); |
|
|
|
Page<UserDTO> page = userService.getAllManagedUsers(pageable); |
|
|
|
List<UserDTO> result = page.getContent(); |
|
|
|
if(currentUser.getCustomerId() != null){ |
|
|
|
if (currentUser.getCustomerId() != null) { |
|
|
|
result = result.stream() |
|
|
|
.filter(item -> item.getCustomerId() != null |
|
|
|
&& item.getCustomerId().equals(currentUser.getCustomerId())) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
} |
|
|
|
page = changeUserDTOToPageFromList(result,pageable); |
|
|
|
page = changeUserDTOToPageFromList(result, pageable); |
|
|
|
HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/users"); |
|
|
|
return new ResponseEntity<>(result, headers, HttpStatus.OK); |
|
|
|
} |
|
|
|
|
|
|
|
private Page<UserDTO> changeUserDTOToPageFromList(List<UserDTO> userDTOS, Pageable pageable){ |
|
|
|
@GetMapping("/users/all") |
|
|
|
@Timed |
|
|
|
public ResponseEntity<List<UserDTO>> getAllUsers() { |
|
|
|
UserDTO currentUser = userService.getCurrentUserDTO().get(); |
|
|
|
List<UserDTO> result = userService.getAllManagedUsers(); |
|
|
|
if (currentUser.getCustomerId() != null) { |
|
|
|
result = result.stream() |
|
|
|
.filter(item -> item.getCustomerId() != null |
|
|
|
&& item.getCustomerId().equals(currentUser.getCustomerId())) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
} |
|
|
|
return ResponseEntity.ok().body(result); |
|
|
|
} |
|
|
|
|
|
|
|
private Page<UserDTO> changeUserDTOToPageFromList(List<UserDTO> userDTOS, Pageable pageable) { |
|
|
|
int start = Math.toIntExact(pageable.getOffset()); |
|
|
|
int end = Math.toIntExact((start + pageable.getPageSize()) > userDTOS.size() ? userDTOS.size() : (start + pageable.getPageSize())); |
|
|
|
if(userDTOS.size() > start) { |
|
|
|
if (userDTOS.size() > start) { |
|
|
|
return new PageImpl<>(userDTOS.subList(start, end), pageable, userDTOS.size()); |
|
|
|
} |
|
|
|
return new PageImpl<>(Lists.newArrayList(), pageable, userDTOS.size()); |
|
|
|
@@ -289,7 +303,7 @@ public class UserResource { |
|
|
|
log.debug("REST request to get User : {}", id); |
|
|
|
UserDTO currentUser = userService.getCurrentUserDTO().get(); |
|
|
|
Optional<UserDTO> userSelected = userService.getUserWithAuthorities(id).map(UserDTO::new); |
|
|
|
if(currentUser.getCustomerId() != null && !currentUser.getCustomerId().equals(userSelected.get().getCustomerId())) { |
|
|
|
if (currentUser.getCustomerId() != null && !currentUser.getCustomerId().equals(userSelected.get().getCustomerId())) { |
|
|
|
userSelected = Optional.empty(); |
|
|
|
} |
|
|
|
return ResponseUtil.wrapOrNotFound(userSelected); |
|
|
|
@@ -307,18 +321,18 @@ public class UserResource { |
|
|
|
log.debug("REST request to delete User: {}", login); |
|
|
|
UserDTO currentUser = userService.getCurrentUserDTO().get(); |
|
|
|
UserDTO deleteUser = userService.getUserWithAuthoritiesByLogin(login).map(UserDTO::new).get(); |
|
|
|
if(currentUser.getCustomerId() != null && !currentUser.getCustomerId().equals(deleteUser.getCustomerId())) { |
|
|
|
if (currentUser.getCustomerId() != null && !currentUser.getCustomerId().equals(deleteUser.getCustomerId())) { |
|
|
|
throw new BadRequestAlertException("1019", ENTITY_NAME, "1019"); |
|
|
|
} |
|
|
|
userService.deleteUser(login); |
|
|
|
return ResponseEntity.ok().headers(HeaderUtil.createAlert( "userManagement.deleted", login)).build(); |
|
|
|
return ResponseEntity.ok().headers(HeaderUtil.createAlert("userManagement.deleted", login)).build(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* SEARCH /_search/users/:query : search for the User corresponding |
|
|
|
* to the query. |
|
|
|
* |
|
|
|
* @param query the query to search |
|
|
|
* @param query the query to search |
|
|
|
* @param pageable |
|
|
|
* @return the result of the search |
|
|
|
*/ |
|
|
|
@@ -328,17 +342,17 @@ public class UserResource { |
|
|
|
UserDTO currentUser = userService.getCurrentUserDTO().get(); |
|
|
|
log.debug("REST request to search for a page of Users for query {}", query); |
|
|
|
Page<UserDTO> page; |
|
|
|
if(query != null && !query.isEmpty()) { |
|
|
|
if (query != null && !query.isEmpty()) { |
|
|
|
page = userService.searchUser(query, pageable); |
|
|
|
} else { |
|
|
|
page = userService.getAllManagedUsers(pageable); |
|
|
|
} |
|
|
|
List<UserDTO> result = page.getContent(); |
|
|
|
if(currentUser.getCustomerId() != null){ |
|
|
|
if (currentUser.getCustomerId() != null) { |
|
|
|
result = result.stream() |
|
|
|
.filter(item -> item.getCustomerId() != null && item.getCustomerId() == currentUser.getCustomerId()) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
page = changeUserDTOToPageFromList(result,pageable); |
|
|
|
page = changeUserDTOToPageFromList(result, pageable); |
|
|
|
} |
|
|
|
HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/users"); |
|
|
|
return ResponseEntity.ok().headers(headers).body(page.getContent()); |
|
|
|
@@ -346,6 +360,7 @@ public class UserResource { |
|
|
|
|
|
|
|
/** |
|
|
|
* Upload images |
|
|
|
* |
|
|
|
* @param userId |
|
|
|
* @param images |
|
|
|
* @return |
|
|
|
@@ -354,7 +369,7 @@ public class UserResource { |
|
|
|
@PostMapping("/users/upload-avatar/{userId}") |
|
|
|
@Timed |
|
|
|
public ResponseEntity<String> uploadAvatar(@PathVariable Long userId, |
|
|
|
@RequestParam(value="images") final MultipartFile[] images) |
|
|
|
@RequestParam(value = "images") final MultipartFile[] images) |
|
|
|
throws URISyntaxException { |
|
|
|
log.debug("REST request to upload avatar user : {}", userId); |
|
|
|
|
|
|
|
@@ -378,7 +393,7 @@ public class UserResource { |
|
|
|
UserDTO currentUserDTO = userService.getUserWithAuthorities() |
|
|
|
.map(UserDTO::new) |
|
|
|
.orElseThrow(() -> new InternalServerErrorException("1006")); |
|
|
|
if(currentUserDTO.getFcmToken() == null || currentUserDTO.getFcmToken() == "") { |
|
|
|
if (currentUserDTO.getFcmToken() == null || currentUserDTO.getFcmToken() == "") { |
|
|
|
currentUserDTO.setFcmToken(fcmToken); |
|
|
|
} else if (currentUserDTO.getFcmToken().contains(fcmToken)) { |
|
|
|
currentUserDTO.setFcmToken(currentUserDTO.getFcmToken()); |
|
|
|
@@ -402,10 +417,10 @@ public class UserResource { |
|
|
|
UserDTO currentUserDTO = userService.getUserWithAuthorities() |
|
|
|
.map(UserDTO::new) |
|
|
|
.orElseThrow(() -> new InternalServerErrorException("1006")); |
|
|
|
if(currentUserDTO.getFcmToken() != null && !currentUserDTO.getFcmToken().isEmpty() |
|
|
|
if (currentUserDTO.getFcmToken() != null && !currentUserDTO.getFcmToken().isEmpty() |
|
|
|
&& currentUserDTO.getFcmToken().contains(fcmToken)) { |
|
|
|
if(currentUserDTO.getFcmToken().contains(vn.azteam.tpf.Constants.Constants.COMMA_DOT)) { |
|
|
|
String remainingFcmToken = currentUserDTO.getFcmToken().replace(vn.azteam.tpf.Constants.Constants.COMMA_DOT + fcmToken,vn.azteam.tpf.Constants.Constants.EMPTY); |
|
|
|
if (currentUserDTO.getFcmToken().contains(vn.azteam.tpf.Constants.Constants.COMMA_DOT)) { |
|
|
|
String remainingFcmToken = currentUserDTO.getFcmToken().replace(vn.azteam.tpf.Constants.Constants.COMMA_DOT + fcmToken, vn.azteam.tpf.Constants.Constants.EMPTY); |
|
|
|
currentUserDTO.setFcmToken(remainingFcmToken); |
|
|
|
} else { |
|
|
|
currentUserDTO.setFcmToken(null); |