Merge pull request 'Max/Backend/UserControllerUpdate' (#94) from Max/Backend/UserControllerUpdate into master
All checks were successful
deploy to production / deploy-frontend (push) Successful in 25s
Build and test backend / Build-backend (push) Successful in 2m16s
Build and test backend / Test-backend (push) Successful in 1m22s
deploy to production / deploy-backend (push) Successful in 2m24s
Build and test FrontEnd / Build-frontend (push) Successful in 26s

Reviewed-on: #94
Reviewed-by: LeoMoulin <leomoulin125@gmail.com>
Reviewed-by: Debucquoy Anthony <d.tonitch@gmail.com>
This commit is contained in:
2024-03-14 21:53:33 +01:00
4 changed files with 160 additions and 18 deletions

View File

@ -5,16 +5,10 @@ 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.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;
@Service
public class UserService {
private final UserRepository userRepo;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
@ -34,17 +28,79 @@ public class UserService {
}
}
/** 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();
}
}