Changeset 775e15e for phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/UserService.java
- Timestamp:
- 09/17/22 01:23:47 (2 years ago)
- Branches:
- master
- Children:
- 7e88e46
- Parents:
- e5b84dc
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/UserService.java
re5b84dc r775e15e 1 1 package finki.it.phoneluxbackend.services; 2 2 3 3 import com.auth0.jwt.JWT; 4 import com.auth0.jwt.JWTVerifier; 5 import com.auth0.jwt.algorithms.Algorithm; 6 import com.auth0.jwt.interfaces.DecodedJWT; 7 import finki.it.phoneluxbackend.data.UserRole; 8 import finki.it.phoneluxbackend.entities.PhoneOffer; 4 9 import finki.it.phoneluxbackend.entities.User; 10 import finki.it.phoneluxbackend.repositories.PhoneOfferRepository; 5 11 import finki.it.phoneluxbackend.repositories.UserRepository; 6 12 import finki.it.phoneluxbackend.entities.ConfirmationToken; 7 13 import lombok.AllArgsConstructor; 8 9 14 import org.springframework.http.ResponseEntity; 10 15 import org.springframework.security.core.userdetails.UserDetails; … … 16 21 import java.time.LocalDateTime; 17 22 23 import java.util.Comparator; 24 import java.util.List; 18 25 import java.util.UUID; 26 import java.util.stream.Collectors; 19 27 20 28 @Service … … 22 30 public class UserService implements UserDetailsService { 23 31 private final UserRepository userRepository; 32 private final PhoneOfferRepository phoneOfferRepository; 24 33 private final BCryptPasswordEncoder bCryptPasswordEncoder; 25 34 private final ConfirmationTokenService confirmationTokenService; … … 68 77 69 78 79 public User getUserFromToken(String token) { 80 Algorithm algorithm = Algorithm.HMAC256("secret".getBytes()); 81 JWTVerifier verifier = JWT.require(algorithm).build(); 82 DecodedJWT decodedJWT = verifier.verify(token); 83 String email = decodedJWT.getSubject(); 84 UserRole role = UserRole.valueOf(decodedJWT.getClaim("role").asArray(String.class)[0]); 85 String name = decodedJWT.getClaim("name").as(String.class); 86 Long id = decodedJWT.getClaim("id").as(Long.class); 87 88 return new User(id,name,role); 89 } 90 91 public List<PhoneOffer> getFavouriteOffersForUser(Long userId) { 92 boolean exists = userRepository.existsById(userId); 93 if(!exists) 94 throw new IllegalStateException("User with id "+userId+" does not exist"); 95 96 return userRepository.findById(userId).get().getFavouriteOffers(); 97 } 98 99 public ResponseEntity<Object> editOfferForUser(Long userId, Long offerId, String option) { 100 boolean userExists = userRepository.existsById(userId); 101 if (!userExists) 102 { 103 return ResponseEntity.badRequest().body("User with id "+userId+" doesn't exist"); 104 } 105 106 boolean offerExists = phoneOfferRepository.existsById(offerId); 107 108 if (!offerExists) 109 { 110 return ResponseEntity.badRequest().body("Offer with id "+offerId+" doesn't exist"); 111 } 112 113 User user = userRepository.findById(userId).get(); 114 PhoneOffer phoneOffer = phoneOfferRepository.findById(offerId).get(); 115 116 if(option.equals("add")) { 117 user.getFavouriteOffers().add(phoneOffer); 118 } 119 else{ 120 user.getFavouriteOffers().remove(phoneOffer); 121 } 122 123 userRepository.save(user); 124 125 return ResponseEntity.ok().build(); 126 } 127 128 public List<User> getUsers(String searchValue) { 129 List<User> users = userRepository.findAll().stream() 130 .filter(user -> user.getUserRole() != UserRole.SUPERADMIN && user.getEnabled()) 131 .map(user -> new User(user.getId(),user.getFirstName(), 132 user.getLastName(),user.getEmail(),user.getUserRole())) 133 .collect(Collectors.toList()); 134 135 if(searchValue != null) 136 { 137 users = users.stream() 138 .filter(user -> user.getEmail().toLowerCase().contains(searchValue.stripIndent().toLowerCase()) 139 || user.getFirstName().toLowerCase().contains(searchValue.stripIndent().toLowerCase())) 140 .collect(Collectors.toList()); 141 } 142 143 return users.stream() 144 .sorted(Comparator.comparing(User::getId)) 145 .collect(Collectors.toList()); 146 147 } 148 149 public ResponseEntity<Object> giveAdminRoleToUser(Long userId) { 150 boolean userExists = userRepository.existsById(userId); 151 if (!userExists) 152 { 153 return ResponseEntity.badRequest().body("User with id "+userId+" doesn't exist"); 154 } 155 156 User user = userRepository.findById(userId).get(); 157 158 user.setUserRole(UserRole.ADMIN); 159 userRepository.save(user); 160 161 return ResponseEntity.ok().build(); 162 } 163 164 public ResponseEntity<Object> removeAdminRoleFromUser(Long userId) { 165 boolean userExists = userRepository.existsById(userId); 166 if (!userExists) 167 { 168 return ResponseEntity.badRequest().body("User with id "+userId+" doesn't exist"); 169 } 170 171 User user = userRepository.findById(userId).get(); 172 173 user.setUserRole(UserRole.USER); 174 userRepository.save(user); 175 return ResponseEntity.ok().build(); 176 } 70 177 }
Note:
See TracChangeset
for help on using the changeset viewer.