113 lines
4.1 KiB
Java
113 lines
4.1 KiB
Java
package SuMoC.backend.user.service;
|
|
|
|
import SuMoC.backend.user.UserRepository;
|
|
import SuMoC.backend.user.dtos.MinimalUserDTO;
|
|
import SuMoC.backend.user.enums.FriendShipStatus;
|
|
import SuMoC.backend.user.enums.RoleName;
|
|
import SuMoC.backend.user.enums.VisibilityPrivacy;
|
|
import SuMoC.backend.user.models.Friendship;
|
|
import SuMoC.backend.user.models.User;
|
|
import jakarta.transaction.Transactional;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
@Service
|
|
@Transactional
|
|
public class UserService {
|
|
private final UserRepository userRepository;
|
|
|
|
@Autowired
|
|
public UserService(UserRepository userRepository) {
|
|
this.userRepository = userRepository;
|
|
}
|
|
|
|
public MinimalUserDTO getUser(long id) {
|
|
User sender = getSender();
|
|
|
|
User requested = userRepository.findById(id).get();
|
|
|
|
// Prüfen, ob eine Freundschaft zum Sender existiert
|
|
FriendShipStatus status = requested.getFriendships().stream()
|
|
.filter(f -> sender.equals(f.getFriend()) || sender.equals(f.getUser()))
|
|
.map(Friendship::getStatus)
|
|
.findFirst()
|
|
.orElse(null); // nimmt die erste gefundene Freundschaft
|
|
|
|
if (status == FriendShipStatus.BLOCKED) throw new RuntimeException("Kein Zugriff auf dieses Profil");
|
|
|
|
//TODO: Think about private profile visibility heuristics
|
|
// if (requested.getVisibilityPrivacy() == VisibilityPrivacy.PRIVATE && !status.equals(FriendShipStatus.ACCEPTED)) {
|
|
// throw new RuntimeException("Kein Zugriff auf dieses Profil");
|
|
// }
|
|
|
|
switch (status) {
|
|
case ACCEPTED -> {
|
|
return new MinimalUserDTO(requested.getId(), requested.getPrivateName());
|
|
}
|
|
|
|
case BLOCKED -> {
|
|
throw new RuntimeException("Kein Zugriff auf dieses Profil");
|
|
}
|
|
|
|
default -> {
|
|
return new MinimalUserDTO(requested.getId(), requested.getPublicName());
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public void deleteUser(Long id) {
|
|
User sender = getSender();
|
|
|
|
if (!sender.getId().equals(id) && !sender.getRoles().contains(RoleName.ADMIN)) {
|
|
|
|
throw new RuntimeException("Ein Nutzer kann nur sein eigenes Profil löschen.");
|
|
}
|
|
// User is admin or deleting own profile
|
|
userRepository.deleteById(id);
|
|
}
|
|
|
|
public User getSender() {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
return userRepository.findByEmail(authentication.getName()).get();
|
|
}
|
|
|
|
public void changePassword(Long id, String newPassword) {
|
|
User sender = getSender();
|
|
if (!sender.getId().equals(id)) {
|
|
throw new RuntimeException("Ein Nutzer kann nur sein eigenes Passwort ändern.");
|
|
}
|
|
// TODO: Add Email Notification for password change
|
|
sender.setPassword(newPassword);
|
|
userRepository.save(sender);
|
|
}
|
|
|
|
public void changeEmail(Long id, String newEmail) {
|
|
User sender = getSender();
|
|
if (!sender.getId().equals(id)) {
|
|
throw new RuntimeException("Ein Nutzer kann nur seine eigene E-Mail-Adresse ändern.");
|
|
}
|
|
// TODO: Add Email Notification for password change
|
|
sender.setEmail(newEmail);
|
|
userRepository.save(sender);
|
|
}
|
|
|
|
public void changeVisibilityPrivacy(VisibilityPrivacy visibilityPrivacy) {
|
|
User user = getSender();
|
|
user.setVisibilityPrivacy(visibilityPrivacy);
|
|
}
|
|
|
|
public void changePrivateName(Long id, String newPrivateName) {
|
|
User sender = getSender();
|
|
if (!sender.getId().equals(id) && !sender.getRoles().contains(RoleName.ADMIN)) {
|
|
throw new RuntimeException("Not allowed to change private name of other users unless admin.");
|
|
}
|
|
User userToChange = userRepository.findById(id).orElseThrow();
|
|
userToChange.setPrivateName(newPrivateName);
|
|
userRepository.save(userToChange);
|
|
}
|
|
}
|