Search in sources :

Example 1 with User

use of com.rafaelvieira.letmebuy.entities.User in project letmebuy by rafaelrok.

the class JwtTokenEnhancer method enhance.

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    User user = userRepository.findByEmail(authentication.getName());
    Map<String, Object> map = new HashMap<>();
    map.put("userFirstName", user.getFirstName());
    map.put("userId", user.getId());
    DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) accessToken;
    token.setAdditionalInformation(map);
    return accessToken;
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) HashMap(java.util.HashMap) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Example 2 with User

use of com.rafaelvieira.letmebuy.entities.User in project letmebuy by rafaelrok.

the class UserUpdateValidator method isValid.

@Override
public boolean isValid(UserUpdateDTO dto, ConstraintValidatorContext context) {
    @SuppressWarnings("unchecked") var uriVars = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
    long userId = Long.parseLong(uriVars.get("id"));
    List<FieldMessage> list = new ArrayList<>();
    User user = repository.findByEmail(dto.getEmail());
    if (user != null && userId != user.getId()) {
        list.add(new FieldMessage("email", "Email já existe"));
    }
    for (FieldMessage e : list) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(e.getMessage()).addPropertyNode(e.getFieldName()).addConstraintViolation();
    }
    return list.isEmpty();
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) ArrayList(java.util.ArrayList) Map(java.util.Map) FieldMessage(com.rafaelvieira.letmebuy.controllers.exceptions.FieldMessage)

Example 3 with User

use of com.rafaelvieira.letmebuy.entities.User in project letmebuy by rafaelrok.

the class UserService method update.

@Transactional
public UserDTO update(Long id, UserUpdateDTO dto) {
    try {
        User entity = repository.getOne(id);
        copyDtoToEntity(dto, entity);
        entity = repository.save(entity);
        return new UserDTO(entity);
    } catch (EntityNotFoundException e) {
        throw new ResourceNotFoundException("Id not found " + id);
    }
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) EntityNotFoundException(javax.persistence.EntityNotFoundException) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with User

use of com.rafaelvieira.letmebuy.entities.User in project letmebuy by rafaelrok.

the class UserService method findById.

@Transactional(readOnly = true)
public UserDTO findById(Long id) {
    authService.validateSelfOrAdmin(id);
    Optional<User> obj = repository.findById(id);
    User entity = obj.orElseThrow(() -> new ResourceNotFoundException("Entity not found"));
    return new UserDTO(entity);
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with User

use of com.rafaelvieira.letmebuy.entities.User in project letmebuy by rafaelrok.

the class UserService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = repository.findByEmail(username);
    if (user == null) {
        logger.error("User not found: " + username);
        throw new UsernameNotFoundException("Email not found");
    }
    logger.info("User found: " + username);
    return user;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(com.rafaelvieira.letmebuy.entities.User)

Aggregations

User (com.rafaelvieira.letmebuy.entities.User)7 UserDTO (com.rafaelvieira.letmebuy.dto.UserDTO)3 Transactional (org.springframework.transaction.annotation.Transactional)3 FieldMessage (com.rafaelvieira.letmebuy.controllers.exceptions.FieldMessage)2 ResourceNotFoundException (com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)1