Max/Backend/UserControllerUpdate #94
@ -15,6 +15,7 @@ import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@ -30,7 +31,7 @@ public class UserController {
|
||||
}
|
||||
|
||||
@GetMapping("/user")
|
||||
Maxime marked this conversation as resolved
Outdated
|
||||
public ResponseEntity<Object[]> getUser(@RequestHeader("Authorization") String authorization){
|
||||
public ResponseEntity<HashMap<String,Object>> getUser(@RequestHeader("Authorization") String authorization){
|
||||
|
||||
if (authorization == null) return new UnauthorizedResponse<>(null);
|
||||
User user = authServ.getUserFromToken(authorization);
|
||||
@ -50,13 +51,13 @@ public class UserController {
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
public ResponseEntity<Iterable<Object[]>> getAllUsers(@RequestHeader("Authorization") String authorization){
|
||||
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllUsers(@RequestHeader("Authorization") String authorization){
|
||||
|
||||
if (!isSecretaryOrAdmin(authorization))
|
||||
return new UnauthorizedResponse<>(null);
|
||||
|
||||
Iterable<User> users = userService.getAll();
|
||||
ArrayList<Object[]> withoutPassword = new ArrayList<>();
|
||||
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
|
||||
|
||||
for (User u :users){
|
||||
withoutPassword.add(userWithoutPassword(u));
|
||||
@ -82,8 +83,18 @@ public class UserController {
|
||||
* @param user the user to return
|
||||
* @return all the user data without the password
|
||||
*/
|
||||
private Object[] userWithoutPassword(User user){
|
||||
return new Object[] {user.getRegNo(),user.getFirstName(),user.getLastName(),user.getBirthDate(),user.getCountry(),user.getAddress(),user.getRole()};
|
||||
private HashMap<String,Object> userWithoutPassword(User user){
|
||||
Maxime marked this conversation as resolved
Outdated
tonitch
commented
est ce qu'une Map ne serait pas plus appropriée ? est ce qu'une Map ne serait pas plus appropriée ?
Maxime
commented
hashmapped ! hashmapped !
|
||||
HashMap<String,Object> toReturn = new HashMap<>();
|
||||
|
||||
toReturn.put("regNo",user.getRegNo());
|
||||
toReturn.put("firstName",user.getFirstName());
|
||||
toReturn.put("lastName",user.getLastName());
|
||||
toReturn.put("birthDate",user.getBirthDate());
|
||||
toReturn.put("country",user.getCountry());
|
||||
toReturn.put("address",user.getAddress());
|
||||
toReturn.put("role",user.getRole());
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private boolean isSecretaryOrAdmin(String authorization){
|
||||
|
@ -38,7 +38,6 @@ public class UserService {
|
||||
*/
|
||||
public boolean modifyData(User poster, Map<String ,Object> updates, User target){
|
||||
|
||||
System.out.printf("%s and %s",poster.getRegNo(),target.getRegNo());
|
||||
if (poster.getRegNo().equals(target.getRegNo())){
|
||||
Maxime marked this conversation as resolved
tonitch
commented
Si c'est du debug il faudrais l'enlever, si c'est du log c'est mieux d'utiliser Si c'est du debug il faudrais l'enlever, si c'est du log c'est mieux d'utiliser `Logger.info(msg)` https://docs.oracle.com/en/java/javase/21/docs/api/java.logging/java/util/logging/Logger.html#info(java.lang.String)
Maxime
commented
hups ! hups !
|
||||
for (Map.Entry<String, Object> entry : updates.entrySet()){
|
||||
|
||||
@ -67,7 +66,7 @@ public class UserService {
|
||||
target.setProfilePictureUrl((String) entry.getValue());
|
||||
break;
|
||||
case "password":
|
||||
target.setPassword(encodePassword((String) entry.getValue()));
|
||||
target.setPassword(passwordEncoder.encode((String) entry.getValue()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -97,14 +96,11 @@ public class UserService {
|
||||
}
|
||||
|
||||
public void save(User user){
|
||||
user.setPassword(encodePassword(user.getPassword()));
|
||||
user.setPassword(passwordEncoder.encode(user.getPassword()));
|
||||
userRepo.save(user);
|
||||
}
|
||||
|
||||
public Iterable<User> getAll(){
|
||||
return userRepo.findAll();
|
||||
}
|
||||
public String encodePassword(String rawPassword){
|
||||
return passwordEncoder.encode(rawPassword);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Autant ça me fait mal que ça soit un Object mais je comprends mais pourquoi une liste du coup ?
hashMapped !