Files
Clyde/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java
Bartha Maxime 4b1db883e2
All checks were successful
Build and test backend / Build-backend (pull_request) Successful in 2m0s
Build and test backend / Test-backend (pull_request) Successful in 1m58s
Build and test FrontEnd / Build-frontend (pull_request) Successful in 25s
updated tonitch's reviews
2024-03-13 15:28:17 +01:00

106 lines
3.6 KiB
Java

package ovh.herisson.Clyde.Services;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
import java.util.*;
@Service
public class UserService {
private final UserRepository userRepo;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public UserService(UserRepository userRepo){
this.userRepo = userRepo;
}
public User getUser(String identifier){
if (identifier == null) return null;
try {
int id = Integer.parseInt(identifier);
return userRepo.findById(id);
}
catch (NumberFormatException nfe){
return userRepo.findByEmail(identifier);
}
}
/** modify the target data
* verify the permission of modifying from the poster
*
* @param poster the user wanting to modify target's data
* @param updates the changes to be made
* @param target the user to update
* @return if the changes were done or not
*/
public boolean modifyData(User poster, Map<String ,Object> updates, User target){
if (poster.getRegNo().equals(target.getRegNo())){
for (Map.Entry<String, Object> entry : updates.entrySet()){
if ( entry.getKey().equals("regNo") || entry.getKey().equals("role")) {return false;}
switch (entry.getKey()){
case "firstName":
target.setFirstName((String) entry.getValue());
break;
case "lastName":
target.setLastName((String) entry.getValue());
break;
case "email":
target.setEmail((String) entry.getValue());
break;
case "address":
target.setAddress((String) entry.getValue());
break;
case "country":
target.setCountry((String) entry.getValue());
break;
case "birthDate":
target.setBirthDate((Date) entry.getValue());
break;
case "profilePictureUrl":
target.setProfilePictureUrl((String) entry.getValue());
break;
case "password":
target.setPassword(passwordEncoder.encode((String) entry.getValue()));
break;
}
}
userRepo.save(target);
return true;
}
// the secretary can change roles (for example if a student becomes a teacher)
else if (poster.getRole() == Role.Secretary)
{
for (Map.Entry<String, Object> entry : updates.entrySet()){
if ( !entry.getKey().equals("role")) {return false;}
if (entry.getValue() == Role.Admin){return false;}
target.setRole((Role) entry.getValue());
userRepo.save(target);
return true;
}
}
return false;
}
public boolean checkPassword(User user, String tryingPassword){
return passwordEncoder.matches(tryingPassword, user.getPassword());
}
public void save(User user){
user.setPassword(passwordEncoder.encode(user.getPassword()));
userRepo.save(user);
}
public Iterable<User> getAll(){
return userRepo.findAll();
}
}